diff --git a/math/aliquot_sum.cpp b/math/aliquot_sum.cpp index 6d94d53a1..837be6b2f 100644 --- a/math/aliquot_sum.cpp +++ b/math/aliquot_sum.cpp @@ -20,6 +20,7 @@ */ #include /// for assert +#include /// for integral typedefs #include /// for IO operations /** diff --git a/math/check_factorial.cpp b/math/check_factorial.cpp index 0be45b895..05898fbcd 100644 --- a/math/check_factorial.cpp +++ b/math/check_factorial.cpp @@ -10,6 +10,7 @@ * @author [ewd00010](https://github.com/ewd00010) */ #include /// for assert +#include /// for integral typedefs #include /// for cout /** diff --git a/math/double_factorial.cpp b/math/double_factorial.cpp index 72feda60c..878f87ec4 100644 --- a/math/double_factorial.cpp +++ b/math/double_factorial.cpp @@ -10,6 +10,7 @@ */ #include +#include /// for integral typedefs #include /** Compute double factorial using iterative method diff --git a/math/eulers_totient_function.cpp b/math/eulers_totient_function.cpp index f1752e9e9..da03a2531 100644 --- a/math/eulers_totient_function.cpp +++ b/math/eulers_totient_function.cpp @@ -1,6 +1,7 @@ /** * @file - * @brief Implementation of [Euler's Totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function) + * @brief Implementation of [Euler's + * Totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function) * @description * Euler Totient Function is also known as phi function. * \f[\phi(n) = @@ -24,8 +25,9 @@ * @author [Mann Mehta](https://github.com/mann2108) */ -#include /// for IO operations -#include /// for assert +#include /// for assert +#include /// for integral typedefs +#include /// for IO operations /** * @brief Mathematical algorithms @@ -39,12 +41,14 @@ namespace math { uint64_t phiFunction(uint64_t n) { uint64_t result = n; for (uint64_t i = 2; i * i <= n; i++) { - if (n % i != 0) continue; + if (n % i != 0) + continue; while (n % i == 0) n /= i; result -= result / i; } - if (n > 1) result -= result / n; + if (n > 1) + result -= result / n; return result; } diff --git a/math/factorial.cpp b/math/factorial.cpp index acfa053d8..a6b039da4 100644 --- a/math/factorial.cpp +++ b/math/factorial.cpp @@ -12,8 +12,8 @@ */ #include /// for assert +#include /// for integral typedefs #include /// for I/O operations - /** * @namespace * @brief Mathematical algorithms diff --git a/math/fibonacci.cpp b/math/fibonacci.cpp index 4e3c15de8..1cd50e7ce 100644 --- a/math/fibonacci.cpp +++ b/math/fibonacci.cpp @@ -9,8 +9,8 @@ * @see fibonacci_large.cpp, fibonacci_fast.cpp, string_fibonacci.cpp */ #include +#include /// for integral typedefs #include - /** * Recursively compute sequences * @param n input @@ -31,7 +31,7 @@ uint64_t fibonacci(uint64_t n) { * Function for testing the fibonacci() function with a few * test cases and assert statement. * @returns `void` -*/ + */ static void test() { uint64_t test_case_1 = fibonacci(0); assert(test_case_1 == 0); diff --git a/math/fibonacci_matrix_exponentiation.cpp b/math/fibonacci_matrix_exponentiation.cpp index 1a119d210..3dd4c7213 100644 --- a/math/fibonacci_matrix_exponentiation.cpp +++ b/math/fibonacci_matrix_exponentiation.cpp @@ -1,101 +1,103 @@ /** - * @file + * @file * @brief This program computes the N^th Fibonacci number in modulo mod * input argument . * * Takes O(logn) time to compute nth Fibonacci number - * + * * * \author [villayatali123](https://github.com/villayatali123) * \author [unknown author]() - * @see fibonacci.cpp, fibonacci_fast.cpp, string_fibonacci.cpp, fibonacci_large.cpp + * @see fibonacci.cpp, fibonacci_fast.cpp, string_fibonacci.cpp, + * fibonacci_large.cpp */ -#include -#include #include +#include /// for integral typedefs +#include +#include /** * This function finds nth fibonacci number in a given modulus * @param n nth fibonacci number - * @param mod modulo number + * @param mod modulo number */ -uint64_t fibo(uint64_t n , uint64_t mod ) -{ - std::vector result(2,0); - std::vector> transition(2,std::vector(2,0)); - std::vector> Identity(2,std::vector(2,0)); - n--; - result[0]=1, result[1]=1; - Identity[0][0]=1; Identity[0][1]=0; - Identity[1][0]=0; Identity[1][1]=1; - - transition[0][0]=0; - transition[1][0]=transition[1][1]=transition[0][1]=1; - - while(n) - { - if(n%2) - { - std::vector> res(2, std::vector(2,0)); - for(int i=0;i<2;i++) - { - for(int j=0;j<2;j++) - { - for(int k=0;k<2;k++) - { - res[i][j]=(res[i][j]%mod+((Identity[i][k]%mod*transition[k][j]%mod))%mod)%mod; - } - } - } - for(int i=0;i<2;i++) - { - for(int j=0;j<2;j++) - { - Identity[i][j]=res[i][j]; - } - } - n--; - } - else{ - std::vector> res1(2, std::vector(2,0)); - for(int i=0;i<2;i++) - { - for(int j=0;j<2;j++) - { - for(int k=0;k<2;k++) - { - res1[i][j]=(res1[i][j]%mod+((transition[i][k]%mod*transition[k][j]%mod))%mod)%mod; - } - } - } - for(int i=0;i<2;i++) - { - for(int j=0;j<2;j++) - { - transition[i][j]=res1[i][j]; - } - } - n=n/2; - } - } - return ((result[0]%mod*Identity[0][0]%mod)%mod+(result[1]%mod*Identity[1][0]%mod)%mod)%mod; +uint64_t fibo(uint64_t n, uint64_t mod) { + std::vector result(2, 0); + std::vector> transition(2, + std::vector(2, 0)); + std::vector> Identity(2, std::vector(2, 0)); + n--; + result[0] = 1, result[1] = 1; + Identity[0][0] = 1; + Identity[0][1] = 0; + Identity[1][0] = 0; + Identity[1][1] = 1; + + transition[0][0] = 0; + transition[1][0] = transition[1][1] = transition[0][1] = 1; + + while (n) { + if (n % 2) { + std::vector> res(2, + std::vector(2, 0)); + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 2; j++) { + for (int k = 0; k < 2; k++) { + res[i][j] = + (res[i][j] % mod + + ((Identity[i][k] % mod * transition[k][j] % mod)) % + mod) % + mod; + } + } + } + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 2; j++) { + Identity[i][j] = res[i][j]; + } + } + n--; + } else { + std::vector> res1( + 2, std::vector(2, 0)); + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 2; j++) { + for (int k = 0; k < 2; k++) { + res1[i][j] = + (res1[i][j] % mod + ((transition[i][k] % mod * + transition[k][j] % mod)) % + mod) % + mod; + } + } + } + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 2; j++) { + transition[i][j] = res1[i][j]; + } + } + n = n / 2; + } + } + return ((result[0] % mod * Identity[0][0] % mod) % mod + + (result[1] % mod * Identity[1][0] % mod) % mod) % + mod; } /** * Function to test above algorithm */ -void test() -{ - assert(fibo(6, 1000000007 ) == 8); +void test() { + assert(fibo(6, 1000000007) == 8); std::cout << "test case:1 passed\n"; - assert(fibo(5, 1000000007 ) == 5); + assert(fibo(5, 1000000007) == 5); std::cout << "test case:2 passed\n"; - assert(fibo(10 , 1000000007) == 55); + assert(fibo(10, 1000000007) == 55); std::cout << "test case:3 passed\n"; - assert(fibo(500 , 100) == 25); + assert(fibo(500, 100) == 25); std::cout << "test case:3 passed\n"; - assert(fibo(500 , 10000) == 4125); + assert(fibo(500, 10000) == 4125); std::cout << "test case:3 passed\n"; std::cout << "--All tests passed--\n"; } @@ -103,11 +105,12 @@ void test() /** * Main function */ -int main() -{ - test(); - uint64_t mod=1000000007; - std::cout<<"Enter the value of N: "; - uint64_t n=0; std::cin>>n; - std::cout<> n; + std::cout << n << "th Fibonacci number in modulo " << mod << ": " + << fibo(n, mod) << std::endl; } diff --git a/math/fibonacci_sum.cpp b/math/fibonacci_sum.cpp index 510757888..4c52951de 100644 --- a/math/fibonacci_sum.cpp +++ b/math/fibonacci_sum.cpp @@ -13,6 +13,7 @@ */ #include /// for assert +#include /// for integral typedefs #include /// for std::cin and std::cout #include /// for std::vector diff --git a/math/finding_number_of_digits_in_a_number.cpp b/math/finding_number_of_digits_in_a_number.cpp index 32ddd649e..e94a6c217 100644 --- a/math/finding_number_of_digits_in_a_number.cpp +++ b/math/finding_number_of_digits_in_a_number.cpp @@ -18,6 +18,7 @@ #include /// for assert #include /// for log calculation +#include /// for integral typedefs #include /// for IO operations /** diff --git a/math/integral_approximation.cpp b/math/integral_approximation.cpp index 7e72ef404..033742f63 100644 --- a/math/integral_approximation.cpp +++ b/math/integral_approximation.cpp @@ -1,18 +1,29 @@ /** * @file - * @brief Compute integral approximation of the function using [Riemann sum](https://en.wikipedia.org/wiki/Riemann_sum) - * @details In mathematics, a Riemann sum is a certain kind of approximation of an integral by a finite sum. It is named after nineteenth-century German mathematician Bernhard Riemann. - * One very common application is approximating the area of functions or lines on a graph and the length of curves and other approximations. - * The sum is calculated by partitioning the region into shapes (rectangles, trapezoids, parabolas, or cubics) that form a region similar to the region being measured, then calculating the area for each of these shapes, and finally adding all of these small areas together. - * This approach can be used to find a numerical approximation for a definite integral even if the fundamental theorem of calculus does not make it easy to find a closed-form solution. - * Because the region filled by the small shapes is usually not the same shape as the region being measured, the Riemann sum will differ from the area being measured. - * This error can be reduced by dividing up the region more finely, using smaller and smaller shapes. As the shapes get smaller and smaller, the sum approaches the Riemann integral. - * \author [Benjamin Walton](https://github.com/bwalton24) - * \author [Shiqi Sheng](https://github.com/shiqisheng00) + * @brief Compute integral approximation of the function using [Riemann + * sum](https://en.wikipedia.org/wiki/Riemann_sum) + * @details In mathematics, a Riemann sum is a certain kind of approximation of + * an integral by a finite sum. It is named after nineteenth-century German + * mathematician Bernhard Riemann. One very common application is approximating + * the area of functions or lines on a graph and the length of curves and other + * approximations. The sum is calculated by partitioning the region into shapes + * (rectangles, trapezoids, parabolas, or cubics) that form a region similar to + * the region being measured, then calculating the area for each of these + * shapes, and finally adding all of these small areas together. This approach + * can be used to find a numerical approximation for a definite integral even if + * the fundamental theorem of calculus does not make it easy to find a + * closed-form solution. Because the region filled by the small shapes is + * usually not the same shape as the region being measured, the Riemann sum will + * differ from the area being measured. This error can be reduced by dividing up + * the region more finely, using smaller and smaller shapes. As the shapes get + * smaller and smaller, the sum approaches the Riemann integral. \author + * [Benjamin Walton](https://github.com/bwalton24) \author [Shiqi + * Sheng](https://github.com/shiqisheng00) */ -#include /// for assert -#include /// for mathematical functions -#include /// for passing in functions +#include /// for assert +#include /// for mathematical functions +#include /// for integral typedefs +#include /// for passing in functions #include /// for IO operations /** diff --git a/math/inv_sqrt.cpp b/math/inv_sqrt.cpp index ef490ddf9..1f625e1cf 100644 --- a/math/inv_sqrt.cpp +++ b/math/inv_sqrt.cpp @@ -10,9 +10,9 @@ #include /// for assert #include /// for `std::sqrt` +#include /// for integral typedefs #include /// for IO operations #include /// for numeric_limits - /** * @brief This is the function that calculates the fast inverse square root. * The following code is the fast inverse square root implementation from diff --git a/math/largest_power.cpp b/math/largest_power.cpp index 8fffbbd07..22f6da738 100644 --- a/math/largest_power.cpp +++ b/math/largest_power.cpp @@ -1,41 +1,42 @@ /** * @file - * @brief Algorithm to find largest x such that p^x divides n! (factorial) using Legendre's Formula. - * @details Given an integer n and a prime number p, the task is to find the largest x such that - * p^x (p raised to power x) divides n! (factorial). This will be done using Legendre's formula: - * x = [n/(p^1)] + [n/(p^2)] + [n/(p^3)] + \ldots + 1 - * @see more on https://math.stackexchange.com/questions/141196/highest-power-of-a-prime-p-dividing-n + * @brief Algorithm to find largest x such that p^x divides n! (factorial) using + * Legendre's Formula. + * @details Given an integer n and a prime number p, the task is to find the + * largest x such that p^x (p raised to power x) divides n! (factorial). This + * will be done using Legendre's formula: x = [n/(p^1)] + [n/(p^2)] + [n/(p^3)] + * + \ldots + 1 + * @see more on + * https://math.stackexchange.com/questions/141196/highest-power-of-a-prime-p-dividing-n * @author [uday6670](https://github.com/uday6670) */ -#include /// for std::cin and std::cout -#include /// for assert - +#include /// for assert +#include /// for integral typedefs +#include /// for std::cin and std::cout /** * @namespace math * @brief Mathematical algorithms */ namespace math { - /** - * @brief Function to calculate largest power - * @param n number - * @param p prime number - * @returns largest power - */ - uint64_t largestPower(uint32_t n, const uint16_t& p) - { - // Initialize result - int x = 0; - - // Calculate result - while (n) - { - n /= p; - x += n; - } - return x; +/** + * @brief Function to calculate largest power + * @param n number + * @param p prime number + * @returns largest power + */ +uint64_t largestPower(uint32_t n, const uint16_t& p) { + // Initialize result + int x = 0; + + // Calculate result + while (n) { + n /= p; + x += n; } + return x; +} } // namespace math @@ -43,36 +44,34 @@ namespace math { * @brief Function for testing largestPower function. * test cases and assert statement. * @returns `void` -*/ -static void test() -{ - uint8_t test_case_1 = math::largestPower(5,2); - assert(test_case_1==3); - std::cout<<"Test 1 Passed!"< /// for assert +#include /// for integral typedefs #include /// for std::cin and std::cout #include /// for std::vector diff --git a/math/linear_recurrence_matrix.cpp b/math/linear_recurrence_matrix.cpp index 98c90e5f8..d59ad6aa1 100644 --- a/math/linear_recurrence_matrix.cpp +++ b/math/linear_recurrence_matrix.cpp @@ -18,6 +18,7 @@ * @author [Ashish Daulatabad](https://github.com/AshishYUO) */ #include /// for assert +#include /// for integral typedefs #include /// for IO operations #include /// for std::vector STL diff --git a/math/magic_number.cpp b/math/magic_number.cpp index 51232601b..7ba3ab0bc 100644 --- a/math/magic_number.cpp +++ b/math/magic_number.cpp @@ -17,6 +17,7 @@ * @author [Neha Hasija](https://github.com/neha-hasija17) */ #include /// for assert +#include /// for integral typedefs #include /// for io operations /** diff --git a/math/modular_division.cpp b/math/modular_division.cpp index 6fd984f2b..8208222f0 100644 --- a/math/modular_division.cpp +++ b/math/modular_division.cpp @@ -25,6 +25,7 @@ */ #include /// for assert +#include /// for integral typedefs #include /// for IO operations /** diff --git a/math/modular_exponentiation.cpp b/math/modular_exponentiation.cpp index 01410860f..a02293933 100644 --- a/math/modular_exponentiation.cpp +++ b/math/modular_exponentiation.cpp @@ -17,8 +17,8 @@ * @author [Shri2206](https://github.com/Shri2206) */ #include /// for assert +#include /// for integral typedefs #include /// for io operations - /** * @namespace math * @brief Mathematical algorithms diff --git a/math/modular_inverse_simple.cpp b/math/modular_inverse_simple.cpp index 813f0e0b4..b2c79fe9a 100644 --- a/math/modular_inverse_simple.cpp +++ b/math/modular_inverse_simple.cpp @@ -8,6 +8,7 @@ */ #include /// for assert +#include /// for integral typedefs #include /// for IO operations /** diff --git a/math/n_bonacci.cpp b/math/n_bonacci.cpp index c34dab7a1..601b2edf0 100644 --- a/math/n_bonacci.cpp +++ b/math/n_bonacci.cpp @@ -16,9 +16,9 @@ */ #include /// for assert +#include /// for integral typedefs #include /// for std::cout #include /// for std::vector - /** * @namespace math * @brief Mathematical algorithms diff --git a/math/n_choose_r.cpp b/math/n_choose_r.cpp index 8bb3bbcdc..d2aabc28a 100644 --- a/math/n_choose_r.cpp +++ b/math/n_choose_r.cpp @@ -11,8 +11,8 @@ */ #include /// for assert +#include /// for integral typedefs #include /// for io operations - /** * @namespace math * @brief Mathematical algorithms diff --git a/math/sieve_of_eratosthenes.cpp b/math/sieve_of_eratosthenes.cpp index e011b6c00..7b77b3474 100644 --- a/math/sieve_of_eratosthenes.cpp +++ b/math/sieve_of_eratosthenes.cpp @@ -12,6 +12,7 @@ */ #include +#include /// for integral typedefs #include #include @@ -21,7 +22,8 @@ * a prime p starting from p * p since all of the lower multiples * have been already eliminated. * @param N number of primes to check - * @return is_prime a vector of `N + 1` booleans identifying if `i`^th number is a prime or not + * @return is_prime a vector of `N + 1` booleans identifying if `i`^th number is + * a prime or not */ std::vector sieve(uint32_t N) { std::vector is_prime(N + 1, true); @@ -39,7 +41,8 @@ std::vector sieve(uint32_t N) { /** * This function prints out the primes to STDOUT * @param N number of primes to check - * @param is_prime a vector of `N + 1` booleans identifying if `i`^th number is a prime or not + * @param is_prime a vector of `N + 1` booleans identifying if `i`^th number is + * a prime or not */ void print(uint32_t N, const std::vector &is_prime) { for (uint32_t i = 2; i <= N; i++) { @@ -54,9 +57,11 @@ void print(uint32_t N, const std::vector &is_prime) { * Test implementations */ void tests() { - // 0 1 2 3 4 5 6 7 8 9 10 - std::vector ans{false, false, true, true, false, true, false, true, false, false, false}; - assert(sieve(10) == ans); + // 0 1 2 3 4 5 6 7 8 + // 9 10 + std::vector ans{false, false, true, true, false, true, + false, true, false, false, false}; + assert(sieve(10) == ans); } /** diff --git a/math/string_fibonacci.cpp b/math/string_fibonacci.cpp index eb9b6d7e1..a7ff4c8bb 100644 --- a/math/string_fibonacci.cpp +++ b/math/string_fibonacci.cpp @@ -8,6 +8,7 @@ * @see fibonacci_large.cpp, fibonacci_fast.cpp, fibonacci.cpp */ +#include /// for integral typedefs #include #ifdef _MSC_VER #include // use this for MS Visual C diff --git a/math/sum_of_binomial_coefficient.cpp b/math/sum_of_binomial_coefficient.cpp index 1942df527..38e02be21 100644 --- a/math/sum_of_binomial_coefficient.cpp +++ b/math/sum_of_binomial_coefficient.cpp @@ -10,6 +10,7 @@ * @author [muskan0719](https://github.com/muskan0719) */ #include /// for assert +#include /// for integral typedefs #include /// for std::cin and std::cout /**