Project Euler

PUZZLE   0001

Multiples of 3 or 5

Project Euler · Problem 1

Among the natural numbers below 10, the multiples of 3 or 5 are 3,5,6, and 9. Their sum is 23.

Find the sum of all multiples of 3 or 5 below 1000.

An ordinary loop works. The more interesting solution asks how to remove the loop entirely.

Hints

Open one at a time

Sum multiples of 3 and multiples of 5 separately.

Correct for numbers counted twice.

Solution

Best opened after a real attempt

Arithmetic series and inclusion–exclusion

The sum of positive multiples of k below a limit N is

km(m+1)2,m=N1k.

Add the multiples of 3 and 5, then subtract the multiples of 15 that were counted twice:

33333342+519920021566672=233168.

This small problem introduces a durable habit: identify overlap explicitly before adding counts or sums.