Skip to content

Instantly share code, notes, and snippets.

@klang
Last active June 7, 2016 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klang/88be106286dbd6c9c896f33f64902f5e to your computer and use it in GitHub Desktop.
Save klang/88be106286dbd6c9c896f33f64902f5e to your computer and use it in GitHub Desktop.
Project Euler - Problem 1 - https://projecteuler.net/problem=1
(* Ocaml *)
let rec range ?(start=0) len =
if start >= len
then []
else start :: (range len ~start:(start+1))
List.fold_left (+) 0 (List.map (fun x -> if 0 = x mod 3 or 0 = x mod 5 then x else 0) (range 1000))
List.fold_left (+) 0 (List.filter (fun x -> 0 = x mod 3 or 0 = x mod 5) (range 1000))
;; Clojure
(reduce + (map #(if (or (= 0 (mod % 5)) (= 0 (mod % 3))) % 0) (range 1000)))
(reduce + (filter #(or (= 0 (mod % 5)) (= 0 (mod % 3))) (range 1000)))
-- Haskell
foldr (+) 0 (map (\x -> if 0==mod x 3 || 0 == mod x 5 then x else 0) [0..1000])
foldr (+) 0 (filter (\x -> 0==mod x 3 || 0 == mod x 5) [0..1000])
import java.util.stream.IntStream;
public class Problem001 {
public static void main(String[] args) {
final int s = IntStream.range(0,1000).filter(x -> x%3==0 || x%5==0 ).reduce(0,(acc,x)->acc+x);
System.out.println(s);
}
}
use List::Util qw(reduce);
$a = reduce {$a + $b} grep { $_ % 3 == 0 || $_ % 5 == 0} 0 .. 1000;
$b += $_ for grep { $_ % 3 == 0 || $_ % 5 == 0} 0 .. 1000;
print $a.$/.$b;
# Python
reduce(lambda x, y: x+y,map(lambda x : x if 0==x%3 or 0==x%5 else 0, range(0,1000)))
reduce(lambda x, y: x+y, filter(lambda x : 0==x%3 or 0==x%5, range(0,1000)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment