TheAlgorithms/C++ 1.0.0
All the 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 <cstdint>
#include <iostream>
#include <vector>
Include dependency graph for lcm_sum.cpp:

Go to the source code of this file.

Namespaces

namespace  math
 for assert
 

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

Definition in file lcm_sum.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 97 of file lcm_sum.cpp.

97 {
98 test(); // execute the tests
99 return 0;
100}
static void test()
Definition lcm_sum.cpp:66

◆ test()

static void test ( )
static

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

Returns
void

Definition at line 66 of file lcm_sum.cpp.

66 {
67 uint64_t n = 2;
68 uint64_t test_1 = math::lcmSum(n);
69 assert(test_1 == 4);
70 std::cout << "Passed Test 1!" << std::endl;
71
72 n = 5;
73 uint64_t test_2 = math::lcmSum(n);
74 assert(test_2 == 55);
75 std::cout << "Passed Test 2!" << std::endl;
76
77 n = 10;
78 uint64_t test_3 = math::lcmSum(n);
79 assert(test_3 == 320);
80 std::cout << "Passed Test 3!" << std::endl;
81
82 n = 11;
83 uint64_t test_4 = math::lcmSum(n);
84 assert(test_4 == 616);
85 std::cout << "Passed Test 4!" << std::endl;
86
87 n = 15;
88 uint64_t test_5 = math::lcmSum(n);
89 assert(test_5 == 1110);
90 std::cout << "Passed Test 5!" << std::endl;
91}
static void test_1()
static void test_2()
static void test_3()
uint64_t lcmSum(const uint16_t &num)
Definition lcm_sum.cpp:30