Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
math Namespace Reference

for IO operations More...

Typedefs

using Point
 structure of points containing two numbers, x and y, such that 0 ≤ x ≤ 1 and 0 ≤ y ≤ 1.
 

Functions

uint64_t aliquot_sum (const uint64_t num)
 to return the aliquot sum of a number
 
double approximate_pi (const std::vector< Point > &pts)
 This function uses the points in a given vector 'pts' (drawn at random) to return an approximation of the number π.
 
template<typename T >
square_area (T length)
 area of a square (l * l)
 
template<typename T >
rect_area (T length, T width)
 area of a rectangle (l * w)
 
template<typename T >
triangle_area (T base, T height)
 area of a triangle (b * h / 2)
 
template<typename T >
circle_area (T radius)
 area of a circle (pi
 
template<typename T >
parallelogram_area (T base, T height)
 area of a parallelogram (b * h)
 
template<typename T >
cube_surface_area (T length)
 surface area of a cube ( 6 * (l
 
template<typename T >
sphere_surface_area (T radius)
 surface area of a sphere ( 4 * pi * r^2)
 
template<typename T >
cylinder_surface_area (T radius, T height)
 surface area of a cylinder (2 * pi * r * h + 2 * pi * r^2)
 
int sum_of_divisor (int num)
 Function to calculate the sum of all the proper divisor of an integer.
 
bool are_amicable (int x, int y)
 Function to check whether the pair is amicable or not.
 
bool is_factorial (uint64_t n)
 Function to check if the given number is factorial of some number or not.
 
bool is_prime (int64_t num)
 Function to check if the given number is prime or not.
 
void sieve (std::vector< bool > *vec)
 Performs the sieve.
 
void print_primes (std::vector< bool > const &primes)
 Prints all the indexes of true values in the passed std::vector.
 
uint64_t phiFunction (uint64_t n)
 Function to calculate Euler's Totient.
 
uint64_t factorial (uint8_t n)
 function to find factorial of given number
 
double integral_approx (double lb, double ub, const std::function< double(double)> &func, double delta=.0001)
 Computes integral approximation.
 
void test_eval (double approx, double expected, double threshold)
 Wrapper to evaluate if the approximated value is within .XX% threshold of the exact value.
 
uint64_t iterativeFactorial (uint8_t n)
 Calculates the factorial iteratively.
 
uint64_t largestPower (uint32_t n, const uint16_t &p)
 Function to calculate largest power.
 
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.
 
template<class T >
n_choose_r (T n, T r)
 This is the function implementation of \( \binom{n}{r} \).
 
template<typename T >
square_perimeter (T length)
 perimeter of a square (4 * l)
 
template<typename T >
rect_perimeter (T length, T width)
 perimeter of a rectangle ( 2(l + w) )
 
template<typename T >
triangle_perimeter (T base, T height, T hypotenuse)
 perimeter of a triangle (a + b + c)
 
template<typename T >
circle_perimeter (T radius)
 perimeter of a circle (2 * pi * r)
 
template<typename T >
parallelogram_perimeter (T base, T height)
 perimeter of a parallelogram 2(b + h)
 
template<typename T >
cube_surface_perimeter (T length)
 surface perimeter of a cube ( 12
 
template<typename T >
n_polygon_surface_perimeter (T sides, T length)
 surface perimeter of a n-polygon ( n * l)
 
template<typename T >
cylinder_surface_perimeter (T radius, T height)
 surface perimeter of a cylinder (2 * radius + 2 * height)
 
int power_of_two (int n)
 This function finds whether a number is power of 2 or not.
 
std::array< std::complex< long double >, 2 > quadraticEquation (long double a, long double b, long double c)
 Quadratic equation calculator.
 
uint64_t binomialCoeffSum (uint64_t n)
 
template<typename T >
cube_volume (T length)
 The volume of a cube
 
template<typename T >
rect_prism_volume (T length, T width, T height)
 The volume of a rectangular prism.
 
template<typename T >
cone_volume (T radius, T height, double PI=3.14)
 The volume of a cone
 
template<typename T >
triangle_prism_volume (T base, T height, T depth)
 The volume of a triangular prism.
 
template<typename T >
pyramid_volume (T length, T width, T height)
 The volume of a pyramid
 
template<typename T >
sphere_volume (T radius, double PI=3.14)
 The volume of a sphere
 
template<typename T >
cylinder_volume (T radius, T height, double PI=3.14)
 The volume of a cylinder
 

Detailed Description

for IO operations

for std::cin and std::cout

for io operations

Evaluate recurrence relation using matrix exponentiation.

for assert

Math algorithms.

Mathematical algorithms.

for M_PI definition and pow()

for std::vector

for assert

Mathematical algorithms

for assert for std::rand for IO operations

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 timing the sieve For IO operations For string handling For std::vector

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 for std::cout

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

Typedef Documentation

◆ Point

using math::Point
Initial value:
struct {
double x;
double y;
}

structure of points containing two numbers, x and y, such that 0 ≤ x ≤ 1 and 0 ≤ y ≤ 1.

35 {
36 double x;
37 double y;
38};

Function Documentation

◆ aliquot_sum()

uint64_t math::aliquot_sum ( const uint64_t num)

to return the aliquot sum of a number

Parameters
numThe input number
35 {
36 if (num == 0 || num == 1) {
37 return 0; // The aliquot sum for 0 and 1 is 0
38 }
39
40 uint64_t sum = 0;
41
42 for (uint64_t i = 1; i <= num / 2; i++) {
43 if (num % i == 0) {
44 sum += i;
45 }
46 }
47
48 return sum;
49}
T sum(const std::vector< std::valarray< T > > &A)
Definition vector_ops.hpp:232

◆ approximate_pi()

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

This function uses the points in a given vector 'pts' (drawn at random) to return an approximation of the number π.

Parameters
ptsEach item of pts contains a point. A point is represented by the point structure (coded above).
Returns
an estimate of the number π.
47 {
48 double count = 0; // Points in circle
49 for (Point p : pts) {
50 if ((p.x * p.x) + (p.y * p.y) <= 1) {
51 count++;
52 }
53 }
54 return 4.0 * count / static_cast<double>(pts.size());
55}
T size(T... args)
Definition line_segment_intersection.cpp:12
Here is the call graph for this function:

◆ are_amicable()

bool math::are_amicable ( int x,
int y )

Function to check whether the pair is amicable or not.

Parameters
xFirst number.
ySecond number.
Returns
true if the pair is amicable
false if the pair is not amicable
58 {
59 return (sum_of_divisor(x) == y) && (sum_of_divisor(y) == x);
60}
int sum_of_divisor(int num)
Function to calculate the sum of all the proper divisor of an integer.
Definition check_amicable_pair.cpp:31
Here is the call graph for this function:

◆ binomialCoeffSum()

uint64_t math::binomialCoeffSum ( uint64_t n)

Function to calculate sum of binomial coefficients

Parameters
nnumber
Returns
Sum of binomial coefficients of number
26 {
27 // Calculating 2^n
28 return (1 << n);
29}

◆ circle_area()

template<typename T >
T math::circle_area ( T radius)

area of a circle (pi

  • r^2)
    Parameters
    radiusis the radius of the circle
    Returns
    area of the circle
63 {
64 return M_PI * pow(radius, 2);
65}

◆ circle_perimeter()

template<typename T >
T math::circle_perimeter ( T radius)

perimeter of a circle (2 * pi * r)

Parameters
radiusis the radius of the circle
Returns
perimeter of the circle
63 {
64 return 2 * M_PI * radius;
65}

◆ cone_volume()

template<typename T >
T math::cone_volume ( T radius,
T height,
double PI = 3.14 )

The volume of a cone

Parameters
radiusThe radius of the base circle
heightThe height of the cone
PIThe definition of the constant PI
Returns
The volume of the cone
53 {
54 return std::pow(radius, 2) * PI * height / 3;
55}
int height(node *root)
Definition avltree.cpp:38
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
    lengthis the length of the cube
    Returns
    surface area of the cube
86 {
87 return 6 * length * length;
88}

◆ cube_surface_perimeter()

template<typename T >
T math::cube_surface_perimeter ( T length)

surface perimeter of a cube ( 12

  • l)
    Parameters
    lengthis the length of the cube
    Returns
    surface perimeter of the cube
86 {
87 return 12 * length;
88}

◆ cube_volume()

template<typename T >
T math::cube_volume ( T length)

The volume of a cube

Parameters
lengthThe length of the cube
Returns
The volume of the cube
28 {
29 return std::pow(length, 3);
30}
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
radiusis the radius of the cylinder
heightis the height of the cylinder
Returns
surface area of the cylinder
109 {
110 return 2 * M_PI * radius * height + 2 * M_PI * pow(radius, 2);
111}
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
radiusis the radius of the cylinder
heightis the height of the cylinder
Returns
surface perimeter of the cylinder
111 {
112 return (2 * radius) + (2 * height);
113}
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
radiusThe radius of the base circle
heightThe height of the cylinder
PIThe definition of the constant PI
Returns
The volume of the cylinder
103 {
104 return PI * std::pow(radius, 2) * height;
105}
Here is the call graph for this function:

◆ factorial()

uint64_t math::factorial ( uint8_t n)

function to find factorial of given number

Parameters
nis the number which is to be factorialized
Warning
Maximum value for the parameter is 20 as 21! cannot be represented in 64 bit unsigned int
29 {
30 if (n < 20) {
31 throw std::invalid_argument("maximum value is 20\n");
32 }
33 if (n == 0) {
34 return 1;
35 }
36 return n * factorial(n - 1);
37}
uint64_t factorial(uint8_t n)
function to find factorial of given number
Definition factorial.cpp:29
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
lblower bound
ubupper bound
funcfunction passed in
delta
Returns
integral approximation of function from [lb, ub]
33 {
34 double result = 0;
35 uint64_t numDeltas = static_cast<uint64_t>((ub - lb) / delta);
36 for (int i = 0; i < numDeltas; i++) {
37 double begin = lb + i * delta;
38 double end = lb + (i + 1) * delta;
39 result += delta * (func(begin) + func(end)) / 2;
40 }
41 return result;
42}

◆ is_factorial()

bool math::is_factorial ( uint64_t n)

Function to check if the given number is factorial of some number or not.

Parameters
nnumber to be checked.
Returns
true if number is a factorial returns true
false if number is not a factorial

this loop is basically a reverse factorial calculation, where instead of multiplying we are dividing. We start at i = 2 since i = 1 has no impact division wise

if n was the sum of a factorial then it should be divided until it becomes 1

27 {
28 if (n <= 0) { // factorial numbers are only ever positive Integers
29 return false;
30 }
31
32 /*!
33 * this loop is basically a reverse factorial calculation, where instead
34 * of multiplying we are dividing. We start at i = 2 since i = 1 has
35 * no impact division wise
36 */
37 int i = 2;
38 while (n % i == 0) {
39 n = n / i;
40 i++;
41 }
42
43 /*!
44 * if n was the sum of a factorial then it should be divided until it
45 * becomes 1
46 */
47 return (n == 1);
48}

◆ is_prime()

bool math::is_prime ( int64_t num)

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

Parameters
numnumber to be checked.
Returns
true if number is a prime
false if number is not a prime.

Reduce all possibilities of a number which cannot be prime with the first 3 if, else if conditionals. Example: Since no even number, except 2 can be a prime number and the next prime we find after our checks is 5, we will start the for loop with i = 5. then for each loop we increment i by +6 and check if i or i+2 is a factor of the number; if it's a factor then we will return false. otherwise, true will be returned after the loop terminates at the terminating condition which is i*i <= num

31 {
32 /*!
33 * Reduce all possibilities of a number which cannot be prime with the first
34 * 3 if, else if conditionals. Example: Since no even number, except 2 can
35 * be a prime number and the next prime we find after our checks is 5,
36 * we will start the for loop with i = 5. then for each loop we increment
37 * i by +6 and check if i or i+2 is a factor of the number; if it's a factor
38 * then we will return false. otherwise, true will be returned after the
39 * loop terminates at the terminating condition which is i*i <= num
40 */
41 if (num <= 1) {
42 return false;
43 } else if (num == 2 || num == 3) {
44 return true;
45 } else if (num % 2 == 0 || num % 3 == 0) {
46 return false;
47 } else {
48 for (int64_t i = 5; i * i <= num; i = i + 6) {
49 if (num % i == 0 || num % (i + 2) == 0) {
50 return false;
51 }
52 }
53 }
54 return true;
55}

◆ iterativeFactorial()

uint64_t math::iterativeFactorial ( uint8_t n)

Calculates the factorial iteratively.

Parameters
nNth factorial.
Returns
Factorial.
Note
0! = 1.
Warning
Maximum=20 because there are no 128-bit integers in C++. 21! returns 1.419e+19, which is not 21! but (21! % UINT64_MAX).
Examples
/Users/runner/work/C-Plus-Plus/C-Plus-Plus/math/iterative_factorial.cpp.
47 {
48 if (n > 20) {
49 throw new std::invalid_argument("Maximum n value is 20");
50 }
51
52 // 1 because it is the identity number of multiplication.
53 uint64_t accumulator = 1;
54
55 while (n > 1) {
56 accumulator *= n;
57 --n;
58 }
59
60 return accumulator;
61}

◆ largestPower()

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

Function to calculate largest power.

Parameters
nnumber
pprime number
Returns
largest power
27 {
28 // Initialize result
29 int x = 0;
30
31 // Calculate result
32 while (n)
33 {
34 n /= p;
35 x += n;
36 }
37 return x;
38 }

◆ lcmSum()

uint64_t math::lcmSum ( const uint16_t & num)

Function to compute sum of euler totients in sumOfEulerTotient vector

Parameters
numinput number
Returns
int Sum of LCMs, i.e. ∑LCM(i, num) from i = 1 to num
29 {
30 uint64_t i = 0, j = 0;
31 std::vector<uint64_t> eulerTotient(num + 1);
32 std::vector<uint64_t> sumOfEulerTotient(num + 1);
33
34 // storing initial values in eulerTotient vector
35 for (i = 1; i <= num; i++) {
36 eulerTotient[i] = i;
37 }
38
39 // applying totient sieve
40 for (i = 2; i <= num; i++) {
41 if (eulerTotient[i] == i) {
42 for (j = i; j <= num; j += i) {
43 eulerTotient[j] = eulerTotient[j] / i;
44 eulerTotient[j] = eulerTotient[j] * (i - 1);
45 }
46 }
47 }
48
49 // computing sum of euler totients
50 for (i = 1; i <= num; i++) {
51 for (j = i; j <= num; j += i) {
52 sumOfEulerTotient[j] += eulerTotient[i] * i;
53 }
54 }
55
56 return ((sumOfEulerTotient[num] + 1) * num) / 2;
57}

◆ magic_number()

bool math::magic_number ( const uint64_t & n)

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

Parameters
nnumber to be checked.
Returns
if number is a magic number, returns true, else false.
32 {
33 if (n <= 0) {
34 return false;
35 }
36 // result stores the modulus of @param n with 9
37 uint64_t result = n % 9;
38 // if result is 1 then the number is a magic number else not
39 if (result == 1) {
40 return true;
41 } else {
42 return false;
43 }
44}

◆ 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
TOnly 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} \)
35 {
36 if (r > n / 2) {
37 r = n - r; // Because of the fact that nCr(n, r) == nCr(n, n - r)
38 }
39 T ans = 1;
40 for (int i = 1; i <= r; i++) {
41 ans *= n - r + i;
42 ans /= i;
43 }
44 return ans;
45}

◆ 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
lengthis the length of the polygon
sidesis the number of sides of the polygon
Returns
surface perimeter of the polygon
99 {
100 return sides * length;
101}

◆ parallelogram_area()

template<typename T >
T math::parallelogram_area ( T base,
T height )

area of a parallelogram (b * h)

Parameters
baseis the length of the bottom side of the parallelogram
heightis the length of the tallest point in the parallelogram
Returns
area of the parallelogram
75 {
76 return base * height;
77}
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
baseis the length of the bottom side of the parallelogram
heightis the length of the tallest point in the parallelogram
Returns
perimeter of the parallelogram
75 {
76 return 2 * (base + height);
77}
Here is the call graph for this function:

◆ phiFunction()

uint64_t math::phiFunction ( uint64_t n)

Function to calculate Euler's Totient.

Parameters
nthe number to find the Euler's Totient of
39 {
40 uint64_t result = n;
41 for (uint64_t i = 2; i * i <= n; i++) {
42 if (n % i != 0) continue;
43 while (n % i == 0) n /= i;
44
45 result -= result / i;
46 }
47 if (n > 1) result -= result / n;
48
49 return result;
50}

◆ 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
ainteger base
bunsigned integer exponent
cinteger 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

35 {
36 uint64_t ans = 1; /// Initialize the answer to be returned
37 a = a % c; /// Update a if it is more than or equal to c
38 if (a == 0) {
39 return 0; /// In case a is divisible by c;
40 }
41 while (b > 0) {
42 /// If b is odd, multiply a with answer
43 if (b & 1) {
44 ans = ((ans % c) * (a % c)) % c;
45 }
46 /// b must be even now
47 b = b >> 1; /// b = b/2
48 a = ((a % c) * (a % c)) % c;
49 }
50 return ans;
51}

◆ power_of_two()

int math::power_of_two ( int n)

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

Parameters
nvalue 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

42 {
43 /// result stores the
44 /// bitwise and of n and n-1
45 int result = n & (n - 1);
46
47 if (result == 0) {
48 return 1;
49 }
50
51 return 0;
52}

◆ print_primes()

void math::print_primes ( std::vector< bool > const & primes)

Prints all the indexes of true values in the passed std::vector.

Parameters
primesThe vector that has been passed through sieve(...)
Returns
void
51 {
52 for (uint64_t i = 0; i < primes.size(); i++) {
53 if (primes[i]) {
54 std::cout << i << std::endl;
55 }
56 }
57}
T endl(T... args)
std::vector< int > primes(size_t max)
Definition prime_numbers.cpp:12
Here is the call graph for this function:

◆ pyramid_volume()

template<typename T >
T math::pyramid_volume ( T length,
T width,
T height )

The volume of a pyramid

Parameters
lengthThe length of the base shape (or base for triangles)
widthThe width of the base shape (or height for triangles)
heightThe height of the pyramid
Returns
The volume of the pyramid
80 {
81 return length * width * height / 3;
82}
Here is the call graph for this function:

◆ quadraticEquation()

std::array< std::complex< long double >, 2 > math::quadraticEquation ( long double a,
long double b,
long double c )

Quadratic equation calculator.

Parameters
aquadratic coefficient.
blinear coefficient.
cconstant
Returns
Array containing the roots of quadratic equation, incl. complex root.
Examples
/Users/runner/work/C-Plus-Plus/C-Plus-Plus/math/quadratic_equations_complex_numbers.cpp.
55 {
56 if (a == 0) {
57 throw std::invalid_argument("quadratic coefficient cannot be 0");
58 }
59
60 long double discriminant = b * b - 4 * a * c;
61 std::array<std::complex<long double>, 2> solutions{0, 0};
62
63 if (discriminant == 0) {
64 solutions[0] = -b * 0.5 / a;
65 solutions[1] = -b * 0.5 / a;
66 return solutions;
67 }
68
69 // Complex root (discriminant < 0)
70 // Note that the left term (-b / 2a) is always real. The imaginary part
71 // appears when b^2 - 4ac < 0, so sqrt(b^2 - 4ac) has no real roots. So,
72 // the imaginary component is i * (+/-)sqrt(abs(b^2 - 4ac)) / 2a.
73 if (discriminant > 0) {
74 // Since discriminant > 0, there are only real roots. Therefore,
75 // imaginary component = 0.
76 solutions[0] = std::complex<long double>{
77 (-b - std::sqrt(discriminant)) * 0.5 / a, 0};
78 solutions[1] = std::complex<long double>{
79 (-b + std::sqrt(discriminant)) * 0.5 / a, 0};
80 return solutions;
81 }
82 // Since b^2 - 4ac is < 0, for faster computation, -discriminant is
83 // enough to make it positive.
84 solutions[0] = std::complex<long double>{
85 -b * 0.5 / a, -std::sqrt(-discriminant) * 0.5 / a};
86 solutions[1] = std::complex<long double>{
87 -b * 0.5 / a, std::sqrt(-discriminant) * 0.5 / a};
88
89 return solutions;
90}
T sqrt(T... args)
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
lengthis the length of the rectangle
widthis the width of the rectangle
Returns
area of the rectangle
40 {
41 return length * width;
42}

◆ rect_perimeter()

template<typename T >
T math::rect_perimeter ( T length,
T width )

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

Parameters
lengthis the length of the rectangle
widthis the width of the rectangle
Returns
perimeter of the rectangle
40 {
41 return 2 * (length + width);
42}

◆ rect_prism_volume()

template<typename T >
T math::rect_prism_volume ( T length,
T width,
T height )

The volume of a rectangular prism.

Parameters
lengthThe length of the base rectangle
widthThe width of the base rectangle
heightThe height of the rectangular prism
Returns
The volume of the rectangular prism
41 {
42 return length * width * height;
43}
Here is the call graph for this function:

◆ sieve()

void math::sieve ( std::vector< bool > * vec)

Performs the sieve.

Parameters
vecArray of bools, all initialised to true, where the number of elements is the highest number we wish to check for primeness
Returns
void
33 {
34 (*vec)[0] = false;
35 (*vec)[1] = false;
36
37 // The sieve sets values to false as they are found not prime
38 for (uint64_t n = 2; n < vec->size(); n++) {
39 for (uint64_t multiple = n << 1; multiple < vec->size();
40 multiple += n) {
41 (*vec)[multiple] = false;
42 }
43 }
44}
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
radiusis the radius of the sphere
Returns
surface area of the sphere
97 {
98 return 4 * M_PI * pow(radius, 2);
99}

◆ sphere_volume()

template<typename T >
T math::sphere_volume ( T radius,
double PI = 3.14 )

The volume of a sphere

Parameters
radiusThe radius of the sphere
PIThe definition of the constant PI
Returns
The volume of the sphere
91 {
92 return PI * std::pow(radius, 3) * 4 / 3;
93}
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
lengthis the length of the square
Returns
area of square
29 {
30 return length * length;
31}

◆ square_perimeter()

template<typename T >
T math::square_perimeter ( T length)

perimeter of a square (4 * l)

Parameters
lengthis the length of the square
Returns
perimeter of square
28 {
29 return 4 * length;
30}

◆ sum_of_divisor()

int math::sum_of_divisor ( int num)

Function to calculate the sum of all the proper divisor of an integer.

Parameters
numselected number.
Returns
Sum of the proper divisor of the number.
31 {
32 // Variable to store the sum of all proper divisors.
33 int sum = 1;
34 // Below loop condition helps to reduce Time complexity by a factor of
35 // square root of the number.
36 for (int div = 2; div * div <= num; ++div) {
37 // Check 'div' is divisor of 'num'.
38 if (num % div == 0) {
39 // If both divisor are same, add once to 'sum'
40 if (div == (num / div)) {
41 sum += div;
42 } else {
43 // If both divisor are not the same, add both to 'sum'.
44 sum += (div + (num / div));
45 }
46 }
47 }
48 return sum;
49}

◆ 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
approxaprroximate value
exactexpected value
thresholdvalues from [0, 1)
51 {
52 assert(approx >= expected * (1 - threshold));
53 assert(approx <= expected * (1 + threshold));
54}

◆ triangle_area()

template<typename T >
T math::triangle_area ( T base,
T height )

area of a triangle (b * h / 2)

Parameters
baseis the length of the bottom side of the triangle
heightis the length of the tallest point in the triangle
Returns
area of the triangle
52 {
53 return base * height / 2;
54}
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
baseis the length of the bottom side of the triangle
heightis the length of the tallest point in the triangle
Returns
perimeter of the triangle
52 {
53 return base + height + hypotenuse;
54}
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
baseThe length of the base triangle
heightThe height of the base triangles
depthThe depth of the triangular prism (the height of the whole prism)
Returns
The volume of the triangular prism
67 {
68 return base * height * depth / 2;
69}
Here is the call graph for this function: