mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Update math/lcm_sum.cpp
Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
This commit is contained in:
parent
1af94ccf6f
commit
997404b48a
@ -1,13 +1,13 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief An algorithm to calculate the sum of LCM: \f$\mathrm{LCM}(1,n) + \mathrm{LCM}(2,n) + \ldots + \mathrm{LCM}(n,n)\f$
|
||||
* @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$
|
||||
* 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:
|
||||
* \f[
|
||||
* \sum\mathrm{LCM}(i, n) = \frac{1}{2} \left[\left(\sum (d * \mathrm{ETF}(d)) + 1\right) * n\right]
|
||||
* \f]
|
||||
*
|
||||
* where \mathrm{ETF}(i) represents Euler totient function of i.
|
||||
* @author [Chesta Mittal](https://github.com/chestamittal)
|
||||
*/
|
||||
|
||||
@ -25,11 +25,11 @@ namespace math {
|
||||
* @param num input number
|
||||
* @returns int Sum of LCMs, i.e. ∑LCM(i, num) from i = 1 to num
|
||||
*/
|
||||
int lcmSum(int num) {
|
||||
uint64_t lcmSum(uint64_t num) {
|
||||
|
||||
int i=0, j=0;
|
||||
std::vector <int> eulerTotient(num+1);
|
||||
std::vector <int> sumOfEulerTotient(num+1);
|
||||
uint64_t i=0, j=0;
|
||||
std::vector <uint64_t> eulerTotient(num+1);
|
||||
std::vector <uint64_t> sumOfEulerTotient(num+1);
|
||||
|
||||
// storing initial values in eulerTotient vector
|
||||
for(i=1; i<=num; i++) {
|
||||
@ -63,28 +63,28 @@ namespace math {
|
||||
* @returns `void`
|
||||
*/
|
||||
static void test() {
|
||||
int n = 2;
|
||||
int test_1 = math::lcmSum(n);
|
||||
uint64_t n = 2;
|
||||
uint64_t test_1 = math::lcmSum(n);
|
||||
assert(test_1 == 4);
|
||||
std::cout << "Passed Test 1!" << std::endl;
|
||||
|
||||
n = 5;
|
||||
int test_2 = math::lcmSum(n);
|
||||
uint64_t test_2 = math::lcmSum(n);
|
||||
assert(test_2 == 55);
|
||||
std::cout << "Passed Test 2!" << std::endl;
|
||||
|
||||
n = 10;
|
||||
int test_3 = math::lcmSum(n);
|
||||
uint64_t test_3 = math::lcmSum(n);
|
||||
assert(test_3 == 320);
|
||||
std::cout << "Passed Test 3!" << std::endl;
|
||||
|
||||
n = 11;
|
||||
int test_4 = math::lcmSum(n);
|
||||
uint64_t test_4 = math::lcmSum(n);
|
||||
assert(test_4 == 616);
|
||||
std::cout << "Passed Test 4!" << std::endl;
|
||||
|
||||
n = 15;
|
||||
int test_5 = math::lcmSum(n);
|
||||
uint64_t test_5 = math::lcmSum(n);
|
||||
assert(test_5 == 1110);
|
||||
std::cout << "Passed Test 5!" << std::endl;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user