Sum of LCM(1, n), LCM(2, n), LCM(3, n), … , LCM(n, n)

Given an integer n, the task is to find the sum:

LCM(1, n) + LCM(2, n) + LCM(3, n) + … + LCM(n, n)
where LCM(i, n) is the Least Common Multiple of i and n.

Examples:

Input: 3
Output: 10
LCM(1, 3) + LCM(2, 3) + LCM(3, 3) = 3 + 6 + 3 = 12

Input: 5
Output: 55
LCM(1, 5) + LCM(2, 5) + LCM(3, 5) + LCM(4, 5) + LCM(5, 5) = 55

Naive Approach: LCM of two numbers a and b = (a * b) / gcd(a, b) where gcd(a, b) is the Greatest Common Divisor of a and b.

  • Calculate the values of individual LCM for all pairs starting from (1, n) to (n, n).
  • Sum all the LCM results from the previous step.
  • Print the sum in the end.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
  
// Function to calculate the required LCM sum
ll lcmSum(long long n)
{
    ll sum = 0;
  
    for (long long int i = 1; i <= n; i++) {
  
        // GCD of i and n
        long long int gcd = __gcd(i, n);
  
        // LCM of i and n i.e. (i * n) / gcd(i, n)
        long long int lcm = (i * n) / gcd;
  
        // Update sum
        sum = sum + lcm;
    }
  
    return sum;
}
  
// Driver code
int main()
{
    int n = 3;
  
    cout << lcmSum(n);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
import math
  
# Function to calculate the required LCM sum 
def lcmSum(n): 
  
    Sum = 0
    for i in range(1, n + 1): 
  
        # GCD of i and n 
        gcd = math.gcd(i, n) 
  
        # LCM of i and n i.e. (i * n) / gcd(i, n) 
        lcm = (i * n) // gcd 
  
        # Update sum 
        Sum = Sum + lcm 
  
    return Sum
  
# Driver code 
if __name__ == "__main__":
  
    n = 3
    print(lcmSum(n)) 
  
# This code is contributed by Rituraj Jain

chevron_right


Output:

12

Efficient Approach: Using Euler Totient Function,
∑LCM(i, n) = ((∑(d * ETF(d)) + 1) * n) / 2
where ETF(d) is Euler totient function of d and d belongs to the set of divisors of n.

Example:

Let n be 5 then LCM(1, 5) + LCM(2, 5) + LCM(3, 5) + LCM(4, 5) + LCM(5, 5)
= 5 + 10 + 15 + 20 + 5
= 55

With Euler Totient Function:
All divisors of 5 are {1, 5}
Hence, ((1*ETF(1) + 5*ETF(4) + 1) * 5) / 2 = 55

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
#define n 1000002
#define ll long long int
  
ll phi[n + 2], ans[n + 2];
  
// Euler totient Function
void ETF()
{
    for (int i = 1; i <= n; i++) {
        phi[i] = i;
    }
  
    for (int i = 2; i <= n; i++) {
        if (phi[i] == i) {
            phi[i] = i - 1;
            for (int j = 2 * i; j <= n; j += i) {
                phi[j] = (phi[j] * (i - 1)) / i;
            }
        }
    }
}
  
// Function to return the required LCM sum
ll LcmSum(int m)
{
    ETF();
  
    for (int i = 1; i <= n; i++) {
  
        // Summation of d * ETF(d) where
        // d belongs to set of divisors of n
        for (int j = i; j <= n; j += i) {
            ans[j] += (i * phi[i]);
        }
    }
  
    ll answer = ans[m];
    answer = (answer + 1) * m;
    answer = answer / 2;
    return answer;
}
  
// Driver code
int main()
{
    int m = 5;
  
    cout << LcmSum(m);
  
    return 0;
}

chevron_right


Output:

55


My Personal Notes arrow_drop_up