# Algorithms_in_C++: math Namespace Reference

Loading...
Searching...
No Matches
math Namespace Reference

for std::rand More...

## Classes

struct | Point

## Functions

double | approximate_pi (const std::vector< Point > &pts)
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...
template<typename T >
T | square_perimeter (T length)
| perimeter of a square (4 * l) More...
template<typename T >
T | rect_perimeter (T length, T width)
| perimeter of a rectangle ( 2(l + w) ) More...
template<typename T >
T | triangle_perimeter (T base, T height, T hypotenuse)
| perimeter of a triangle (a + b + c) More...
template<typename T >
T | circle_perimeter (T radius)
| perimeter of a circle (2 * pi * r) More...
template<typename T >
T | parallelogram_perimeter (T base, T height)
| perimeter of a parallelogram 2(b + h) More...
template<typename T >
T | cube_surface_perimeter (T length)
| surface perimeter of a cube ( 12 More...
template<typename T >
T | n_polygon_surface_perimeter (T sides, T length)
| surface perimeter of a n-polygon ( n * l) More...
template<typename T >
T | cylinder_surface_perimeter (T radius, T height)
| surface perimeter of a cylinder (2 * radius + 2 * height) More...
int | power_of_two (int n)
| This function finds whether a number is power of 2 or not. 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...

## Detailed Description

for std::rand

for std::cin and std::cout

for io operations

Evaluate recurrence relation using matrix exponentiation.

for assert

Math algorithms.

for std::vector

for IO operations

for M_PI definition and pow()

for IO operations for std::vector

Mathematical algorithms

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

for std::cin and std::cout

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)

### Algorithm

This problem can be solved using matrix exponentiation method.

See also
  here for simple number exponentiation algorithm or explaination here.

Author
  Ashish Daulatabad for assert for IO operations for std::vector STL

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

for assert for M_PI definition and pow() for uint16_t datatype

Mathematical algorithms

for IO operations

Mathematical algorithms

for assert for std::pow for std::uint32_t

Mathematical algorithms

## Function Documentation

## approximate_pi()

double math::approximate_pi | ( | const std::vector< Point > & | pts | ) |

This function use the points pts (drawn at random) to return an estimate of the number π using the given points

Parameters

  pts | Each item of pts contains a point. A point is represented by a structure containing exactly two numbers, respectively x and y such that 0 ≤ x ≤ 1 and 0 ≤ y ≤ 1. pts always contains at least one item

Returns
  an estimate of the number π

{
/**
* This function use the points pts (drawn at random) to return an estimate of the number π using the given points
* @param pts Each item of pts contains a point. A point is represented by a structure containing exactly
* two numbers, respectively x and y such that 0 ≤ x ≤ 1 and 0 ≤ y ≤ 1.
* pts always contains at least one item
* @return an estimate of the number π
*/
{
int count =0; // Points in cercle
for(Point p:pts)
if(p.x * p.x + p.y*p.y <= 1)
++count;
return 4.0*count/pts.size();
}
}
math::Point
Definition: approximate_pi.cpp:30

## binomialCoeffSum()

uint64_t math::binomialCoeffSum | ( | uint64_t | n | ) |

Function to calculate sum of binomial coefficients

Parameters

  n | number

Returns
  Sum of binomial coefficients of number

{
// Calculating 2^n
return (1 << n);
}

## circle_area()

template<typename T >

T math::circle_area | ( | T | radius | ) |

area of a circle (pi

- r^2)
  Parameters

    radius | is the radius of the circle

  Returns
    area of the circle

{
return M_PI * pow(radius, 2);
}

## circle_perimeter()

template<typename T >

T math::circle_perimeter | ( | T | radius | ) |

perimeter of a circle (2 * pi * r)

Parameters

  radius | is the radius of the circle

Returns
  perimeter of the circle

{
return 2 * M_PI * radius;
}

## cone_volume()

template<typename T >

T math::cone_volume | ( | T | radius,
|  | T | height,
|  | double | PI = `3.14`
| ) |  |

The volume of a cone

Parameters

  radius | The radius of the base circle
  height | The height of the cone
  PI | The definition of the constant PI

Returns
  The volume of the cone

{
return std::pow(radius, 2) * PI * height / 3;
}
height
int height(node *root)
Definition: avltree.cpp:31
std::pow
T pow(T... args)
Here is the call graph for this function:

## cube_surface_area()

template<typename T >

T math::cube_surface_area | ( | T | length | ) |

surface area of a cube ( 6 * (l

- l))
  Parameters

    length | is the length of the cube

  Returns
    surface area of the cube

{
return 6 * length * length;
}

## cube_surface_perimeter()

template<typename T >

T math::cube_surface_perimeter | ( | T | length | ) |

surface perimeter of a cube ( 12

- l)
  Parameters

    length | is the length of the cube

  Returns
    surface perimeter of the cube

{
return 12 * length;
}

## cube_volume()

template<typename T >

T math::cube_volume | ( | T | length | ) |

The volume of a cube

Parameters

  length | The length of the cube

Returns
  The volume of the cube

{
return std::pow(length, 3);
}
Here is the call graph for this function:

## cylinder_surface_area()

template<typename T >

T math::cylinder_surface_area | ( | T | radius,
|  | T | height
| ) |  |

surface area of a cylinder (2 * pi * r * h + 2 * pi * r^2)

Parameters

  radius | is the radius of the cylinder
  height | is the height of the cylinder

Returns
  surface area of the cylinder

{
return 2 * M_PI * radius * height + 2 * M_PI * pow(radius, 2);
}
Here is the call graph for this function:

## cylinder_surface_perimeter()

template<typename T >

T math::cylinder_surface_perimeter | ( | T | radius,
|  | T | height
| ) |  |

surface perimeter of a cylinder (2 * radius + 2 * height)

Parameters

  radius | is the radius of the cylinder
  height | is the height of the cylinder

Returns
  surface perimeter of the cylinder

{
return (2 * radius) + (2 * height);
}
Here is the call graph for this function:

## cylinder_volume()

template<typename T >

T math::cylinder_volume | ( | T | radius,
|  | T | height,
|  | double | PI = `3.14`
| ) |  |

The volume of a cylinder

Parameters

  radius | The radius of the base circle
  height | The height of the cylinder
  PI | The definition of the constant PI

Returns
  The volume of the cylinder

{
return PI * std::pow(radius, 2) * height;
}
Here is the call graph for this function:

## integral_approx()

double math::integral_approx | ( | double | lb,
|  | double | ub,
|  | const std::function< double(double)> & | func,
|  | double | delta = `.0001`
| ) |  |

Computes integral approximation.

Parameters

  lb | lower bound
  ub | upper bound
  func | function passed in
  delta |

Returns
  integral approximation of function from [lb, ub]

{
double result = 0;
uint64_t numDeltas = static_cast<uint64_t>((ub - lb) / delta);
for (int i = 0; i < numDeltas; i++) {
double begin = lb + i * delta;
double end = lb + (i + 1) * delta;
result += delta * (func(begin) + func(end)) / 2;
}
return result;
}

## largestPower()

uint64_t math::largestPower | ( | uint32_t | n,
|  | const uint16_t & | p
| ) |  |

Function to calculate largest power.

Parameters

  n | number
  p | prime number

Returns
  largest power

{
// Initialize result
int x = 0;
// Calculate result
while (n)
{
n /= p;
x += n;
}
return x;
}

## lcmSum()

uint64_t math::lcmSum | ( | const uint16_t & | num | ) |

Function to compute sum of euler totients in sumOfEulerTotient vector

Parameters

  num | input number

Returns
  int Sum of LCMs, i.e. ∑LCM(i, num) from i = 1 to 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;
}
std::vector

## magic_number()

bool math::magic_number | ( | const uint64_t & | n | ) |

Function to check if the given number is magic number or not.

Parameters

  n | number to be checked.

Returns
  if number is a magic number, returns true, else false.

{
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;
}
}

## n_choose_r()

template<class T >

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} \)

Template Parameters

  T | Only for integer types such as long, int_64 etc

Parameters

  n | \( n \) in \( \binom{n}{r} \)
  r | \( r \) in \( \binom{n}{r} \)

Returns
  ans \( \binom{n}{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;
}

## n_polygon_surface_perimeter()

template<typename T >

T math::n_polygon_surface_perimeter | ( | T | sides,
|  | T | length
| ) |  |

surface perimeter of a n-polygon ( n * l)

Parameters

  length | is the length of the polygon
  sides | is the number of sides of the polygon

Returns
  surface perimeter of the polygon

{
return sides * length;
}

## parallelogram_area()

template<typename T >

T math::parallelogram_area | ( | T | base,
|  | T | height
| ) |  |

area of a parallelogram (b * h)

Parameters

  base | is the length of the bottom side of the parallelogram
  height | is the length of the tallest point in the parallelogram

Returns
  area of the parallelogram

{
return base * height;
}
Here is the call graph for this function:

## parallelogram_perimeter()

template<typename T >

T math::parallelogram_perimeter | ( | T | base,
|  | T | height
| ) |  |

perimeter of a parallelogram 2(b + h)

Parameters

  base | is the length of the bottom side of the parallelogram
  height | is the length of the tallest point in the parallelogram

Returns
  perimeter of the parallelogram

{
return 2 * (base + height);
}
Here is the call graph for this function:

## power()

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.

Parameters

  a | integer base
  b | unsigned integer exponent
  c | integer modulo

Returns
  a raised to power b modulo c

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

{
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;
}

## power_of_two()

int math::power_of_two | ( | int | n | ) |

This function finds whether a number is power of 2 or not.

Parameters

  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

Returns
  1 if `n` IS the power of 2
  0 if n is NOT a power of 2

result stores the bitwise and of n and n-1

{
/// result stores the
/// bitwise and of n and n-1
int result = n & (n - 1);
if (result == 0) {
return 1;
}
return 0;
}

## pyramid_volume()

template<typename T >

T math::pyramid_volume | ( | T | length,
|  | T | width,
|  | T | height
| ) |  |

The volume of a pyramid

Parameters

  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

Returns
  The volume of the pyramid

{
return length * width * height / 3;
}
Here is the call graph for this function:

## rect_area()

template<typename T >

T math::rect_area | ( | T | length,
|  | T | width
| ) |  |

area of a rectangle (l * w)

Parameters

  length | is the length of the rectangle
  width | is the width of the rectangle

Returns
  area of the rectangle

{
return length * width;
}

## rect_perimeter()

template<typename T >

T math::rect_perimeter | ( | T | length,
|  | T | width
| ) |  |

perimeter of a rectangle ( 2(l + w) )

Parameters

  length | is the length of the rectangle
  width | is the width of the rectangle

Returns
  perimeter of the rectangle

{
return 2 * (length + width);
}

## rect_prism_volume()

template<typename T >

T math::rect_prism_volume | ( | T | length,
|  | T | width,
|  | T | height
| ) |  |

The volume of a rectangular prism.

Parameters

  length | The length of the base rectangle
  width | The width of the base rectangle
  height | The height of the rectangular prism

Returns
  The volume of the rectangular prism

{
return length * width * height;
}
Here is the call graph for this function:

## sphere_surface_area()

template<typename T >

T math::sphere_surface_area | ( | T | radius | ) |

surface area of a sphere ( 4 * pi * r^2)

Parameters

  radius | is the radius of the sphere

Returns
  surface area of the sphere

{
return 4 * M_PI * pow(radius, 2);
}

## sphere_volume()

template<typename T >

T math::sphere_volume | ( | T | radius,
|  | double | PI = `3.14`
| ) |  |

The volume of a sphere

Parameters

  radius | The radius of the sphere
  PI | The definition of the constant PI

Returns
  The volume of the sphere

{
return PI * std::pow(radius, 3) * 4 / 3;
}
Here is the call graph for this function:

## square_area()

template<typename T >

T math::square_area | ( | T | length | ) |

area of a square (l * l)

Parameters

  length | is the length of the square

Returns
  area of square

{
return length * length;
}

## square_perimeter()

template<typename T >

T math::square_perimeter | ( | T | length | ) |

perimeter of a square (4 * l)

Parameters

  length | is the length of the square

Returns
  perimeter of square

{
return 4 * length;
}

## test_eval()

void math::test_eval | ( | double | approx,
|  | double | expected,
|  | double | threshold
| ) |  |

Wrapper to evaluate if the approximated value is within `.XX%` threshold of the exact value.

Parameters

  approx | aprroximate value
  exact | expected value
  threshold | values from [0, 1)

{
assert(approx >= expected * (1 - threshold));
assert(approx <= expected * (1 + threshold));
}

## triangle_area()

template<typename T >

T math::triangle_area | ( | T | base,
|  | T | height
| ) |  |

area of a triangle (b * h / 2)

Parameters

  base | is the length of the bottom side of the triangle
  height | is the length of the tallest point in the triangle

Returns
  area of the triangle

{
return base * height / 2;
}
Here is the call graph for this function:

## triangle_perimeter()

template<typename T >

T math::triangle_perimeter | ( | T | base,
|  | T | height,
|  | T | hypotenuse
| ) |  |

perimeter of a triangle (a + b + c)

Parameters

  base | is the length of the bottom side of the triangle
  height | is the length of the tallest point in the triangle

Returns
  perimeter of the triangle

{
return base + height + hypotenuse;
}
Here is the call graph for this function:

## triangle_prism_volume()

template<typename T >

T math::triangle_prism_volume | ( | T | base,
|  | T | height,
|  | T | depth
| ) |  |

The volume of a triangular prism.

Parameters

  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)

Returns
  The volume of the triangular prism

{
return base * height * depth / 2;
}
Here is the call graph for this function: