Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

find sum of multiples of a number up to N

// 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));
    }
}
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #find #sum #multiples #number #N
ADD COMMENT
Topic
Name
1+6 =