Finding sum of multiples in JavaScript

In JavaScript, finding the sum of multiples of a given number within a specific range is a common programming problem. We need to identify all numbers divisible by the input number and calculate their sum using loops and conditional checks.

Understanding the Problem

The task is to find the sum of all multiples of a given number within a specified range. A multiple is any number that can be divided evenly by the given number (remainder is zero).

For example: if we have number 3 and range 1 to 12, the multiples of 3 are: 3, 6, 9, 12. The sum would be 3 + 6 + 9 + 12 = 30.

Algorithm

Step 1: Create a function that accepts three parameters: the target number, starting range, and ending range.

Step 2: Initialize a sum variable to store the result.

Step 3: Loop through all numbers from start to end of the range.

Step 4: Check if each number is divisible by the target number using the modulus operator (%).

Step 5: Add qualifying multiples to the sum and return the final result.

Basic Implementation

function sumOfMultiples(number, startingRange, endingRange) {
   let sum = 0;

   for (let i = startingRange; i <= endingRange; i++) {
      if (i % number === 0) {
         sum += i;
      }
   }

   return sum;
}

// Example usage
const number = 3;
const startingRange = 1;
const endingRange = 15;

const result = sumOfMultiples(number, startingRange, endingRange);
console.log(`Sum of multiples of ${number} from ${startingRange} to ${endingRange}: ${result}`);
Sum of multiples of 3 from 1 to 15: 45

Optimized Approach

Instead of checking every number, we can start from the first multiple and increment by the target number:

function optimizedSumOfMultiples(number, startingRange, endingRange) {
   let sum = 0;
   
   // Find the first multiple within range
   let firstMultiple = Math.ceil(startingRange / number) * number;
   
   // Add all multiples within range
   for (let i = firstMultiple; i <= endingRange; i += number) {
      sum += i;
   }
   
   return sum;
}

// Compare both approaches
const num = 4;
const start = 5;
const end = 25;

console.log("Basic approach:", sumOfMultiples(num, start, end));
console.log("Optimized approach:", optimizedSumOfMultiples(num, start, end));
Basic approach: 60
Optimized approach: 60

Multiple Numbers Example

Finding sum of multiples of multiple numbers (like 3 or 5):

function sumOfMultipleNumbers(numbers, startingRange, endingRange) {
   let sum = 0;
   let counted = new Set();
   
   for (let i = startingRange; i <= endingRange; i++) {
      for (let number of numbers) {
         if (i % number === 0 && !counted.has(i)) {
            sum += i;
            counted.add(i);
            break;
         }
      }
   }
   
   return sum;
}

// Sum of multiples of 3 or 5 between 1 and 20
const result = sumOfMultipleNumbers([3, 5], 1, 20);
console.log("Sum of multiples of 3 or 5:", result);
Sum of multiples of 3 or 5: 98

Complexity Analysis

Approach Time Complexity Space Complexity
Basic Loop O(n) O(1)
Optimized O(n/k) O(1)
Multiple Numbers O(n × m) O(n)

Where n is the range size, k is the target number, and m is the count of numbers to check.

Conclusion

Finding sum of multiples is efficiently solved using loops and modulus operations. The optimized approach reduces iterations by jumping between multiples, while the Set-based solution handles multiple target numbers without double-counting.

Updated on: 2026-03-15T23:19:00+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements