Problem 1: Multiples of 3 or 5

If we list all natural numbers below that are multiples of 3 or 5, we obtain . Their sum is .

The general task asks for the sum of every number smaller than that is divisible by 3 or 5. This is the classic warm-up problem that teaches clean iteration and modular arithmetic.

How to Solve

Iterate through every integer satisfying and evaluate the divisibility test.

Add the number to a running sum when holds; otherwise skip it.

This single pass runs in time with only extra space, matching the straightforward implementation shown below.

Code Solution


function sumMultiplesOf3Or5(limit: number): number {
  let sum = 0;
  for (let i = 0; i < limit; i++) {
    if (i % 3 === 0 || i % 5 === 0) {
      sum += i;
    }
  }
  return sum;
}

Test the Solution