lcm_sum.cpp File Reference

An algorithm to calculate the sum of LCM: \(\mathrm{LCM}(1,n) + \mathrm{LCM}(2,n) + \ldots + \mathrm{LCM}(n,n)\). More...

`#include <cassert>`
`#include <iostream>`
`#include <vector>`
Include dependency graph for lcm_sum.cpp:

## Namespaces

| math
| for std::vector

## Functions

uint64_t | math::lcmSum (const uint16_t &num)
static void | test ()
int | main ()
| Main function. More...

## Detailed Description

An algorithm to calculate the sum of LCM: \(\mathrm{LCM}(1,n) + \mathrm{LCM}(2,n) + \ldots + \mathrm{LCM}(n,n)\).

An algorithm to calculate the sum of LCM: \(\mathrm{LCM}(1,n) + \mathrm{LCM}(2,n) + \ldots + \mathrm{LCM}(n,n)\) where \(\mathrm{LCM}(i,n)\) denotes the Least Common Multiple of the integers i and n. For n greater than or equal to 1. The value of the sum is calculated by formula:

\[ \sum\mathrm{LCM}(i, n) = \frac{1}{2} \left[\left(\sum (d * \mathrm{ETF}(d)) + 1\right) * n\right] \]

where \mathrm{ETF}(i) represents Euler totient function of i.

Author
  Chesta Mittal

## Function Documentation

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
test(); // execute the tests
return 0;
}
test
static void test()
Definition: lcm_sum.cpp:65
Here is the call graph for this function:

## test()

static void test | ( |  | ) |

static

Function for testing lcmSum function. test cases and assert statement.

Returns
  `void`

{
uint64_t n = 2;
uint64_t test_1 = math::lcmSum(n);
assert(test_1 == 4);
std::cout << "Passed Test 1!" << std::endl;
n = 5;
uint64_t test_2 = math::lcmSum(n);
assert(test_2 == 55);
std::cout << "Passed Test 2!" << std::endl;
n = 10;
uint64_t test_3 = math::lcmSum(n);
assert(test_3 == 320);
std::cout << "Passed Test 3!" << std::endl;
n = 11;
uint64_t test_4 = math::lcmSum(n);
assert(test_4 == 616);
std::cout << "Passed Test 4!" << std::endl;
n = 15;
uint64_t test_5 = math::lcmSum(n);
assert(test_5 == 1110);
std::cout << "Passed Test 5!" << std::endl;
}
std::cout
std::endl
T endl(T... args)
test_1
static void test_1()
Definition: heavy_light_decomposition.cpp:505
test_2
static void test_2()
Definition: heavy_light_decomposition.cpp:549
test_3
static void test_3()
Definition: heavy_light_decomposition.cpp:592
math::lcmSum
uint64_t lcmSum(const uint16_t &num)
Definition: lcm_sum.cpp:29
Here is the call graph for this function: