TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
math Namespace Reference

for assert 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)
 
template<typename T >
hemi_sphere_surface_area (T radius)
 surface area of a hemi-sphere ( 3 * 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 assert

std::cout

for IO implementations

Evaluate recurrence relation using matrix exponentiation.

Math algorithms.

for mathematical functions

Mathematical algorithms.

for IO operations

for M_PI definition and pow()

for std::vector

for IO operations

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

Mathematical algorithms

for cout

Mathematical algorithms

For assert For timing the sieve For IO operations For string handling For std::vector

for I/O operations

Mathematical algorithms

for assert

Math algorithms

for std::cin and std::cout for std::vector

Mathematical algorithms

for std::abs for std::array for assert

Maths algorithms

for assert for passing in functions for IO operations

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 assert for integral types for std::invalid_argument for std::cout

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 io operations

Mathematical algorithms

for assert for std::int64_t

Maths algorithms.

for std::cout for std::vector

Mathematical algorithms

for assert for std::cout

Mathematical algorithms

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

Mathematical algorithms

for IO operations for assert

std::array assert std::sqrt, std::trunc, std::pow std::complex std::invalid_argument std::setprecision

Mathematical algorithms

for assert 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.

Definition at line 35 of file approximate_pi.cpp.

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

Definition at line 36 of file aliquot_sum.cpp.

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

◆ 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 π.

Definition at line 47 of file approximate_pi.cpp.

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}

◆ 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

Definition at line 58 of file check_amicable_pair.cpp.

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.

◆ binomialCoeffSum()

uint64_t math::binomialCoeffSum ( uint64_t n)

Function to calculate sum of binomial coefficients

Parameters
nnumber
Returns
Sum of binomial coefficients of number

Definition at line 27 of file sum_of_binomial_coefficient.cpp.

27 {
28 // Calculating 2^n
29 return (1 << n);
30}

◆ 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

Definition at line 63 of file area.cpp.

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

Definition at line 63 of file perimeter.cpp.

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

Definition at line 53 of file volume.cpp.

53 {
54 return std::pow(radius, 2) * PI * height / 3;
55}
int height(node *root)
Definition avltree.cpp:38

◆ 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

Definition at line 86 of file area.cpp.

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

Definition at line 86 of file perimeter.cpp.

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

Definition at line 28 of file volume.cpp.

28 {
29 return std::pow(length, 3);
30}

◆ 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

Definition at line 109 of file area.cpp.

109 {
110 return 2 * M_PI * radius * height + 2 * M_PI * pow(radius, 2);
111}

◆ 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

Definition at line 111 of file perimeter.cpp.

111 {
112 return (2 * radius) + (2 * height);
113}

◆ 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

Definition at line 103 of file volume.cpp.

103 {
104 return PI * std::pow(radius, 2) * height;
105}

◆ 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

Definition at line 29 of file factorial.cpp.

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

◆ hemi_sphere_surface_area()

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

surface area of a hemi-sphere ( 3 * pi * r^2)

Parameters
radiusis the radius of the hemi-sphere
Template Parameters
Tdatatype of radius
Returns
surface area of the hemi-sphere

Definition at line 121 of file area.cpp.

121 {
122 return 3 * M_PI * pow(radius, 2);
123}

◆ 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]

Definition at line 42 of file integral_approximation.cpp.

44 {
45 double result = 0;
46 uint64_t numDeltas = static_cast<uint64_t>((ub - lb) / delta);
47 for (int i = 0; i < numDeltas; i++) {
48 double begin = lb + i * delta;
49 double end = lb + (i + 1) * delta;
50 result += delta * (func(begin) + func(end)) / 2;
51 }
52 return result;
53}

◆ 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

Definition at line 28 of file check_factorial.cpp.

28 {
29 if (n <= 0) { // factorial numbers are only ever positive Integers
30 return false;
31 }
32
38 int i = 2;
39 while (n % i == 0) {
40 n = n / i;
41 i++;
42 }
43
48 return (n == 1);
49}

◆ 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

Definition at line 31 of file check_prime.cpp.

31 {
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.

Definition at line 47 of file iterative_factorial.cpp.

47 {
48 if (n > 20) {
49 throw 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

Definition at line 29 of file largest_power.cpp.

29 {
30 // Initialize result
31 int x = 0;
32
33 // Calculate result
34 while (n) {
35 n /= p;
36 x += n;
37 }
38 return x;
39}

◆ 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

Definition at line 30 of file lcm_sum.cpp.

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

◆ 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.

Definition at line 33 of file magic_number.cpp.

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

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

Definition at line 35 of file n_choose_r.cpp.

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

Definition at line 99 of file perimeter.cpp.

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

Definition at line 75 of file area.cpp.

75 {
76 return base * height;
77}

◆ 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

Definition at line 75 of file perimeter.cpp.

75 {
76 return 2 * (base + height);
77}

◆ phiFunction()

uint64_t math::phiFunction ( uint64_t n)

Function to calculate Euler's Totient.

Parameters
nthe number to find the Euler's Totient of

Definition at line 41 of file eulers_totient_function.cpp.

41 {
42 uint64_t result = n;
43 for (uint64_t i = 2; i * i <= n; i++) {
44 if (n % i != 0)
45 continue;
46 while (n % i == 0) n /= i;
47
48 result -= result / i;
49 }
50 if (n > 1)
51 result -= result / n;
52
53 return result;
54}

◆ 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

Definition at line 35 of file modular_exponentiation.cpp.

35 {
36 uint64_t ans = 1;
37 a = a % c;
38 if (a == 0) {
39 return 0;
40 }
41 while (b > 0) {
43 if (b & 1) {
44 ans = ((ans % c) * (a % c)) % c;
45 }
47 b = b >> 1;
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

Definition at line 42 of file power_of_two.cpp.

42 {
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

Definition at line 51 of file eratosthenes.cpp.

51 {
52 for (uint64_t i = 0; i < primes.size(); i++) {
53 if (primes[i]) {
54 std::cout << i << std::endl;
55 }
56 }
57}
std::vector< int > primes(size_t max)

◆ 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

Definition at line 80 of file volume.cpp.

80 {
81 return length * width * height / 3;
82}

◆ 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.

Definition at line 53 of file 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}

◆ 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

Definition at line 40 of file area.cpp.

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

Definition at line 40 of file perimeter.cpp.

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

Definition at line 41 of file volume.cpp.

41 {
42 return length * width * height;
43}

◆ 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

Definition at line 33 of file eratosthenes.cpp.

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}

◆ 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

Definition at line 97 of file area.cpp.

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

Definition at line 91 of file volume.cpp.

91 {
92 return PI * std::pow(radius, 3) * 4 / 3;
93}

◆ 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

Definition at line 29 of file area.cpp.

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

Definition at line 28 of file perimeter.cpp.

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.

Definition at line 31 of file check_amicable_pair.cpp.

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)

Definition at line 62 of file integral_approximation.cpp.

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

◆ 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

Definition at line 52 of file area.cpp.

52 {
53 return base * height / 2;
54}

◆ 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

Definition at line 52 of file perimeter.cpp.

52 {
53 return base + height + hypotenuse;
54}

◆ 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

Definition at line 67 of file volume.cpp.

67 {
68 return base * height * depth / 2;
69}