Problem 001: Multiples of 3 and 5

From Project Euler:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.
For this one, it's pretty simple just to iterate through all the numbers and add them up as needed (I use threading syntax here because I think it's more readable):
user=> (->> (range 1 1000)
            (filter #(or (== 0 (mod % 3)) (== 0 (mod % 5))))
            (reduce +))
233168
However we don't even need to loop. Since we care only about the sum and not the numbers themselves, we can apply a little bit of number theory. What we want is the sum of all the multiples of 3 or 5. This is equal to the sum of all the multiples of 3, plus the sum of all the multiples of 5, minus the sum of all the multiples of 15 to avoid double-counting.

We can rewrite the sum of the multiples of 3 from 1 to 999 (because we don't want to include 1000) as 3 times the sum of all numbers from 1 to 999 / 3, or 333. There's a formula to get the sum of numbers from 1 to n:
$$
\sum^n_{i=1} i = \frac{n(n+1)}{2}
$$
Which we can write in Clojure like this:
(defn sumto [n] (quot (* n (+ n 1)) 2))
To get the sum of the multiples of 3 from 1 to 1000, we can just write:
(* 3 (sumto (quot 999 3)))
Then to get the final result, it's simply a matter of combining everything together:
user=> (+ (* 3 (sumto (quot 999 3)))
          (* 5 (sumto (quot 999 5)))
          (- (* 15 (sumto (quot 999 15)))))
233168

No comments:

Post a Comment