Javatpoint Logo
Javatpoint Logo

Sum of LCM in Java

In the input, a number n is given. Our task is to find the sum of LCM of numbers from 1 to n with the number N, respectively. In other words, we have to find the value of lcm(1, n) + lcm(2, n) + lcm(3, n) + … + lcm(n, n).

Example 1:

Input

int n = 6

Output: 66

Explanation: The value of lcm(1, 6) + lcm(2, 6) + lcm(3, 6) + lcm(4, 6) + lcm(5, 6) + lcm(6, 6) = 6 + 6 + 6 + 12 + 30 + 6 = 66

Example 2:

Input

int n = 3

Output: 12

Explanation: The value of lcm(1, 3) + lcm(2, 3) + lcm(3, 3) = 3 + 6 + 3 = 12

Simple Approach

The simple approach is to run a loop and compute the LCM for each pair. The LCM of two numbers p and q (say) can be found by using a loop that runs from the maximum of p and q to p x q. In each iteration, we have to check whether the value pointed by the loop variable is completely divisible by p and q or not. If yes, we can terminate the loop using a break statement, and the value is our LCM of the numbers p and q. If not, then the loop continues. Observe the following program.

FileName: LCMSum.java

Output:

The sum of LCM of numbers for the input number: 6 is 66
 
 
The sum of LCM of numbers for the input number: 3 is 12

Complexity Analysis: There is a loop running from 1 to n, which takes O(n) time. In each iteration, we are invoking the method findLCM() that takes O(a * n), where a runs from 1 to n. Thus, the overall time complexity of the program is O(a * n2), where n is the input number. The space complexity of the program is O(1).

Now, let us reduce the time complexity of the program by doing some optimization.

Approach: Using Mathematical Formula

The basic mathematical formula of the product of two natural numbers, p and q, are:

p x q = HCF(p, q) x LCM(p, q). Hence, LCM(p, q) = (p x q) / HCF(p, q). The numbers p and q are already known. The only thing to find is the HCF (Highest Common Factor). HCF can be found using a loop, which runs from min(p, q) to 1. If the value pointed by the loop variable divides both the number completely, then that value is our HCF, and we can terminate the loop. After that, finding the LCM(p, q) is easy.

Another way to find the HCF is the Euclidean algorithm. We will be implementing both these ways to find HCF and, eventually, LCM. See the following program.

FileName: LCMSum1.java

Output:

The sum of LCM of numbers for the input number: 6 is 66
 
 
The sum of LCM of numbers for the input number: 3 is 12

Complexity Analysis: The method invoked at line 60 decides the time complexity of the program. In the program, the method findLCMUsingEuclidean() is invoked (at line 60). The time complexity of the method findLCMUsingEuclidean() is O(log(min(a, n))), where a ranges from 1 to n. Also, that method is invoked n times. Thus, the overall time complexity of the program is O(n x log(min(a, n))). If we use the method findLCMUsingMethod() (at line 60), then the time consumed by the method is O(min(a, n)). The overall time complexity of the program is O(n x log(min(a, n), where n is the input number.

Approach: Using Euler Totient Function

We will use the Euler Totient method for solving the problem.

The function Euler's Totient Φ(p) for an input 'p' is the number count in the list {1, 2, 3, …, p} that are relatively prime to p. In other words, the numbers whose HCF (Highest Common Factor) with p is equal to 1. Some examples are given below.

Example 1:

Φ(1) = 1

gcd(1, 1) is 1

Example 2:

Φ(4) = 2

gcd(1, 4) is 1 and gcd(3, 4) is 1

Example 3:

Φ(7) = 6

gcd(1, 7) is 1, gcd(2, 7) is 1,

gcd(3, 7) is 1 and gcd(4, 7) is 1

gcd(5, 7) is 1 and gcd(6, 7) is 1

Example 4:

Φ(8) = 4

gcd(1, 8) is 1 and gcd(3, 8) is 1

gcd(5, 8) is 1 and gcd(7, 8) is 1

For calculating Φ (p), one can use two approaches:

1) Using HCF

Run a loop from 1 to p, and check the HCF for each number with p. Whenever the HCF is 1, one can increment the value. The other approach is to use the Euler Formula

2) Using The Euler Product Formula

The formula tells that the value of Φ(p) is equal to p multiplied by the product of (1 - 1/t) for all prime factors t of N. For example, the value of:

Φ(12) = 12 * (1 - 1 / 2) * (1 - 1 / 3) = 4. Note that 2 and 3 are the prime factors of 12.

After computing the ETF (Euler Totient Function) for each number, we can use the following Euler's formula of LCM sum :

∑LCM(a, p) = ((∑(div * ETF(div)) + 1) * p) / 2

where ETF(div) is the Euler totient function of div and div is the set of divisors of p.

Let's verify with the help of an example.

Example

∑LCM(a, p) = LCM(1, 6) + LCM(2, 6) + LCM(3, 6) + LCM(4, 6) + LCM(5, 6) + LCM(6, 6) = 6 + 6 + 6 + 12 + 30 + 6 = 66, where a = 1, p = 6

Now, let's use Euler Totient Function:

All divisors of 6 are: {1, 2, 3, 6}

Hence, ((1 x ETF(1) + 2 x ETF(2) + 3 x ETF(3) + 6 x ETF(6) + 1) * 6) / 2 = ((1 + 2 + 6 + 12 + 1) x 6) / 2 = 132 / 2 = 66

We get the same result. Now comes the implementation part.

FileName: LCMSum2.java

Output:

The sum of LCM of numbers for the input number: 6 is 66
 
 
The sum of LCM of numbers for the input number: 3 is 12

Complexity Analysis: Finding the factor of a number n takes log(log(n)) time, and we are finding the factor of n numbers. Therefore, the overall time complexity of the program is O(n x log(log(n))), where n is the input number.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA