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 ?> |
Output :
Sum of multiples of 7 upto 49 = 196
This article is contributed by Sukanta Nandi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.
Recommended Posts:
- Nth number in a set of multiples of A , B or C
- Kth number from the set of multiples of numbers A, B and C
- Given a number n, count all multiples of 3 and/or 5 in set {1, 2, 3, ... n}
- Multiples of 3 or 7
- Sum of multiples of A and B less than N
- Sum of all the multiples of 3 and 7 below N
- Find the sum of all multiples of 2 and 5 below N
- Sum of the multiples of two numbers below N
- Count of Multiples of A ,B or C less than or equal to N
- Find Multiples of 2 or 3 or 5 less than or equal to N
- Queries for counts of multiples in an array
- Sort elements of the array that occurs in between multiples of K
- Count of common multiples of two numbers in a range
- Minimum sum after subtracting multiples of k from the elements of the array
- Count common elements in two arrays containing multiples of N and M
Improved By : Sam007



