📜  N以下两个数字的倍数之和

📅  最后修改于: 2021-06-25 23:33:18             🧑  作者: Mango

给定三个整数ABN。任务是找到N以下所有元素的总和,这些元素是AB的倍数。

例子:

天真的方法:

  • 初始化变量sum = 0
  • 对于每个i,0n循环检查i%A = 0i%B = 0
  • 如果满足上述条件,则更新sum = sum + i
  • 最后返回总和

下面是上述方法的实现:

C++
// C++ program to find the
// sum of all the integers
// below N which are multiples
// of either A or B
#include 
using namespace std;
 
// Function to return the
// sum of all the integers
// below N which are multiples
// of either A or B
int findSum(int n, int a, int b)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
 
        // If i is a multiple of a or b
        if (i % a == 0 || i % b == 0)
            sum += i;
 
    return sum;
}
 
// Driver code
int main()
{
    int n = 10, a = 3, b = 5;
    cout << findSum(n, a, b);
    return 0;
}


C
// C program for above approach
#include 
 
// Function to return the
// sum of all the integers
// below N which are multiples
// of either A or B
int findSum(int n, int a, int b)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
 
        // If i is a multiple of a or b
        if (i % a == 0 || i % b == 0)
            sum += i;
 
    return sum;
}
 
// Driver Code
int main()
{
      int n = 10, a = 3, b = 5;
    printf("%d",findSum(n, a, b));
    return 0;
}
 
//This code is contributed by Shivshanker Singh


Java
// Java program to find the
// sum of all the integers
// below N which are multiples
// of either A or B
 
import java.io.*;
 
class GFG
{
 
    // Function to return the
    // sum of all the integers
    // below N which are multiples
    // of either A or B
    static int findSum(int n, int a, int b)
    {
        int sum = 0;
        for (int i = 0; i < n; i++)
 
            // If i is a multiple of a or b
            if (i % a == 0 || i % b == 0)
                sum += i;
 
        return sum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 10, a = 3, b = 5;
        System.out.println(findSum(n, a, b));
    }
}
// This code is contributed by anuj_67..


Python3
# Python 3 program to find the sum of
# all the integers below N which are
# multiples of either A or B
 
# Function to return the sum of all
# the integers below N which are
# multiples of either A or B
def findSum(n, a, b):
    sum = 0
    for i in range(0, n, 1):
         
        # If i is a multiple of a or b
        if (i % a == 0 or i % b == 0):
            sum += i
 
    return sum
 
# Driver code
if __name__ == '__main__':
    n = 10
    a = 3
    b = 5
    print(findSum(n, a, b))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find the sum
// of all the integers
// below N which are multiples
// of either A or B
using System;
 
class GFG
{
 
    // Function to return the sum
    // of all the integers
    // below N which are multiples
    // of either A or B
    static int findSum(int n, int a, int b)
    {
        int sum = 0;
        for (int i = 0; i < n; i++)
     
            // If i is a multiple of a or b
            if (i % a == 0 || i % b == 0)
                sum += i;
     
        return sum;
    }
 
 
    // Driver code
    static void Main()
    {
        int n = 10, a = 3, b = 5;
        Console.WriteLine(findSum(n, a, b));
    }
    // This code is contributed by Ryuga
}


PHP


Javascript


C++
// C++ program to find the
// sum of all the integers
// below N which are multiples
// of either A or B
#include 
#include 
using namespace std;
 
// Function to find sum of AP series
long long sumAP(long long n, long long d)
{
    // Number of terms
    n /= d;
 
    return (n) * (1 + n) * d / 2;
}
 
// Function to find the sum of all
// multiples of a and b below n
long long sumMultiples(long long n, long long a,
                                    long long b)
{
     
    // Since, we need the sum of
    // multiples less than N
    n--;
    long lcm = (a*b)/__gcd(a,b);
    return sumAP(n, a) + sumAP(n, b) -
                        sumAP(n, lcm);
}
 
// Driver code
int main()
{
    long long n = 10, a = 3, b = 5;
 
    cout << sumMultiples(n, a, b);
 
    return 0;
}
// This code is Modified by Shivshanker Singh.


C
// C program to find the
// sum of all the integers
// below N which are multiples
// of either A or B
#include 
 
// Function to find sum of AP series
long long sumAP(long long n, long long d)
{
    // Number of terms
    n /= d;
 
    return (n) * (1 + n) * d / 2;
}
 
// Function to find the gcd of A and B.
long gcd(int p, int q)
{
    if (p == 0)
        return q;
    return gcd(q % p, p);
}
 
// Function to find the sum of all
// multiples of a and b below n
long long sumMultiples(long long n, long long a,
                                   long long b)
{
    // Since, we need the sum of
    // multiples less than N
    n--;
    long lcm = (a*b)/gcd(a,b);
    return sumAP(n, a) + sumAP(n, b) - sumAP(n, lcm);
}
 
// Driver code
int main()
{
    long long n = 10, a = 3, b = 5;
 
    printf("%lld", sumMultiples(n, a, b));
 
    return 0;
}
// This code is Contributed by Shivshanker Singh.


Java
// Java  program to find the
// sum of all the integers
// below N which are multiples
// of either A or B
import java.io.*;
 
class GFG
{
 
    // Function to find sum of AP series
    static long sumAP(long n, long d)
    {
        // Number of terms
        n = (int)n / d;
 
        return (n) * (1 + n) * d / 2;
    }
   
    // Function to find gcd of A and B
    public static long gcd(long p, long q)
    {
        if (p == 0)
            return q;
        return gcd(q % p, p);
    }
   
    // Function to find the sum of all
    // multiples of a and b below n
    static long sumMultiples(long n, long a,
                                     long b)
    {
         
        // Since, we need the sum of
        // multiples less than N
        n--;
        long lcm = (a * b) / gcd(a, b);
        return sumAP(n, a) + sumAP(n, b) -
                              sumAP(n, lcm);
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        long n = 10, a = 3, b = 5;
 
        System.out.println(sumMultiples(n, a, b));
    }
    // This code is Modified by Shivshanker Singh.
}


Python3
import math
# Python3 program to find the sum of
# all the integers below N which are
# multiples of either A or B
 
# Function to find sum of AP series
def sumAP(n, d):
     
    # Number of terms
    n = n//d
 
    return (n) * (1 + n) * d // 2
 
# Function to find the sum of all
# multiples of a and b below n
def sumMultiples(n, a, b):
 
    # Since, we need the sum of
    # multiples less than N
    n = n-1
    lcm = (a*b)//math.gcd(a, b)
    return sumAP(n, a) + sumAP(n, b) - \
                         sumAP(n, lcm)
 
# Driver code
n = 10
a = 3
b = 5
print(sumMultiples(n, a, b))
 
# This code is Modified by Shivshanker Singh.


C#
// C#  program to find the
// sum of all the integers
// below N which are multiples
// of either A or B
using System;
 
public class GFG
{
 
    // Function to find sum of AP series
    static long sumAP(long n, long d)
    {
        // Number of terms
        n = (int)n / d;
 
        return (n) * (1 + n) * d / 2;
    }
   
    // Function to find gcd of A and B
    static long gcd(long p, long q)
    {
        if (p == 0)
            return q;
        return gcd(q % p, p);
    }
 
    // Function to find the sum of all
    // multiples of a and b below n
    static long sumMultiples(long n, long a,
                                     long b)
    {
         
        // Since, we need the sum of
        // multiples less than N
        n--;
        long lcm = (a * b) / gcd(a, b);
        return sumAP(n, a) + sumAP(n, b) -
                             sumAP(n, lcm);
    }
 
    // Driver code
    static public void Main()
    {
 
        long n = 10, a = 3, b = 5;
 
        Console.WriteLine(sumMultiples(n, a, b));
    }
    // This code is Modified by Shivshanker Singh.
}


PHP


Javascript


输出:
23

高效的方法:
为了更好地了解一种有效的方法,让我们从头开始-

我们有数字= 1,2,3,4,………。 ,N-1,N

所有可被A整除的数字= A,2A,3A,…………..⌊N/A⌋* A

让我们称之为sum1 = A + 2A + 3A +………….. +⌊N/A⌋* A

sum1 = A(1 + 2 + 3+………….. +⌊N/A⌋)

sum1 = A *⌊N/A⌋*(⌊N/A⌋+1)/ 2

其中是下限(或最小整数)函数。

并使用n个自然数公式n *(n + 1)/ 2的总和。

类似地,被B整除的数字之和–

sum2 = B *⌊N/B⌋*(⌊N/B⌋+ 1)/ 2

因此,总和= sum1 + sum2,但是可能会有两个数字在两者中是相同的,

例如让N = 10,A = 2,B = 3

然后sum1 = 2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20

sum2 = 3 + 6 + 9 + 12 + 15 + 18

我们可以清楚地看到数字6、12、18是重复的,所有其他数字都是6的倍数,即AB的LCM

lcm = A和B的LCM

sum3 = lcm *⌊N/lcm⌋*(⌊N/lcm⌋+1)/ 2

最后我们可以通过使用

sum = sum1 + sum2 – sum3

我们可以通过使用

lcm =(A * B)/ gcd

其中gcd = A和B的GCD

下面是上述方法的实现:

C++

// C++ program to find the
// sum of all the integers
// below N which are multiples
// of either A or B
#include 
#include 
using namespace std;
 
// Function to find sum of AP series
long long sumAP(long long n, long long d)
{
    // Number of terms
    n /= d;
 
    return (n) * (1 + n) * d / 2;
}
 
// Function to find the sum of all
// multiples of a and b below n
long long sumMultiples(long long n, long long a,
                                    long long b)
{
     
    // Since, we need the sum of
    // multiples less than N
    n--;
    long lcm = (a*b)/__gcd(a,b);
    return sumAP(n, a) + sumAP(n, b) -
                        sumAP(n, lcm);
}
 
// Driver code
int main()
{
    long long n = 10, a = 3, b = 5;
 
    cout << sumMultiples(n, a, b);
 
    return 0;
}
// This code is Modified by Shivshanker Singh.

C

// C program to find the
// sum of all the integers
// below N which are multiples
// of either A or B
#include 
 
// Function to find sum of AP series
long long sumAP(long long n, long long d)
{
    // Number of terms
    n /= d;
 
    return (n) * (1 + n) * d / 2;
}
 
// Function to find the gcd of A and B.
long gcd(int p, int q)
{
    if (p == 0)
        return q;
    return gcd(q % p, p);
}
 
// Function to find the sum of all
// multiples of a and b below n
long long sumMultiples(long long n, long long a,
                                   long long b)
{
    // Since, we need the sum of
    // multiples less than N
    n--;
    long lcm = (a*b)/gcd(a,b);
    return sumAP(n, a) + sumAP(n, b) - sumAP(n, lcm);
}
 
// Driver code
int main()
{
    long long n = 10, a = 3, b = 5;
 
    printf("%lld", sumMultiples(n, a, b));
 
    return 0;
}
// This code is Contributed by Shivshanker Singh.

Java

// Java  program to find the
// sum of all the integers
// below N which are multiples
// of either A or B
import java.io.*;
 
class GFG
{
 
    // Function to find sum of AP series
    static long sumAP(long n, long d)
    {
        // Number of terms
        n = (int)n / d;
 
        return (n) * (1 + n) * d / 2;
    }
   
    // Function to find gcd of A and B
    public static long gcd(long p, long q)
    {
        if (p == 0)
            return q;
        return gcd(q % p, p);
    }
   
    // Function to find the sum of all
    // multiples of a and b below n
    static long sumMultiples(long n, long a,
                                     long b)
    {
         
        // Since, we need the sum of
        // multiples less than N
        n--;
        long lcm = (a * b) / gcd(a, b);
        return sumAP(n, a) + sumAP(n, b) -
                              sumAP(n, lcm);
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        long n = 10, a = 3, b = 5;
 
        System.out.println(sumMultiples(n, a, b));
    }
    // This code is Modified by Shivshanker Singh.
}

Python3

import math
# Python3 program to find the sum of
# all the integers below N which are
# multiples of either A or B
 
# Function to find sum of AP series
def sumAP(n, d):
     
    # Number of terms
    n = n//d
 
    return (n) * (1 + n) * d // 2
 
# Function to find the sum of all
# multiples of a and b below n
def sumMultiples(n, a, b):
 
    # Since, we need the sum of
    # multiples less than N
    n = n-1
    lcm = (a*b)//math.gcd(a, b)
    return sumAP(n, a) + sumAP(n, b) - \
                         sumAP(n, lcm)
 
# Driver code
n = 10
a = 3
b = 5
print(sumMultiples(n, a, b))
 
# This code is Modified by Shivshanker Singh.

C#

// C#  program to find the
// sum of all the integers
// below N which are multiples
// of either A or B
using System;
 
public class GFG
{
 
    // Function to find sum of AP series
    static long sumAP(long n, long d)
    {
        // Number of terms
        n = (int)n / d;
 
        return (n) * (1 + n) * d / 2;
    }
   
    // Function to find gcd of A and B
    static long gcd(long p, long q)
    {
        if (p == 0)
            return q;
        return gcd(q % p, p);
    }
 
    // Function to find the sum of all
    // multiples of a and b below n
    static long sumMultiples(long n, long a,
                                     long b)
    {
         
        // Since, we need the sum of
        // multiples less than N
        n--;
        long lcm = (a * b) / gcd(a, b);
        return sumAP(n, a) + sumAP(n, b) -
                             sumAP(n, lcm);
    }
 
    // Driver code
    static public void Main()
    {
 
        long n = 10, a = 3, b = 5;
 
        Console.WriteLine(sumMultiples(n, a, b));
    }
    // This code is Modified by Shivshanker Singh.
}

的PHP

Java脚本


输出:
23

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。