Sum of multiples of a number up to N
Given a number a and limit N. Find the sum of multiple of a upto N.
Examples :
Input : a = 4, N = 23 Output : sum = 60 [Multiples : 4, 8, 12, 16, 20] Input :a = 7, N = 49 Output :sum = 196 [Multiples: 7, 14, 21, 28, 35, 42, 49]
The basic idea is to iterate from i = a to i = n, i++ and check whether i % a == 0 or not.If zero then add i to sum(initially sum = 0).Thus we will get the sum.It will take O(n) time.
We can modify the loop as i = a, i <= n, i = i + a to reduce the number of iterations.But it will also take O(m) time if there is m multiples of a.
To get the result in O(1) time we can use the formula of summation of n natural numbers.For the above example,
a = 4 and N = 23, number of multiples of a, m = N/a(integer division). The multiples are 4, 8, 12, 16, 20.
We can write it as 4 X [1, 2, 3, 4, 5]. So we can get the sum of multiples as:
sum = a * (Summation of 1 to m [natural numbers from 1 to m]) sum = 4 * (m*(m+1) / 2) sum = 4 * (5*6 / 2) = 4 * 15 = 60
C++
// C++ program to find sum of multiples of a number // up to N efficiently #include <iostream> using namespace std; // Function for calculating sum of multiples of // a upto N 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 int main() { int a = 7, N = 49; cout << "Sum of multiples of " << a << " up to " << N << " = " << calculate_sum(a, N) << endl; return 0; } |
Java
// Java program to find sum of multiples // of a number up to N efficiently 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(String[] args) { int a = 7 , N = 49 ; System.out.println( "Sum of multiples of " + a + " up to " + N + " = " + calculate_sum(a, N)); } } // This code is contributed by Anant Agarwal. |
Python3
"""Python program to find sum of multiples of a number up to N""" # Calculates sum of multiples of # a number upto N def calculate_sum(a, N): # Number of multiples m = N / a # sum of first m natural numbers sum = m * (m + 1 ) / 2 # sum of multiples ans = a * sum print ( "Sum of multiples of " , a, " up to " , N, " = " , ans) # Driver Code calculate_sum( 7 , 49 ) # This code is contributed by Abhishek Agrawal. |
C#
// 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)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find sum // of multiples of a number // up to N efficiently // Function for calculating sum // of multiples of a upto N function calculate_sum( $a , $N ) { // Number of multiples $m = $N / $a ; // sum of first m // natural numbers $sum = $m * ( $m + 1) / 2; // sum of multiples $ans = $a * $sum ; return $ans ; } // Driver code $a = 7; $N = 49; echo "Sum of multiples of " . $a , " up to " . $N . " = " . calculate_sum( $a , $N ) ; // This code is contributed by Sam007 ?> |
Javascript
<script> // Javascript program to find sum // of multiples of a number // up to N efficiently // Function for calculating sum // of multiples of a upto N function calculate_sum(a, N) { // Number of multiples m = N / a; // Sum of first m // natural numbers sum = m * (m + 1) / 2; // Sum of multiples ans = a * sum; return ans; } // Driver code let a = 7; let N = 49; document.write( "Sum of multiples of " + a + " up to " + N + " = " + calculate_sum(a, N)); // This code is contributed by mohan1240760 </script> |
Output :
Sum of multiples of 7 upto 49 = 196
Time complexity: O(1)
Auxiliary space: O(1)
This article is contributed by Sukanta Nandi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...