PUZZLE 0001
Multiples of 3 or 5
Project Euler · Problem 1
Among the natural numbers below 10, the multiples of 3 or 5 are
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 timeSum multiples of 3 and multiples of 5 separately.
Correct for numbers counted twice.
Solution
Best opened after a real attemptArithmetic series and inclusion–exclusion
The sum of positive multiples of
Add the multiples of 3 and 5, then subtract the multiples of 15 that were counted twice:
This small problem introduces a durable habit: identify overlap explicitly before adding counts or sums.