math Namespace Reference

for std::vector More...

## Functions

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...
uint64_t | binomialCoeffSum (uint64_t n)

## Detailed Description

for std::vector

Math algorithms.

for std::cin and std::cout

for IO operations

for io operations

for assert

for assert for std::cin and std::cout

Mathematical algorithms

for std::cin and std::cout

Mathematical algorithms

for assert

Mathematical algorithms

for assert for io operations

Mathematical algorithms

## Function Documentation

## 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);
}

## 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< uint64_t >

## 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;
}
}
math::fibonacci_sum::result
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76

## 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;
}
ans
ll ans(ll n)
Definition: matrix_exponentiation.cpp:91
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;
}
Here is the call graph for this function: