Given two integers n and k, consider all multiples of k that are less than or equal to n. Return the sum of these multiples.
Examples:
Input: n = 5, k = 2
Output: 6
Explanation: The multiples of 2 that do not exceed 5 are 2 and 4. Their sum is 6.Input: n = 5, k = 3
Output: 3
Explanation: The only multiple of 3 that does not exceed 5 is 3.
Table of Content
[Naive Approach] Check Every Number - O(n) Time and O(1) Space
The idea is to traverse all numbers from 1 to n and check whether each number is divisible by k. If a number is a multiple of k, add it to the answer. After checking every number, return the accumulated sum.
Working of Approach:
- Initialize a variable sum to store the answer.
- Traverse every number from 1 to n.
- For each number, check if it is divisible by k using the modulo operator.
- If it is divisible, add it to sum.
- Return the final value of sum.
#include <iostream>
using namespace std;
int multipleSum(int n, int k)
{
// Stores the sum of all multiples of k.
int sum = 0;
// Traverse all numbers from 1 to n.
for (int i = 1; i <= n; i++)
{
// If i is a multiple of k, add it to the sum.
if (i % k == 0)
{
sum += i;
}
}
// Return the final sum.
return sum;
}
int main()
{
int n = 5, k = 3;
cout << multipleSum(n, k);
return 0;
}
public class GFG {
static int multipleSum(int n, int k)
{
// Stores the sum of all multiples of k.
int sum = 0;
// Traverse all numbers from 1 to n.
for (int i = 1; i <= n; i++) {
// If i is a multiple of k, add it to the sum.
if (i % k == 0) {
sum += i;
}
}
// Return the final sum.
return sum;
}
public static void main(String[] args)
{
int n = 5, k = 3;
System.out.println(multipleSum(n, k));
}
}
def multipleSum(n, k):
# Stores the sum of all multiples of k.
sum = 0
# Traverse all numbers from 1 to n.
for i in range(1, n + 1):
# If i is a multiple of k, add it to the sum.
if i % k == 0:
sum += i
# Return the final sum.
return sum
if __name__ == "__main__":
n, k = 5, 3
print(multipleSum(n, k))
using System;
class GFG {
static int multipleSum(int n, int k)
{
// Stores the sum of all multiples of k.
int sum = 0;
// Traverse all numbers from 1 to n.
for (int i = 1; i <= n; i++) {
// If i is a multiple of k, add it to the sum.
if (i % k == 0) {
sum += i;
}
}
// Return the final sum.
return sum;
}
static void Main()
{
int n = 5, k = 3;
Console.WriteLine(multipleSum(n, k));
}
}
function multipleSum(n, k)
{
// Stores the sum of all multiples of k.
let sum = 0;
// Traverse all numbers from 1 to n.
for (let i = 1; i <= n; i++) {
// If i is a multiple of k, add it to the sum.
if (i % k === 0) {
sum += i;
}
}
// Return the final sum.
return sum;
}
// Driver Code
let n = 5, k = 3;
console.log(multipleSum(n, k));
Output
3
[Expected Approach] Using Arithmetic Progression Formula - O(1) Time and O(1) Space
The idea is to observe that all multiples of k up to n form an arithmetic progression: k, 2k, 3k, ..., cnt × k, where cnt = n / k. Instead of iterating through all numbers, compute the sum directly using the arithmetic progression formula. To reduce the risk of integer overflow during multiplication, divide either cnt or cnt + 1 by 2 before multiplying.
Working of Approach:
- Compute the number of multiples as cnt = n / k.
- The required sum is k × cnt × (cnt + 1) / 2.
- Since one of cnt or cnt + 1 is always even, divide the even value by 2 first.
- This keeps intermediate multiplication smaller and reduces the risk of integer overflow.
- Return the computed sum.
Let us understand with an example:
Input: n = 5, k = 3
- First, compute the number of multiples of 3 not exceeding 5: cnt = 5 / 3 = 1.
- Since cnt is odd, use the formula cnt × ((cnt + 1) / 2) × k.
- The sum becomes 1 × ((1 + 1) / 2) × 3 = 1 × 1 × 3 = 3.
- The only multiple of 3 that is less than or equal to 5 is 3.
- Hence, the required sum of all multiples is 3.
#include <iostream>
using namespace std;
int multipleSum(int n, int k)
{
int cnt = n / k;
int res;
// Calculate the sum of all multiples of k up to n.
if (cnt % 2 == 0)
{
res = (cnt / 2) * (cnt + 1) * k;
}
else
{
res = cnt * ((cnt + 1) / 2) * k;
}
return res;
}
int main()
{
int n = 5, k = 3;
cout << multipleSum(n, k);
return 0;
}
public class GFG {
int multipleSum(int n, int k)
{
int cnt = n / k;
int res;
// Calculate the sum of all multiples of k up to n.
if (cnt % 2 == 0) {
res = (cnt / 2) * (cnt + 1) * k;
}
else {
res = cnt * ((cnt + 1) / 2) * k;
}
return res;
}
public static void main(String[] args)
{
GFG obj = new GFG();
int n = 5, k = 3;
System.out.println(obj.multipleSum(n, k));
}
}
def multipleSum(n, k):
cnt = n // k
res = 0
# Calculate the sum of all multiples of k up to n.
if cnt % 2 == 0:
res = (cnt // 2) * (cnt + 1) * k
else:
res = cnt * ((cnt + 1) // 2) * k
return res
if __name__ == "__main__":
n, k = 5, 3
print(multipleSum(n, k))
using System;
class GFG {
static int multipleSum(int n, int k)
{
int cnt = n / k;
int res;
// Calculate the sum of all multiples of k up to n.
if (cnt % 2 == 0) {
res = (cnt / 2) * (cnt + 1) * k;
}
else {
res = cnt * ((cnt + 1) / 2) * k;
}
return res;
}
static void Main()
{
int n = 5, k = 3;
Console.WriteLine(multipleSum(n, k));
}
}
function multipleSum(n, k)
{
let cnt = Math.floor(n / k);
let res;
// Calculate the sum of all multiples of k up to n.
if (cnt % 2 === 0) {
res = (cnt / 2) * (cnt + 1) * k;
}
else {
res = cnt * ((cnt + 1) / 2) * k;
}
return res;
}
// Driver Code
let n = 5, k = 3;
console.log(multipleSum(n, k));
Output
3