📌  相关文章
📜  从1到N的数字之和,可以被3或4整除

📅  最后修改于: 2021-04-21 21:42:15             🧑  作者: Mango

给定数字N。任务是找到所有从1到N的,可被3或4整除的数字之和。

例子

Input : N = 5
Output : 7
sum = 3 + 4

Input : N = 12 
Output : 42
sum = 3 + 4 + 6 + 8 + 9 + 12

方法:要解决此问题,请执行以下步骤:

  1. 找到可被3整除的数字之和,直到N。用S1表示。
  2. 查找可被4整除的数字之和,直到N。用S2表示。
  3. 找出可被12(3 * 4)整除的数字之和,直到N。用S3表示。
  4. 最终答案将是S1 + S2 – S3

为了找到总和,我们可以使用AP的一般公式:

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

Where,
n -> total number of terms
a -> first term
d -> common difference

对于S1:可被3除以N的总数将为N / 3,而序列将为3、6、9、12 …。

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

对于S2:将被4除以N的总数将为N / 4,而序列将为4、8、12、16…..

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

对于S3:可被12除以N的总数为N / 12。

Hence, 
S3 = ((N/12)/2) * (2 * 12 + (N/12 - 1) * 12)

因此,结果将是:

S = S1 + S2 - S3

下面是上述方法的实现:

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


Java
// Java program to find sum of numbers from 1 to N 
// which are divisible by 3 or 4 
class GFG{
  
// Function to calculate the sum 
// of numbers divisible by 3 or 4 
static int sum(int N) 
{ 
    int S1, S2, S3; 
  
    S1 = ((N / 3)) * (2 * 3 + (N / 3 - 1) * 3) / 2; 
    S2 = ((N / 4)) * (2 * 4 + (N / 4 - 1) * 4) / 2; 
    S3 = ((N / 12)) * (2 * 12 + (N / 12 - 1) * 12) / 2; 
  
    return S1 + S2 - S3; 
} 
  
// Driver code 
 public static void main (String[] args) {
    int N = 20; 
  
    System.out.print(sum(12)); 
}
  
}


Python3
# Python3 program to find sum of numbers 
# from 1 to N
# which are divisible by 3 or 4
  
# Function to calculate the sum 
# of numbers divisible by 3 or 4 
def sum(N):
  
    global S1,S2,S3
  
    S1 = (((N // 3)) * 
         (2 * 3 + (N //3 - 1) * 3) //2)
    S2 = (((N // 4)) * 
         (2 * 4 + (N // 4 - 1) * 4) // 2)
    S3 = (((N // 12)) * 
         (2 * 12 + (N // 12 - 1) * 12) // 2)
  
    return int(S1 + S2 - S3)
  
if __name__=='__main__':
    N = 12
    print(sum(N))
  
# This code is contributed by Shrikant13


C#
// C# program to find sum of 
// numbers from 1 to N which 
// are divisible by 3 or 4 
using System;
  
class GFG
{
  
// Function to calculate the sum 
// of numbers divisible by 3 or 4 
static int sum(int N) 
{ 
    int S1, S2, S3; 
  
    S1 = ((N / 3)) * (2 * 3 + 
          (N / 3 - 1) * 3) / 2; 
    S2 = ((N / 4)) * (2 * 4 + 
          (N / 4 - 1) * 4) / 2; 
    S3 = ((N / 12)) * (2 * 12 + 
          (N / 12 - 1) * 12) / 2; 
  
    return S1 + S2 - S3; 
} 
  
// Driver code 
public static void Main () 
{
    int N = 20; 
  
    Console.WriteLine(sum(12)); 
}
} 
  
// This code is contributed
// by inder_verma


PHP


输出:
42