|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
for IO operations More...
Functions | |
| double | integral_approx (double lb, double ub, const std::function< double(double)> &func, double delta=.0001) |
| Computes integral approximation. More... | |
| void | test_eval (double approx, double expected, double threshold) |
Wrapper to evaluate if the approximated value is within .XX% threshold of the exact value. More... | |
| uint64_t | largestPower (uint32_t n, const uint16_t &p) |
| Function to calculate largest power. More... | |
| uint64_t | lcmSum (const uint16_t &num) |
| bool | magic_number (const uint64_t &n) |
| uint64_t | power (uint64_t a, uint64_t b, uint64_t c) |
| This function calculates a raised to exponent b under modulo c using modular exponentiation. More... | |
| template<class T > | |
| T | n_choose_r (T n, T r) |
| This is the function implementation of \( \binom{n}{r} \). More... | |
| void | power_of_two (int n) |
| Function to test above algorithm. More... | |
| uint64_t | binomialCoeffSum (uint64_t n) |
for IO operations
Math algorithms.
for std::cout
for io operations
Evaluate recurrence relation using matrix exponentiation.
for assert
for std::vector
for assert for int32_t type for atoi
Mathematical algorithms
for assert for std::cin and std::cout
Mathematical algorithms
for assert for mathematical functions for passing in functions
Mathematical functions
Mathematical algorithms
Given a recurrence relation; evaluate the value of nth term. For e.g., For fibonacci series, recurrence series is f(n) = f(n-1) + f(n-2) where f(0) = 0 and f(1) = 1. Note that the method used only demonstrates recurrence relation with one variable (n), unlike nCr problem, since it has two (n, r)
This problem can be solved using matrix exponentiation method.
Mathematical algorithms
for assert
Mathematical algorithms
for std::is_equal, std::swap for assert for IO operations
Mathematical algorithms
for assert for io operations
Mathematical algorithms
Mathematical algorithms
| uint64_t math::binomialCoeffSum | ( | uint64_t | n | ) |
| double math::integral_approx | ( | double | lb, |
| double | ub, | ||
| const std::function< double(double)> & | func, | ||
| double | delta = .0001 |
||
| ) |
Computes integral approximation.
| lb | lower bound |
| ub | upper bound |
| func | function passed in |
| delta |
| uint64_t math::largestPower | ( | uint32_t | n, |
| const uint16_t & | p | ||
| ) |
| uint64_t math::lcmSum | ( | const uint16_t & | num | ) |
Function to compute sum of euler totients in sumOfEulerTotient vector
| num | input number |
| bool math::magic_number | ( | const uint64_t & | n | ) |
Function to check if the given number is magic number or not.
| n | number to be checked. |
| T math::n_choose_r | ( | T | n, |
| T | r | ||
| ) |
This is the function implementation of \( \binom{n}{r} \).
We are calculating the ans with iterations instead of calculating three different factorials. Also, we are using the fact that \( \frac{n!}{r! (n-r)!} = \frac{(n - r + 1) \times \cdots \times n}{1 \times \cdots \times r} \)
| T | Only for integer types such as long, int_64 etc |
| n | \( n \) in \( \binom{n}{r} \) |
| r | \( r \) in \( \binom{n}{r} \) |
| uint64_t math::power | ( | uint64_t | a, |
| uint64_t | b, | ||
| uint64_t | c | ||
| ) |
This function calculates a raised to exponent b under modulo c using modular exponentiation.
| a | integer base |
| b | unsigned integer exponent |
| c | integer modulo |
Initialize the answer to be returned
Update a if it is more than or equal to c
In case a is divisible by c;
If b is odd, multiply a with answer
b must be even now
b = b/2
| void math::power_of_two | ( | int | n | ) |
Function to test above algorithm.
| n | description |
This function finds whether a number is power of 2 or not
| n | value for which we want to check prints the result, as "Yes, the number n is a power of 2" or "No, the number is not a power of 2" without quotes |
result stores the bitwise and of n and n-1
| void math::test_eval | ( | double | approx, |
| double | expected, | ||
| double | threshold | ||
| ) |