📜  可被X或Y整除的前N个自然数之和

📅  最后修改于: 2021-05-31 19:55:59             🧑  作者: Mango

给定数字N。给定两个数字XY ,任务是找到所有可以被X或Y整除的从1到N的数字之和。
例子

Input : N = 20
Output : 98

Input : N = 14 
Output : 45

方法:要解决此问题,请执行以下步骤:
->查找可被X整除到N的数字之和。用S1表示。
->查找可被Y整除到N的数字之和。用S2表示。
->查找可被X和Y(X * Y)整除到N的数字总和。用S3表示。
->最终答案将是S1 + S2 – S3
为了找到总和,我们可以使用AP的一般公式:

Sn = (n/2) * {2*a + (n-1)*d}

对于S1 :X可以除以N的总数为N / X,总和为:

Hence, 
S1 = ((N/X)/2) * (2 * X + (N/X - 1) * X)

对于S2 :可被Y整除到N的总数为N / Y,总和为:

Hence, 
S2 = ((N/Y)/2) * (2 * Y + (N/Y - 1) * Y)

对于S3 :可被X和Y整除到N的总数为N /(X * Y),总和为:

Hence, 
S2 = ((N/(X*Y))/2) * (2 * Y + (N/(X*Y) - 1) * (X*Y))

因此,结果将是:

S = S1 + S2 - S3

下面是上述方法的实现:

C++
// C++ program to find sum of numbers from
// 1 to N which are divisible by X or Y
#include 
using namespace std;
 
// Function to calculate the sum
// of numbers divisible by X or Y
int sum(int N, int X, int Y)
{
    int S1, S2, S3;
 
    S1 = ((N / X)) * (2 * X + (N / X - 1) * X) / 2;
    S2 = ((N / Y)) * (2 * Y + (N / Y - 1) * Y) / 2;
    S3 = ((N / (X * Y))) * (2 * (X * Y)
                      + (N / (X * Y) - 1) * (X * Y))/ 2;
 
    return S1 + S2 - S3;
}
 
// Driver code
int main()
{
    int N = 14;
    int X = 3, Y = 5;
 
    cout << sum(N, X, Y);
 
    return 0;
}


Java
// Java program to find sum of numbers from
// 1 to N which are divisible by X or Y
 
public class GFG{
     
    // Function to calculate the sum
    // of numbers divisible by X or Y
    static int sum(int N, int X, int Y)
    {
        int S1, S2, S3;
     
        S1 = ((N / X)) * (2 * X + (N / X - 1) * X) / 2;
        S2 = ((N / Y)) * (2 * Y + (N / Y - 1) * Y) / 2;
        S3 = ((N / (X * Y))) * (2 * (X * Y)
                          + (N / (X * Y) - 1) * (X * Y))/ 2;
     
        return S1 + S2 - S3;
    }
     
    // Driver code
    public static void main(String []args)
    {
        int N = 14;
        int X = 3, Y = 5;
     
        System.out.println(sum(N, X, Y));
     
    }
    // This code is contributed by Ryuga
}


Python3
# Python 3 program to find sum of numbers from
# 1 to N which are divisible by X or Y
from math import ceil, floor
 
# Function to calculate the sum
# of numbers divisible by X or Y
def sum(N, X, Y):
    S1 = floor(floor(N / X) * floor(2 * X +
               floor(N / X - 1) * X) / 2)
    S2 = floor(floor(N / Y)) * floor(2 * Y +
               floor(N / Y - 1) * Y) / 2
    S3 = floor(floor(N / (X * Y))) * floor (2 * (X * Y) +
               floor(N / (X * Y) - 1) * (X * Y))/ 2
 
    return S1 + S2 - S3
 
# Driver code
if __name__ == '__main__':
    N = 14
    X = 3
    Y = 5
 
    print(int(sum(N, X, Y)))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find sum of numbers from
// 1 to N which are divisible by X or Y
  
using System;
public class GFG{
      
    // Function to calculate the sum
    // of numbers divisible by X or Y
    static int sum(int N, int X, int Y)
    {
        int S1, S2, S3;
      
        S1 = ((N / X)) * (2 * X + (N / X - 1) * X) / 2;
        S2 = ((N / Y)) * (2 * Y + (N / Y - 1) * Y) / 2;
        S3 = ((N / (X * Y))) * (2 * (X * Y)
                          + (N / (X * Y) - 1) * (X * Y))/ 2;
      
        return S1 + S2 - S3;
    }
      
    // Driver code
    public static void Main()
    {
        int N = 14;
        int X = 3, Y = 5;
      
        Console.Write(sum(N, X, Y));
      
    }
     
}


PHP


Javascript


输出:
45

时间复杂度: O(1)