mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-23 20:24:25 +08:00
Merge branch 'master' into fix-1323
This commit is contained in:
138
math/fibonacci_sum.cpp
Normal file
138
math/fibonacci_sum.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief An algorithm to calculate the sum of [Fibonacci
|
||||
* Sequence](https://en.wikipedia.org/wiki/Fibonacci_number): \f$\mathrm{F}(n) +
|
||||
* \mathrm{F}(n+1) + .. + \mathrm{F}(m)\f$
|
||||
* @details An algorithm to calculate the sum of Fibonacci Sequence:
|
||||
* \f$\mathrm{F}(n) + \mathrm{F}(n+1) + .. + \mathrm{F}(m)\f$ where
|
||||
* \f$\mathrm{F}(i)\f$ denotes the i-th Fibonacci Number . Note that F(0) = 0
|
||||
* and F(1) = 1. The value of the sum is calculated using matrix exponentiation.
|
||||
* Reference source:
|
||||
* https://stackoverflow.com/questions/4357223/finding-the-sum-of-fibonacci-numbers
|
||||
* @author [Sarthak Sahu](https://github.com/SarthakSahu1009)
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for std::cin and std::cout
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
/**
|
||||
* @namespace math
|
||||
* @brief Mathematical algorithms
|
||||
*/
|
||||
namespace math {
|
||||
/**
|
||||
* @namespace fibonacci_sum
|
||||
* @brief Functions for the sum of the Fibonacci Sequence: \f$\mathrm{F}(n) +
|
||||
* \mathrm{F}(n+1) + .. + \mathrm{F}(m)\f$
|
||||
*/
|
||||
namespace fibonacci_sum {
|
||||
using matrix = std::vector<std::vector<uint64_t> >;
|
||||
|
||||
/**
|
||||
* Function to multiply two matrices
|
||||
* @param T matrix 1
|
||||
* @param A martix 2
|
||||
* @returns resultant matrix
|
||||
*/
|
||||
math::fibonacci_sum::matrix multiply(const math::fibonacci_sum::matrix &T, const math::fibonacci_sum::matrix &A) {
|
||||
math::fibonacci_sum::matrix result(2, std::vector<uint64_t>(2, 0));
|
||||
|
||||
// multiplying matrices
|
||||
result[0][0] = T[0][0]*A[0][0] + T[0][1]*A[1][0];
|
||||
result[0][1] = T[0][0]*A[0][1] + T[0][1]*A[1][1];
|
||||
result[1][0] = T[1][0]*A[0][0] + T[1][1]*A[1][0];
|
||||
result[1][1] = T[1][0]*A[0][1] + T[1][1]*A[1][1];
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to compute A^n where A is a matrix.
|
||||
* @param T matrix
|
||||
* @param ex power
|
||||
* @returns resultant matrix
|
||||
*/
|
||||
math::fibonacci_sum::matrix power(math::fibonacci_sum::matrix T, uint64_t ex) {
|
||||
math::fibonacci_sum::matrix A{{1, 1}, {1, 0}};
|
||||
if (ex == 0 || ex == 1) {
|
||||
return T;
|
||||
}
|
||||
|
||||
T = power(T, ex / 2);
|
||||
T = multiply(T, T);
|
||||
if (ex & 1) {
|
||||
T = multiply(T, A);
|
||||
}
|
||||
return T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to compute sum of fibonacci sequence from 0 to n.
|
||||
* @param n number
|
||||
* @returns uint64_t ans, the sum of sequence
|
||||
*/
|
||||
uint64_t result(uint64_t n) {
|
||||
math::fibonacci_sum::matrix T{{1, 1}, {1, 0}};
|
||||
T = power(T, n);
|
||||
uint64_t ans = T[0][1];
|
||||
ans = (ans - 1);
|
||||
return ans;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to compute sum of fibonacci sequence from n to m.
|
||||
* @param n start of sequence
|
||||
* @param m end of sequence
|
||||
* @returns uint64_t the sum of sequence
|
||||
*/
|
||||
uint64_t fiboSum(uint64_t n, uint64_t m) {
|
||||
return (result(m + 2) - result(n + 1));
|
||||
}
|
||||
} // namespace fibonacci_sum
|
||||
} // namespace math
|
||||
|
||||
/**
|
||||
* Function for testing fiboSum function.
|
||||
* test cases and assert statement.
|
||||
* @returns `void`
|
||||
*/
|
||||
static void test() {
|
||||
uint64_t n = 0, m = 3;
|
||||
uint64_t test_1 = math::fibonacci_sum::fiboSum(n, m);
|
||||
assert(test_1 == 4);
|
||||
std::cout << "Passed Test 1!" << std::endl;
|
||||
|
||||
n = 3;
|
||||
m = 5;
|
||||
uint64_t test_2 = math::fibonacci_sum::fiboSum(n, m);
|
||||
assert(test_2 == 10);
|
||||
std::cout << "Passed Test 2!" << std::endl;
|
||||
|
||||
n = 5;
|
||||
m = 7;
|
||||
uint64_t test_3 = math::fibonacci_sum::fiboSum(n, m);
|
||||
assert(test_3 == 26);
|
||||
std::cout << "Passed Test 3!" << std::endl;
|
||||
|
||||
n = 7;
|
||||
m = 10;
|
||||
uint64_t test_4 = math::fibonacci_sum::fiboSum(n, m);
|
||||
assert(test_4 == 123);
|
||||
std::cout << "Passed Test 4!" << std::endl;
|
||||
|
||||
n = 9;
|
||||
m = 12;
|
||||
uint64_t test_5 = math::fibonacci_sum::fiboSum(n, m);
|
||||
assert(test_5 == 322);
|
||||
std::cout << "Passed Test 5!" << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
test(); // execute the tests
|
||||
return 0;
|
||||
}
|
||||
99
math/lcm_sum.cpp
Normal file
99
math/lcm_sum.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief An algorithm to calculate the sum of LCM: \f$\mathrm{LCM}(1,n) + \mathrm{LCM}(2,n) + \ldots + \mathrm{LCM}(n,n)\f$
|
||||
* @details An algorithm to calculate the sum of LCM: \f$\mathrm{LCM}(1,n) + \mathrm{LCM}(2,n) + \ldots + \mathrm{LCM}(n,n)\f$ where \f$\mathrm{LCM}(i,n)\f$
|
||||
* denotes the Least Common Multiple of the integers i and n. For n greater than or equal to 1.
|
||||
* The value of the sum is calculated by formula:
|
||||
* \f[
|
||||
* \sum\mathrm{LCM}(i, n) = \frac{1}{2} \left[\left(\sum (d * \mathrm{ETF}(d)) + 1\right) * n\right]
|
||||
* \f]
|
||||
* where \mathrm{ETF}(i) represents Euler totient function of i.
|
||||
* @author [Chesta Mittal](https://github.com/chestamittal)
|
||||
*/
|
||||
|
||||
#include <iostream> /// for std::cin and std::cout
|
||||
#include <cassert> /// for assert
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
/**
|
||||
* @namespace math
|
||||
* @brief Mathematical algorithms
|
||||
*/
|
||||
namespace math {
|
||||
/**
|
||||
* Function to compute sum of euler totients in sumOfEulerTotient vector
|
||||
* @param num input number
|
||||
* @returns int Sum of LCMs, i.e. ∑LCM(i, num) from i = 1 to num
|
||||
*/
|
||||
uint64_t lcmSum(const uint16_t& num) {
|
||||
|
||||
uint64_t i=0, j=0;
|
||||
std::vector <uint64_t> eulerTotient(num+1);
|
||||
std::vector <uint64_t> sumOfEulerTotient(num+1);
|
||||
|
||||
// storing initial values in eulerTotient vector
|
||||
for(i=1; i<=num; i++) {
|
||||
eulerTotient[i] = i;
|
||||
}
|
||||
|
||||
// applying totient sieve
|
||||
for(i=2; i<=num; i++) {
|
||||
if(eulerTotient[i] == i) {
|
||||
for(j=i; j<=num; j+=i) {
|
||||
eulerTotient[j] = eulerTotient[j]/i;
|
||||
eulerTotient[j] = eulerTotient[j]*(i-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// computing sum of euler totients
|
||||
for(i=1; i<=num; i++) {
|
||||
for(j=i; j <=num; j+=i) {
|
||||
sumOfEulerTotient[j] += eulerTotient[i]*i;
|
||||
}
|
||||
}
|
||||
|
||||
return ((sumOfEulerTotient[num] + 1 ) * num) / 2;
|
||||
}
|
||||
} // namespace math
|
||||
|
||||
/**
|
||||
* Function for testing lcmSum function.
|
||||
* test cases and assert statement.
|
||||
* @returns `void`
|
||||
*/
|
||||
static void test() {
|
||||
uint64_t n = 2;
|
||||
uint64_t test_1 = math::lcmSum(n);
|
||||
assert(test_1 == 4);
|
||||
std::cout << "Passed Test 1!" << std::endl;
|
||||
|
||||
n = 5;
|
||||
uint64_t test_2 = math::lcmSum(n);
|
||||
assert(test_2 == 55);
|
||||
std::cout << "Passed Test 2!" << std::endl;
|
||||
|
||||
n = 10;
|
||||
uint64_t test_3 = math::lcmSum(n);
|
||||
assert(test_3 == 320);
|
||||
std::cout << "Passed Test 3!" << std::endl;
|
||||
|
||||
n = 11;
|
||||
uint64_t test_4 = math::lcmSum(n);
|
||||
assert(test_4 == 616);
|
||||
std::cout << "Passed Test 4!" << std::endl;
|
||||
|
||||
n = 15;
|
||||
uint64_t test_5 = math::lcmSum(n);
|
||||
assert(test_5 == 1110);
|
||||
std::cout << "Passed Test 5!" << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
test(); // execute the tests
|
||||
return 0;
|
||||
}
|
||||
80
math/magic_number.cpp
Normal file
80
math/magic_number.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief A simple program to check if the given number is a magic number or
|
||||
* not. A number is said to be a magic number, if the sum of its digits are
|
||||
* calculated till a single digit recursively by adding the sum of the digits
|
||||
* after every addition. If the single digit comes out to be 1,then the number
|
||||
* is a magic number.
|
||||
*
|
||||
* This is a shortcut method to verify Magic Number.
|
||||
* On dividing the input by 9, if the remainder is 1 then the number is a magic
|
||||
* number else not. The divisibility rule of 9 says that a number is divisible
|
||||
* by 9 if the sum of its digits are also divisible by 9. Therefore, if a number
|
||||
* is divisible by 9, then, recursively, all the digit sums are also divisible
|
||||
* by 9. The final digit sum is always 9. An increase of 1 in the original
|
||||
* number will increase the ultimate value by 1, making it 10 and the ultimate
|
||||
* sum will be 1, thus verifying that it is a magic number.
|
||||
* @author [Neha Hasija](https://github.com/neha-hasija17)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for io operations
|
||||
|
||||
/**
|
||||
* @namespace math
|
||||
* @brief Mathematical algorithms
|
||||
*/
|
||||
namespace math {
|
||||
/**
|
||||
* Function to check if the given number is magic number or not.
|
||||
* @param n number to be checked.
|
||||
* @return if number is a magic number, returns true, else false.
|
||||
*/
|
||||
bool magic_number(const uint64_t &n) {
|
||||
if (n <= 0) {
|
||||
return false;
|
||||
}
|
||||
// result stores the modulus of @param n with 9
|
||||
uint64_t result = n % 9;
|
||||
// if result is 1 then the number is a magic number else not
|
||||
if (result == 1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} // namespace math
|
||||
|
||||
/**
|
||||
* @brief Test function
|
||||
* @returns void
|
||||
*/
|
||||
static void tests() {
|
||||
std::cout << "Test 1:\t n=60\n";
|
||||
assert(math::magic_number(60) == false);
|
||||
std::cout << "passed\n";
|
||||
|
||||
std::cout << "Test 2:\t n=730\n";
|
||||
assert(math::magic_number(730) == true);
|
||||
std::cout << "passed\n";
|
||||
|
||||
std::cout << "Test 3:\t n=0\n";
|
||||
assert(math::magic_number(0) == false);
|
||||
std::cout << "passed\n";
|
||||
|
||||
std::cout << "Test 4:\t n=479001600\n";
|
||||
assert(math::magic_number(479001600) == false);
|
||||
std::cout << "passed\n";
|
||||
|
||||
std::cout << "Test 5:\t n=-35\n";
|
||||
assert(math::magic_number(-35) == false);
|
||||
std::cout << "passed\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
tests(); // execute the tests
|
||||
return 0;
|
||||
}
|
||||
89
math/modular_exponentiation.cpp
Normal file
89
math/modular_exponentiation.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief C++ Program for Modular Exponentiation Iteratively.
|
||||
* @details The task is to calculate the value of an integer a raised to an
|
||||
* integer exponent b under modulo c.
|
||||
* @note The time complexity of this approach is O(log b).
|
||||
*
|
||||
* Example:
|
||||
* (4^3) % 5 (where ^ stands for exponentiation and % for modulo)
|
||||
* (4*4*4) % 5
|
||||
* (4 % 5) * ( (4*4) % 5 )
|
||||
* 4 * (16 % 5)
|
||||
* 4 * 1
|
||||
* 4
|
||||
* We can also verify the result as 4^3 is 64 and 64 modulo 5 is 4
|
||||
*
|
||||
* @author [Shri2206](https://github.com/Shri2206)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for io operations
|
||||
|
||||
/**
|
||||
* @namespace math
|
||||
* @brief Mathematical algorithms
|
||||
*/
|
||||
namespace math {
|
||||
/**
|
||||
* @brief This function calculates a raised to exponent b under modulo c using
|
||||
* modular exponentiation.
|
||||
* @param a integer base
|
||||
* @param b unsigned integer exponent
|
||||
* @param c integer modulo
|
||||
* @return a raised to power b modulo c
|
||||
*/
|
||||
uint64_t power(uint64_t a, uint64_t b, uint64_t c) {
|
||||
uint64_t ans = 1; /// Initialize the answer to be returned
|
||||
a = a % c; /// Update a if it is more than or equal to c
|
||||
if (a == 0) {
|
||||
return 0; /// In case a is divisible by c;
|
||||
}
|
||||
while (b > 0) {
|
||||
/// If b is odd, multiply a with answer
|
||||
if (b & 1) {
|
||||
ans = ((ans % c) * (a % c)) % c;
|
||||
}
|
||||
/// b must be even now
|
||||
b = b >> 1; /// b = b/2
|
||||
a = ((a % c) * (a % c)) % c;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
} // namespace math
|
||||
|
||||
/**
|
||||
* Function for testing power function.
|
||||
* test cases and assert statement.
|
||||
* @returns `void`
|
||||
*/
|
||||
static void test() {
|
||||
uint32_t test_case_1 = math::power(2, 5, 13);
|
||||
assert(test_case_1 == 6);
|
||||
std::cout << "Test 1 Passed!" << std::endl;
|
||||
|
||||
uint32_t test_case_2 = math::power(14, 7, 15);
|
||||
assert(test_case_2 == 14);
|
||||
std::cout << "Test 2 Passed!" << std::endl;
|
||||
|
||||
uint64_t test_case_3 = math::power(8, 15, 41);
|
||||
assert(test_case_3 == 32);
|
||||
std::cout << "Test 3 Passed!" << std::endl;
|
||||
|
||||
uint64_t test_case_4 = math::power(27, 2, 5);
|
||||
assert(test_case_4 == 4);
|
||||
std::cout << "Test 4 Passed!" << std::endl;
|
||||
|
||||
uint16_t test_case_5 = math::power(7, 3, 6);
|
||||
assert(test_case_5 == 1);
|
||||
std::cout << "Test 5 Passed!" << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
test(); // execute the tests
|
||||
return 0;
|
||||
}
|
||||
81
math/n_choose_r.cpp
Normal file
81
math/n_choose_r.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief [Combinations](https://en.wikipedia.org/wiki/Combination) n choose r function implementation
|
||||
* @details
|
||||
* A very basic and efficient method of calculating
|
||||
* choosing r from n different choices.
|
||||
* \f$ \binom{n}{r} = \frac{n!}{r! (n-r)!} \f$
|
||||
*
|
||||
* @author [Tajmeet Singh](https://github.com/tjgurwara99)
|
||||
*/
|
||||
|
||||
#include <iostream> /// for io operations
|
||||
#include <cassert> /// for assert
|
||||
|
||||
/**
|
||||
* @namespace math
|
||||
* @brief Mathematical algorithms
|
||||
*/
|
||||
namespace math {
|
||||
/**
|
||||
* @brief This is the function implementation of \f$ \binom{n}{r} \f$
|
||||
* @details
|
||||
* We are calculating the ans with iterations
|
||||
* instead of calculating three different factorials.
|
||||
* Also, we are using the fact that
|
||||
* \f$ \frac{n!}{r! (n-r)!} = \frac{(n - r + 1) \times \cdots \times n}{1 \times \cdots \times r} \f$
|
||||
* @tparam T Only for integer types such as long, int_64 etc
|
||||
* @param n \f$ n \f$ in \f$ \binom{n}{r} \f$
|
||||
* @param r \f$ r \f$ in \f$ \binom{n}{r} \f$
|
||||
* @returns ans \f$ \binom{n}{r} \f$
|
||||
*/
|
||||
template <class T>
|
||||
T n_choose_r(T n, T r) {
|
||||
if(r > n / 2)
|
||||
r = n - r; // Because of the fact that nCr(n, r) == nCr(n, n - r)
|
||||
T ans = 1;
|
||||
for(int i = 1; i <= r; i++) {
|
||||
ans *= n - r + i;
|
||||
ans /= i;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
} // namespace math
|
||||
|
||||
/**
|
||||
* @brief Test implementations
|
||||
* @returns void
|
||||
*/
|
||||
static void test() {
|
||||
// First test on 5 choose 2
|
||||
uint8_t t = math::n_choose_r(5, 2);
|
||||
assert(((void)"10 is the answer but function says otherwise.\n",
|
||||
t == 10));
|
||||
std::cout << "First test passes." << std::endl;
|
||||
// Second test on 5 choose 3
|
||||
t = math::n_choose_r(5, 3);
|
||||
assert(((void)"10 is the answer but the function says otherwise.\n",
|
||||
t == 10));
|
||||
std::cout << "Second test passes." << std::endl;
|
||||
// Third test on 3 choose 2
|
||||
t = math::n_choose_r(3, 2);
|
||||
assert(((void)"3 is the answer but the function says otherwise.\n",
|
||||
t == 3));
|
||||
std::cout << "Third test passes." << std::endl;
|
||||
// Fourth test on 10 choose 4
|
||||
t = math::n_choose_r(10, 4);
|
||||
assert(((void)"210 is the answer but the function says otherwise.\n",
|
||||
t == 210));
|
||||
std::cout << "Fourth test passes." << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @param argc commandline argument count (ignored)
|
||||
* @param argv commandline array of arguments (ignored)
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main(int argc, char *argv[]) {
|
||||
test(); // executing tests
|
||||
return 0;
|
||||
}
|
||||
67
math/sum_of_binomial_coefficient.cpp
Normal file
67
math/sum_of_binomial_coefficient.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Algorithm to find sum of binomial coefficients of a given positive integer.
|
||||
* @details Given a positive integer n, the task is to find the sum of binomial coefficient i.e
|
||||
* nC0 + nC1 + nC2 + ... + nCn-1 + nCn
|
||||
* By induction, we can prove that the sum is equal to 2^n
|
||||
* @see more on https://en.wikipedia.org/wiki/Binomial_coefficient#Sums_of_the_binomial_coefficients
|
||||
* @author [muskan0719](https://github.com/muskan0719)
|
||||
*/
|
||||
#include <iostream> /// for std::cin and std::cout
|
||||
#include <cassert> /// for assert
|
||||
|
||||
/**
|
||||
* @namespace math
|
||||
* @brief Mathematical algorithms
|
||||
*/
|
||||
namespace math {
|
||||
|
||||
/**
|
||||
* Function to calculate sum of binomial coefficients
|
||||
* @param n number
|
||||
* @return Sum of binomial coefficients of number
|
||||
*/
|
||||
uint64_t binomialCoeffSum(uint64_t n)
|
||||
{
|
||||
// Calculating 2^n
|
||||
return (1 << n);
|
||||
}
|
||||
} // namespace math
|
||||
|
||||
/**
|
||||
* Function for testing binomialCoeffSum function.
|
||||
* test cases and assert statement.
|
||||
* @returns `void`
|
||||
*/
|
||||
static void test()
|
||||
{
|
||||
int test_case_1 = math::binomialCoeffSum(2);
|
||||
assert(test_case_1==4);
|
||||
std::cout<<"Test_case_1 Passed!"<<std::endl;
|
||||
|
||||
int test_case_2 = math::binomialCoeffSum(3);
|
||||
assert(test_case_2==8);
|
||||
std::cout<<"Test_case_2 Passed!"<<std::endl;
|
||||
|
||||
int test_case_3 = math::binomialCoeffSum(4);
|
||||
assert(test_case_3==16);
|
||||
std::cout<<"Test_case_3 Passed!"<<std::endl;
|
||||
|
||||
int test_case_4 = math::binomialCoeffSum(5);
|
||||
assert(test_case_4==32);
|
||||
std::cout<<"Test_case_4 Passed!"<<std::endl;
|
||||
|
||||
int test_case_5 = math::binomialCoeffSum(7);
|
||||
assert(test_case_5==128);
|
||||
std::cout<<"Test_case_5 Passed!"<<std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
test(); // execute the tests
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user