Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
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

namespace  math
 for IO operations
 

Functions

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

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
96 {
97 test(); // execute the tests
98 return 0;
99}
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
65 {
66 uint64_t n = 2;
67 uint64_t test_1 = math::lcmSum(n);
68 assert(test_1 == 4);
69 std::cout << "Passed Test 1!" << std::endl;
70
71 n = 5;
72 uint64_t test_2 = math::lcmSum(n);
73 assert(test_2 == 55);
74 std::cout << "Passed Test 2!" << std::endl;
75
76 n = 10;
77 uint64_t test_3 = math::lcmSum(n);
78 assert(test_3 == 320);
79 std::cout << "Passed Test 3!" << std::endl;
80
81 n = 11;
82 uint64_t test_4 = math::lcmSum(n);
83 assert(test_4 == 616);
84 std::cout << "Passed Test 4!" << std::endl;
85
86 n = 15;
87 uint64_t test_5 = math::lcmSum(n);
88 assert(test_5 == 1110);
89 std::cout << "Passed Test 5!" << std::endl;
90}
T endl(T... args)
static void test_1()
Definition heavy_light_decomposition.cpp:505
static void test_2()
Definition heavy_light_decomposition.cpp:549
static void test_3()
Definition heavy_light_decomposition.cpp:592
uint64_t lcmSum(const uint16_t &num)
Definition lcm_sum.cpp:29
Here is the call graph for this function: