|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
for M_PI definition and pow() More...
Functions | |
| template<typename T > | |
| T | square_area (T length) |
| area of a square (l * l) More... | |
| template<typename T > | |
| T | rect_area (T length, T width) |
| area of a rectangle (l * w) More... | |
| template<typename T > | |
| T | triangle_area (T base, T height) |
| area of a triangle (b * h / 2) More... | |
| template<typename T > | |
| T | circle_area (T radius) |
| area of a circle (pi More... | |
| template<typename T > | |
| T | parallelogram_area (T base, T height) |
| area of a parallelogram (b * h) More... | |
| template<typename T > | |
| T | cube_surface_area (T length) |
| surface area of a cube ( 6 * (l More... | |
| template<typename T > | |
| T | sphere_surface_area (T radius) |
| surface area of a sphere ( 4 * pi * r^2) More... | |
| template<typename T > | |
| T | cylinder_surface_area (T radius, T height) |
| surface area of a cylinder (2 * pi * r * h + 2 * pi * r^2) More... | |
| 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) |
| template<typename T > | |
| T | cube_volume (T length) |
| The volume of a cube More... | |
| template<typename T > | |
| T | rect_prism_volume (T length, T width, T height) |
| The volume of a rectangular prism. More... | |
| template<typename T > | |
| T | cone_volume (T radius, T height, double PI=3.14) |
| The volume of a cone More... | |
| template<typename T > | |
| T | triangle_prism_volume (T base, T height, T depth) |
| The volume of a triangular prism. More... | |
| template<typename T > | |
| T | pyramid_volume (T length, T width, T height) |
| The volume of a pyramid More... | |
| template<typename T > | |
| T | sphere_volume (T radius, double PI=3.14) |
| The volume of a sphere More... | |
| template<typename T > | |
| T | cylinder_volume (T radius, T height, double PI=3.14) |
| The volume of a cylinder More... | |
for M_PI definition and pow()
for std::cout
for io operations
Evaluate recurrence relation using matrix exponentiation.
for assert
Math algorithms.
for std::vector
for IO operations
for assert for uint16_t datatype for IO operations
Mathematical algorithms
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
for math functions for fixed size data types for time to initialize rng for function pointers for std::cout for random number generation for std::vector
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
for assert for std::pow for std::uint32_t
Mathematical algorithms
| uint64_t math::binomialCoeffSum | ( | uint64_t | n | ) |
| T math::circle_area | ( | T | radius | ) |
area of a circle (pi
| radius | is the radius of the circle |
| T math::cone_volume | ( | T | radius, |
| T | height, | ||
| double | PI = 3.14 |
||
| ) |
The volume of a cone
| radius | The radius of the base circle |
| height | The height of the cone |
| PI | The definition of the constant PI |
| T math::cube_surface_area | ( | T | length | ) |
surface area of a cube ( 6 * (l
| length | is the length of the cube |
| T math::cube_volume | ( | T | length | ) |
| T math::cylinder_surface_area | ( | T | radius, |
| T | height | ||
| ) |
| T math::cylinder_volume | ( | T | radius, |
| T | height, | ||
| double | PI = 3.14 |
||
| ) |
| 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} \) |
| T math::parallelogram_area | ( | T | base, |
| T | height | ||
| ) |
area of a parallelogram (b * h)
| base | is the length of the bottom side of the parallelogram |
| height | is the length of the tallest point in the parallelogram |
| 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
| T math::pyramid_volume | ( | T | length, |
| T | width, | ||
| T | height | ||
| ) |
The volume of a pyramid
| length | The length of the base shape (or base for triangles) |
| width | The width of the base shape (or height for triangles) |
| height | The height of the pyramid |
| T math::rect_area | ( | T | length, |
| T | width | ||
| ) |
area of a rectangle (l * w)
| length | is the length of the rectangle |
| width | is the width of the rectangle |
| T math::rect_prism_volume | ( | T | length, |
| T | width, | ||
| T | height | ||
| ) |
The volume of a rectangular prism.
| length | The length of the base rectangle |
| width | The width of the base rectangle |
| height | The height of the rectangular prism |
| T math::sphere_surface_area | ( | T | radius | ) |
surface area of a sphere ( 4 * pi * r^2)
| radius | is the radius of the sphere |
| T math::sphere_volume | ( | T | radius, |
| double | PI = 3.14 |
||
| ) |
| T math::square_area | ( | T | length | ) |
| void math::test_eval | ( | double | approx, |
| double | expected, | ||
| double | threshold | ||
| ) |
| T math::triangle_area | ( | T | base, |
| T | height | ||
| ) |
| T math::triangle_prism_volume | ( | T | base, |
| T | height, | ||
| T | depth | ||
| ) |
The volume of a triangular prism.
| base | The length of the base triangle |
| height | The height of the base triangles |
| depth | The depth of the triangular prism (the height of the whole prism) |