2020-10-20 02:56:45 +08:00
|
|
|
/**
|
|
|
|
* @file
|
2020-10-20 16:49:15 +08:00
|
|
|
* @brief An algorithm to calculate the sum of LCM: \f$\mathrm{LCM}(1,n) + \mathrm{LCM}(2,n) + .. + \mathrm{LCM}(n,n)\f$
|
|
|
|
* @details An algorithm to calculate the sum of LCM: \f$\mathrm{LCM}(1,n) + \mathrm{LCM}(2,n) + .. + \mathrm{LCM}(n,n)\f$ where \f$\mathrm{LCM}(i,n)\f$
|
2020-10-20 02:56:45 +08:00
|
|
|
* 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:
|
2020-10-20 16:49:29 +08:00
|
|
|
* \f[
|
|
|
|
* \sum\mathrm{LCM}(i, n) = \frac{1}{2} \left[\left(\sum (d * \mathrm{ETF}(d)) + 1\right) * n\right]
|
|
|
|
* \f]
|
2020-10-20 02:56:45 +08:00
|
|
|
*
|
|
|
|
* @author [Chesta Mittal](https://github.com/chestamittal)
|
|
|
|
*/
|
|
|
|
|
2020-10-20 04:10:10 +08:00
|
|
|
#include <iostream> /// for std::cin and std::cout
|
|
|
|
#include <cassert> /// for assert
|
|
|
|
#include <vector> /// for std::vector
|
2020-10-20 02:56:45 +08:00
|
|
|
|
|
|
|
/**
|
2020-10-20 05:33:12 +08:00
|
|
|
* @namespace math
|
|
|
|
* @brief Mathematical algorithms
|
2020-10-20 02:56:45 +08:00
|
|
|
*/
|
2020-10-20 05:33:12 +08:00
|
|
|
namespace math {
|
|
|
|
/**
|
|
|
|
* Function to compute sum of euler totients in sumOfEulerTotient vector
|
|
|
|
* @param num input number
|
|
|
|
* @returns int Sum of LCMs, i.e. ∑LCM(i, num) from i = 1 to num
|
|
|
|
*/
|
|
|
|
int lcmSum(int num) {
|
2020-10-20 02:56:45 +08:00
|
|
|
|
2020-10-20 05:33:12 +08:00
|
|
|
int i=0, j=0;
|
2020-10-20 16:49:29 +08:00
|
|
|
std::vector <int> eulerTotient(num+1);
|
|
|
|
std::vector <int> sumOfEulerTotient(num+1);
|
2020-10-20 02:56:45 +08:00
|
|
|
|
2020-10-20 05:33:12 +08:00
|
|
|
// storing initial values in eulerTotient vector
|
2020-10-20 16:49:29 +08:00
|
|
|
for(i=1; i<=num; i++) {
|
2020-10-20 05:33:12 +08:00
|
|
|
eulerTotient[i] = i;
|
|
|
|
}
|
2020-10-20 02:56:45 +08:00
|
|
|
|
2020-10-20 05:33:12 +08:00
|
|
|
// applying totient sieve
|
2020-10-20 16:49:29 +08:00
|
|
|
for(i=2; i<=num; i++) {
|
2020-10-20 05:33:12 +08:00
|
|
|
if(eulerTotient[i] == i) {
|
2020-10-20 16:49:29 +08:00
|
|
|
for(j=i; j<=num; j+=i) {
|
2020-10-20 05:33:12 +08:00
|
|
|
eulerTotient[j] = eulerTotient[j]/i;
|
|
|
|
eulerTotient[j] = eulerTotient[j]*(i-1);
|
|
|
|
}
|
2020-10-20 02:56:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 05:33:12 +08:00
|
|
|
// computing sum of euler totients
|
2020-10-20 16:49:29 +08:00
|
|
|
for(i=1; i<=num; i++) {
|
|
|
|
for(j=i; j <=num; j+=i) {
|
2020-10-20 05:33:12 +08:00
|
|
|
sumOfEulerTotient[j] += eulerTotient[i]*i;
|
|
|
|
}
|
2020-10-20 02:56:45 +08:00
|
|
|
}
|
2020-10-20 05:33:12 +08:00
|
|
|
|
|
|
|
return ((sumOfEulerTotient[num] + 1 ) * num) / 2;
|
2020-10-20 02:56:45 +08:00
|
|
|
}
|
2020-10-20 05:33:03 +08:00
|
|
|
} // namespace math
|
2020-10-20 02:56:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function for testing lcmSum function.
|
|
|
|
* test cases and assert statement.
|
|
|
|
* @returns `void`
|
|
|
|
*/
|
|
|
|
static void test() {
|
|
|
|
int n = 2;
|
2020-10-20 05:32:50 +08:00
|
|
|
int test_1 = math::lcmSum(n);
|
2020-10-20 02:56:45 +08:00
|
|
|
assert(test_1 == 4);
|
|
|
|
std::cout << "Passed Test 1!" << std::endl;
|
|
|
|
|
|
|
|
n = 5;
|
2020-10-20 05:32:11 +08:00
|
|
|
int test_2 = math::lcmSum(n);
|
2020-10-20 02:56:45 +08:00
|
|
|
assert(test_2 == 55);
|
|
|
|
std::cout << "Passed Test 2!" << std::endl;
|
|
|
|
|
|
|
|
n = 10;
|
2020-10-20 05:32:41 +08:00
|
|
|
int test_3 = math::lcmSum(n);
|
2020-10-20 02:56:45 +08:00
|
|
|
assert(test_3 == 320);
|
|
|
|
std::cout << "Passed Test 3!" << std::endl;
|
|
|
|
|
|
|
|
n = 11;
|
2020-10-20 05:32:32 +08:00
|
|
|
int test_4 = math::lcmSum(n);
|
2020-10-20 02:56:45 +08:00
|
|
|
assert(test_4 == 616);
|
|
|
|
std::cout << "Passed Test 4!" << std::endl;
|
|
|
|
|
|
|
|
n = 15;
|
2020-10-20 05:32:21 +08:00
|
|
|
int test_5 = math::lcmSum(n);
|
2020-10-20 02:56:45 +08:00
|
|
|
assert(test_5 == 1110);
|
|
|
|
std::cout << "Passed Test 5!" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-20 04:09:58 +08:00
|
|
|
* @brief Main function
|
|
|
|
* @returns 0 on exit
|
2020-10-20 04:09:23 +08:00
|
|
|
*/
|
2020-10-20 02:56:45 +08:00
|
|
|
int main() {
|
2020-10-20 04:09:35 +08:00
|
|
|
test(); // execute the tests
|
2020-10-20 02:56:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|