# Project Euler Problem 1 Solution

## Question

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.

## C

#include <stdio.h>

int main(int argc, char **argv)
{
int sum = 0;
for (int i = 0; i < 1000; i++) {
if (i % 3 == 0 || i % 5 == 0) {
sum += i;
}
}
printf("%d\n", sum);
return 0;
}
$gcc -march=native -Ofast -std=c11 -o natural natural.c$ time ./natural
real   0m0.001s
user   0m0.000s
sys    0m0.001s

## Clojure

#!/usr/bin/env clojure
(defn multiple? [n]
(or (= (rem n 3) 0) (= (rem n 5) 0)))

(println (reduce + (filter multiple? (range 1000))))
$time ./natural real 0m0.001s user 0m0.000s sys 0m0.001s ## Haskell main :: IO () main = print$ sum [n | n <- [1..999], or [(n mod 3 == 0), (n mod 5 == 0)]]
$ghc -O2 -o natural natural.hs$ time ./natural
real   0m0.002s
user   0m0.000s
sys    0m0.002s

## JavaScript

let s = 0
for (var i = 1; i < 1000; i++) {
if (i % 3 === 0 || i % 5 === 0) {
s += i
}
}
console.log(s)
$time node --use-strict natural.js real 0m0.052s user 0m0.044s sys 0m0.009s ## Python #!/usr/bin/env python print(sum(i for i in range(1000) if (i % 3 == 0) or (i % 5 == 0))) $ time python3 natural.py
real   0m0.016s
user   0m0.016s
sys    0m0.000s

## Ruby

#!/usr/bin/env ruby
sum = 0
1000.times do |i|
if i % 3 == 0 or i % 5 == 0
sum += i
end
end
puts sum
$time ./natural real 0m0.001s user 0m0.000s sys 0m0.001s ## Scheme (display (reduce + 0 (filter (lambda (n) (or (= (remainder n 3) 0) (= (remainder n 5) 0))) (iota 1000)))) (newline) $ time mit-scheme --quiet < natural.scm
real   0m0.040s
user   0m0.024s
sys    0m0.019s