📜  求一个数的倍数之和 - C# 代码示例

📅  最后修改于: 2022-03-11 14:48:47.251000             🧑  作者: Mango

代码示例1
// C# program to find sum of multiples
// of a number up to N efficiently
using System;
 
class GFG {
 
    // Function for calculating sum
    // of multiples of a upto N
    static int calculate_sum(int a, int N)
    {
 
        // Number of multiples
        int m = N / a;
 
        // sum of first m natural numbers
        int sum = m * (m + 1) / 2;
 
        // sum of multiples
        int ans = a * sum;
 
        return ans;
    }
 
    // Driver code
    public static void Main()
    {
 
        int a = 7, N = 49;
        Console.WriteLine("Sum of multiples of " + a +
         " up to " + N + " = " + calculate_sum(a, N));
    }
}