Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
math Namespace Reference

for std::vector More...

Functions

uint64_t lcmSum (const uint16_t &num)
 
bool magic_number (const uint64_t &n)
 
uint64_t binomialCoeffSum (uint64_t n)
 

Detailed Description

for std::vector

for assert

for io operations

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

Mathematical algorithms

for assert

Mathematical algorithms

for std::cin and std::cout

Mathematical algorithms

Function Documentation

◆ binomialCoeffSum()

uint64_t math::binomialCoeffSum ( uint64_t  n)

Function to calculate sum of binomial coefficients

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

◆ 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
28  {
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 }
std::vector
STL class.