📜  LCM(1,n),LCM(2,n),LCM(3,n),…,LCM(n,n)的总和

📅  最后修改于: 2021-04-22 01:19:32             🧑  作者: Mango

给定整数n ,任务是求和:

例子:

天真的方法:两个数字ab的LCM = (a * b)/ gcd(a,b)其中gcd(a,b)ab的最大公约数。

  • 计算从(1,n)(n,n)的所有对的单个LCM的值。
  • 对上一步的所有LCM结果求和。
  • 最后打印总和

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// return gcd of two numbers
static int gcd(int a,int b)
{
 
    // Everything divides 0
    if (a == 0)
        return b;
    if (b == 0)
        return a;
     
    // base case
    if (a == b)
        return a;
     
    // a is greater
    if (a > b)
        return gcd(a - b, b);
    return gcd(a, b - a);
 
}
 
// Function to calculate the required LCM sum
static int lcmSum(int n)
{
    int sum = 0;
 
    for (int i = 1; i <= n; i++)
    {
 
        // GCD of i and n
        int gcd = gcd(i, n);
 
        // LCM of i and n i.e. (i * n) / gcd(i, n)
        int lcm = (i * n) / gcd;
 
        // Update sum
        sum = sum + lcm;
    }
 
    return sum;
}
 
// Driver code
public static void main(String args[])
{
    int n = 3;
 
    System.out.println(lcmSum(n));
}
}
 
// This code is contributed by
// Surendra _Gangwar


Python3
# 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


C#
// C# implementation of the approach
class GFG
{
 
// return gcd of two numbers
static int gcd1(int a,int b)
{
 
    // Everything divides 0
    if (a == 0)
        return b;
    if (b == 0)
        return a;
     
    // base case
    if (a == b)
        return a;
     
    // a is greater
    if (a > b)
        return gcd1(a - b, b);
    return gcd1(a, b - a);
 
}
 
// Function to calculate the required LCM sum
static int lcmSum(int n)
{
    int sum = 0;
 
    for (int i = 1; i <= n; i++)
    {
 
        // GCD of i and n
        int gcd = gcd1(i, n);
 
        // LCM of i and n i.e. (i * n) / gcd(i, n)
        int lcm = (i * n) / gcd;
 
        // Update sum
        sum = sum + lcm;
    }
 
    return sum;
}
 
// Driver code
static void Main()
{
    int n = 3;
 
    System.Console.WriteLine(lcmSum(n));
}
}
 
// This code is contributed by chandan_jnu


PHP


Javascript


C++
// C++ implementation of the approach
#include 
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;
}


Java
// Java implementation of the approach
class GFG
{
     
static int n = 1000002;
 
static int[] phi = new int[n + 2];
static int[] ans = new int[n + 2];
 
// Euler totient Function
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;
            }
        }
    }
}
 
// Function to return the required LCM sum
static int 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]);
        }
    }
 
    int answer = ans[m];
    answer = (answer + 1) * m;
    answer = answer / 2;
    return answer;
}
 
// Driver code
public static void main (String[] args)
{
    int m = 5;
    System.out.println(LcmSum(m));
}
}
 
// This code is contributed by chandan_jnu


Python3
# Python3 implementation of the approach
n = 100002;
 
phi = [0] * (n + 2);
ans = [0] * (n + 2);
 
# Euler totient Function
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;
 
# Function to return the required LCM sum
def LcmSum(m):
    ETF();
 
    for i in range(1, n + 1):
 
        # Summation of d * ETF(d) where
        # d belongs to set of divisors of n
        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;
 
# Driver code
m = 5;
print(LcmSum(m));
 
# This code is contributed
# by chandan_jnu


C#
// C# implementation of the approach
using System;
 
class GFG
{
static int n = 1000002;
 
static int[] phi = new int[n + 2];
static int[] ans = new int[n + 2];
 
// Euler totient Function
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;
            }
        }
    }
}
 
// Function to return the required LCM sum
static int 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]);
        }
    }
 
    int answer = ans[m];
    answer = (answer + 1) * m;
    answer = answer / 2;
    return answer;
}
 
// Driver code
static void Main()
{
    int m = 5;
    Console.WriteLine(LcmSum(m));
}
}
 
// This code is contributed by chandan_jnu


PHP


Javascript


输出:
12

高效的方法:使用欧拉Totient函数,
∑LCM(i,n)=((∑(d * ETF(d))+1)* n)/ 2
其中ETF(d)dd的Euler函数属于组n的约数

例子:

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
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;
}

Java

// Java implementation of the approach
class GFG
{
     
static int n = 1000002;
 
static int[] phi = new int[n + 2];
static int[] ans = new int[n + 2];
 
// Euler totient Function
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;
            }
        }
    }
}
 
// Function to return the required LCM sum
static int 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]);
        }
    }
 
    int answer = ans[m];
    answer = (answer + 1) * m;
    answer = answer / 2;
    return answer;
}
 
// Driver code
public static void main (String[] args)
{
    int m = 5;
    System.out.println(LcmSum(m));
}
}
 
// This code is contributed by chandan_jnu

Python3

# Python3 implementation of the approach
n = 100002;
 
phi = [0] * (n + 2);
ans = [0] * (n + 2);
 
# Euler totient Function
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;
 
# Function to return the required LCM sum
def LcmSum(m):
    ETF();
 
    for i in range(1, n + 1):
 
        # Summation of d * ETF(d) where
        # d belongs to set of divisors of n
        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;
 
# Driver code
m = 5;
print(LcmSum(m));
 
# This code is contributed
# by chandan_jnu

C#

// C# implementation of the approach
using System;
 
class GFG
{
static int n = 1000002;
 
static int[] phi = new int[n + 2];
static int[] ans = new int[n + 2];
 
// Euler totient Function
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;
            }
        }
    }
}
 
// Function to return the required LCM sum
static int 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]);
        }
    }
 
    int answer = ans[m];
    answer = (answer + 1) * m;
    answer = answer / 2;
    return answer;
}
 
// Driver code
static void Main()
{
    int m = 5;
    Console.WriteLine(LcmSum(m));
}
}
 
// This code is contributed by chandan_jnu

的PHP


Java脚本


输出:
55