📜  A和B的倍数之和小于N

📅  最后修改于: 2021-04-29 07:43:22             🧑  作者: Mango

给定数字N,任务是找到N以下的A和B的所有倍数之和。
例子:

Input:N = 11, A= 8, B= 2
Output: Sum = 30
Multiples of 8 less than 11 is 8 only.
Multiples of 2 less than 11 is 2, 4, 6, 8, 10 and their sum is 30.
As 8 is common in both so it is counted only once.

Input: N = 100, A= 5, B= 10
Output: Sum = 950

一个幼稚的方法是迭代1到A,然后找到A和B的倍数,然后将它们相加。在循环末尾显示总和。
高效的方法:由于A的倍数将形成AP系列a,2a,3a…。
和B组成AP系列b,2b,3b…
在将这两个系列的和相加后,我们将得到两个数字的倍数之和,但是可能会有一些公倍数,因此,通过减去lcm(A,B)的倍数,从这两个系列的和中删除重复项。因此,减去lcm(A,B)的序列。
因此,A和B的倍数小于N的总和Sum(A)+ Sum(B)-Sum(1cm(A,B))
下面是上述方法的实现:

C++
// CPP program to find the sum of all
// multiples of A and B below N
#include 
using namespace std;
#define ll long long int
 
// Function to find sum of AP series
ll sumAP(ll n, ll 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
ll sumMultiples(ll A, ll B, ll n)
{
    // Since, we need the sum of
    // multiples less than N
    n--;
 
    // common factors of A and B
    ll common = (A * B) / __gcd(A, B);
 
    return sumAP(n, A) + sumAP(n, B) - sumAP(n, common);
}
 
// Driver code
int main()
{
    ll n = 100, A = 5, B = 10;
 
    cout << "Sum = " << sumMultiples(A, B, n);
 
    return 0;
}


Java
// Java program to find the sum of all
// multiples of A and B below N
 
class GFG{
 
static int __gcd(int a, int b)
    {
      if (b == 0)
        return a;
      return __gcd(b, a % b); 
    }
     
// Function to find sum of AP series
static int sumAP(int n, int 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
static int sumMultiples(int A, int B, int n)
{
    // Since, we need the sum of
    // multiples less than N
    n--;
 
    // common factors of A and B
    int common = (A * B) / __gcd(A,B);
 
    return sumAP(n, A) + sumAP(n, B) - sumAP(n, common);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 100, A = 5, B = 10;
 
    System.out.println("Sum = "+sumMultiples(A, B, n));
}
}
// this code is contributed by mits


Python3
# Python 3 program to find the sum of
# all multiples of A and B below N
from math import gcd,sqrt
 
# Function to find sum of AP series
def sumAP(n, d):
     
    # Number of terms
    n = int(n / d)
 
    return (n) * (1 + n) * d / 2
 
# Function to find the sum of all
# multiples of A and B below N
def sumMultiples(A, B, n):
     
    # Since, we need the sum of
    # multiples less than N
    n -= 1
 
    # common factors of A and B
    common = int((A * B) / gcd(A, B))
 
    return (sumAP(n, A) + sumAP(n, B) -
            sumAP(n, common))
 
# Driver code
if __name__ == '__main__':
    n = 100
    A = 5
    B = 10
 
    print("Sum =", int(sumMultiples(A, B, n)))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find the sum of all
// multiples of A and B below N
 
class GFG{
 
static int __gcd(int a, int b)
    {
    if (b == 0)
        return a;
    return __gcd(b, a % b);
    }
     
// Function to find sum of AP series
static int sumAP(int n, int 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
static int sumMultiples(int A, int B, int n)
{
    // Since, we need the sum of
    // multiples less than N
    n--;
 
    // common factors of A and B
    int common = (A * B) / __gcd(A,B);
 
    return sumAP(n, A) + sumAP(n, B) - sumAP(n, common);
}
 
// Driver code
public static void Main()
{
    int n = 100, A = 5, B = 10;
 
    System.Console.WriteLine("Sum = "+sumMultiples(A, B, n));
}
}
// this code is contributed by mits


PHP


Javascript


输出:
Sum = 950