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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll lcmSum( long long n)
{
ll sum = 0;
for ( long long int i = 1; i <= n; i++) {
long long int gcd = __gcd(i, n);
long long int lcm = (i * n) / gcd;
sum = sum + lcm;
}
return sum;
}
int main()
{
int n = 3;
cout << lcmSum(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
static int lcmSum( int n)
{
int sum = 0 ;
for ( int i = 1 ; i <= n; i++)
{
int gcd = gcd(i, n);
int lcm = (i * n) / gcd;
sum = sum + lcm;
}
return sum;
}
public static void main(String args[])
{
int n = 3 ;
System.out.println(lcmSum(n));
}
}
|
Python3
import math
def lcmSum(n):
Sum = 0
for i in range ( 1 , n + 1 ):
gcd = math.gcd(i, n)
lcm = (i * n) / / gcd
Sum = Sum + lcm
return Sum
if __name__ = = "__main__" :
n = 3
print (lcmSum(n))
|
C#
class GFG
{
static int gcd1( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd1(a - b, b);
return gcd1(a, b - a);
}
static int lcmSum( int n)
{
int sum = 0;
for ( int i = 1; i <= n; i++)
{
int gcd = gcd1(i, n);
int lcm = (i * n) / gcd;
sum = sum + lcm;
}
return sum;
}
static void Main()
{
int n = 3;
System.Console.WriteLine(lcmSum(n));
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $b == 0)
return $a ;
return __gcd( $b , $a % $b );
}
function lcmSum( $n )
{
$sum = 0;
for ( $i = 1; $i <= $n ; $i ++)
{
$gcd = __gcd( $i , $n );
$lcm = ( $i * $n ) / $gcd ;
$sum = $sum + $lcm ;
}
return $sum ;
}
$n = 3;
echo lcmSum( $n );
?>
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
function lcmSum(n)
{
var sum = 0;
for ( var i = 1; i <= n; i++)
{
var _gcd = gcd(i, n);
var lcm = (i * n) / _gcd;
sum = sum + lcm;
}
return sum;
}
var n = 3;
document.write(lcmSum(n));
</script>
|
Time Complexity: O(n * logn), where n represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
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(5) + 1) * 5) / 2 = 55
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define n 1000002
#define ll long long int
ll phi[n + 2], ans[n + 2];
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;
}
}
}
}
ll LcmSum( int m)
{
ETF();
for ( int i = 1; i <= n; i++) {
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;
}
int main()
{
int m = 5;
cout << LcmSum(m);
return 0;
}
|
Java
class GFG
{
static int n = 1000002 ;
static int [] phi = new int [n + 2 ];
static int [] ans = new int [n + 2 ];
static 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;
}
}
}
}
static int LcmSum( int m)
{
ETF();
for ( int i = 1 ; i <= n; i++)
{
for ( int j = i; j <= n; j += i)
{
ans[j] += (i * phi[i]);
}
}
int answer = ans[m];
answer = (answer + 1 ) * m;
answer = answer / 2 ;
return answer;
}
public static void main (String[] args)
{
int m = 5 ;
System.out.println(LcmSum(m));
}
}
|
Python3
n = 100002 ;
phi = [ 0 ] * (n + 2 );
ans = [ 0 ] * (n + 2 );
def ETF():
for i in range ( 1 , n + 1 ):
phi[i] = i;
for i in range ( 2 , n + 1 ):
if (phi[i] = = i):
phi[i] = i - 1 ;
for j in range ( 2 * i, n + 1 , i):
phi[j] = (phi[j] * (i - 1 )) / / i;
def LcmSum(m):
ETF();
for i in range ( 1 , n + 1 ):
for j in range (i, n + 1 , i):
ans[j] + = (i * phi[i]);
answer = ans[m];
answer = (answer + 1 ) * m;
answer = answer / / 2 ;
return answer;
m = 5 ;
print (LcmSum(m));
|
C#
using System;
class GFG
{
static int n = 1000002;
static int [] phi = new int [n + 2];
static int [] ans = new int [n + 2];
static 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;
}
}
}
}
static int LcmSum( int m)
{
ETF();
for ( int i = 1; i <= n; i++)
{
for ( int j = i; j <= n; j += i)
{
ans[j] += (i * phi[i]);
}
}
int answer = ans[m];
answer = (answer + 1) * m;
answer = answer / 2;
return answer;
}
static void Main()
{
int m = 5;
Console.WriteLine(LcmSum(m));
}
}
|
PHP
<?php
$n = 10002;
$phi = array_fill (0, $n + 2, 0);
$ans = array_fill (0, $n + 2, 0);
function ETF()
{
global $phi , $n ;
for ( $i = 1; $i <= $n ; $i ++)
{
$phi [ $i ] = $i ;
}
for ( $i = 2; $i <= $n ; $i ++)
{
if ( $phi [ $i ] == $i )
{
$phi [ $i ] = $i - 1;
for ( $j = 2 * $i ; $j <= $n ; $j += $i )
{
$phi [ $j ] = (int)(( $phi [ $j ] *
( $i - 1)) / $i );
}
}
}
}
function LcmSum( $m )
{
ETF();
global $ans , $n , $phi ;
for ( $i = 1; $i <= $n ; $i ++)
{
for ( $j = $i ; $j <= $n ; $j += $i )
{
$ans [ $j ] += ( $i * $phi [ $i ]);
}
}
$answer = $ans [ $m ];
$answer = ( $answer + 1) * $m ;
$answer = (int)( $answer / 2);
return $answer ;
}
$m = 5;
echo LcmSum( $m );
?>
|
Javascript
<script>
var n = 1000002;
var phi = Array(n + 2).fill(0);
var ans = Array(n + 2).fill(0);
function ETF() {
for (i = 1; i <= n; i++) {
phi[i] = i;
}
for (i = 2; i <= n; i++) {
if (phi[i] == i) {
phi[i] = i - 1;
for (j = 2 * i; j <= n; j += i) {
phi[j] = (phi[j] * (i - 1)) / i;
}
}
}
}
function LcmSum(m) {
ETF();
for (i = 1; i <= n; i++) {
for (j = i; j <= n; j += i) {
ans[j] += (i * phi[i]);
}
}
var answer = ans[m];
answer = (answer + 1) * m;
answer = answer / 2;
return answer;
}
var m = 5;
document.write(LcmSum(m));
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(N)
Last Updated :
21 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...