📌  相关文章
📜  最多可被2或5整除的N之和

📅  最后修改于: 2021-05-04 16:46:59             🧑  作者: Mango

给定数字n。任务是找到最多为n的数字之和,它们可以被2或5整除。
例子:

Input: n = 2
Output: 2

Input: n = 5
Output: 11

天真的方法是将数字迭代到n,然后检查它是否可以被2或5整除。如果可以将其整除,则只需将此数字加到所需的总和中即可。最后,我们得到了总和为O(n)的复杂度。
高效方法:

1.首先找到可被2整除的数字。因此,对于AP,这些数字具有

    \begin{align*} Sum=\frac{n*\left(2*a+(n-1)*d\right)}{2}\\ \end{align*} put the value, we got \begin{align*} Sum_2=\frac{\frac{n}{2}*\left(4+\left(\frac{n}{2}-1\right)*2\right)}{2}\\ \end{align*}

2.其次,我们找到可被5整除的数字。因此,对于AP,这些数字具有

    \begin{align*} Sum=\frac{n*\left(2*a+(n-1)*d\right)}{2}\\ \end{align*} put the value, we got \begin{align*} Sum_5=\frac{\frac{n}{5}*\left(10+\left(\frac{n}{5}-1\right)*5\right)}{2}\\ \end{align*}

3.首先,我们找到可被2和5.整除的数字,对于AP,这些数字具有

    \begin{align*} Sum=\frac{n*\left(2*a+(n-1)*d\right)}{2}\\ \end{align*} put the value, we got \begin{align*} Sum_{10}=\frac{\frac{n}{10}*\left(20+\left(\frac{n}{10}-1\right)*10\right)}{2}\\ \end{align*}

4.由于我们必须找到可被2或5整除的数的总和,因此,所需的总和由下式给出:

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
#define ll long long int
using namespace std;
 
// Function to find the sum
ll findSum(int n)
{
 
    ll sum2, sum5, sum10;
 
    // sum2 is sum of numbers divisible by 2
    sum2 = ((n / 2) * (4 + (n / 2 - 1) * 2)) / 2;
 
    // sum5 is sum of number divisible by 5
    sum5 = ((n / 5) * (10 + (n / 5 - 1) * 5)) / 2;
 
    // sum10 of numbers divisible by 2 and 5
    sum10 = ((n / 10) * (20 + (n / 10 - 1) * 10)) / 2;
 
    return sum2 + sum5 - sum10;
}
 
// Driver code
int main()
{
    int n = 5;
 
    cout << findSum(n) << endl;
    return 0;
}


Java
// Java implementation of
// above approach
import java.lang.*;
import java.util.*;
 
class GFG
{
 
// Function to find the sum
static long findSum(int n)
{
    long sum2, sum5, sum10;
     
    // sum2 is sum of numbers
    // divisible by 2
    sum2 = ((n / 2) * (4 +
            (n / 2 - 1) * 2)) / 2;
     
    // sum5 is sum of number
    // divisible by 5
    sum5 = ((n / 5) * (10 +
            (n / 5 - 1) * 5)) / 2;
     
    // sum10 of numbers divisible
    // by 2 and 5
    sum10 = ((n / 10) * (20 +
             (n / 10 - 1) * 10)) / 2;
     
    return sum2 + sum5 - sum10;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 5;
    System.out.println(findSum(n));
}
}
 
// This code is contributed by Raj


Python3
# Python3 implementation of
# above approach
 
# Function to find the sum
def findSum(n):
 
     
    # sum2 is sum of numbers divisible by 2
    sum2 = ((n // 2) * (4 + (n // 2 - 1) * 2)) // 2
 
    # sum5 is sum of number divisible by 5
    sum5 = ((n // 5) * (10 + (n // 5 - 1) * 5)) // 2
 
    # sum10 of numbers divisible by 2 and 5
    sum10 = ((n // 10) * (20 + (n // 10 - 1) * 10)) // 2
 
    return sum2 + sum5 - sum10;
 
 
# Driver code
if __name__=='__main__':
    n = 5
    print (int(findSum(n)))
     
 
# this code is contributed by Shivi_Aggarwal


C#
// C# implementation of
// above approach
using System;
 
class GFG
{
 
// Function to find the sum
static long findSum(int n)
{
    long sum2, sum5, sum10;
     
    // sum2 is sum of numbers
    // divisible by 2
    sum2 = ((n / 2) * (4 +
            (n / 2 - 1) * 2)) / 2;
     
    // sum5 is sum of number
    // divisible by 5
    sum5 = ((n / 5) * (10 +
            (n / 5 - 1) * 5)) / 2;
     
    // sum10 of numbers divisible
    // by 2 and 5
    sum10 = ((n / 10) * (20 +
             (n / 10 - 1) * 10)) / 2;
     
    return sum2 + sum5 - sum10;
}
 
// Driver code
public static void Main ()
{
    int n = 5;
    Console.WriteLine(findSum(n));
}
}
 
// This code is contributed by inder_verma


PHP


Javascript


输出:
11