Ninjas on a Penny Farthing

  posted  

Erlang & Project Euler


Just a quick and fairly simple post. I've been reading Programming Erlang over the last few days and decided tonight to have a quick play with the Project Euler problems (having never done them before). Here ~10 minutes work :)

%% Erlang Solution to Project Euler Problem #2 %% "Sum of all even primes under one million" %% Written by Darcy Laycock %% http://blog.ninjahideout.com/ -module(pe). -compile(export_all). start() -> process(1000000). process(Max) -> io:format("Calculating All Even Fib. Under ~p~n", [Max]), evenFactorialsUnder(1, 1, Max, 0). printResults(0) -> io:format("No Known Results.~n"); printResults(Results) -> io:format("Found Result:~n~p~n", [Results]). evenFactorialsUnder(A, B, Max, Acc) when A+B > Max -> printResults(Acc); evenFactorialsUnder(A, B, Max, Acc) -> NewNum = A + B, case NewNum rem 2 of 0 -> %% even evenFactorialsUnder(NewNum, A, Max, Acc + NewNum); 1 -> %% odd evenFactorialsUnder(NewNum, A, Max, Acc) end.

Edit: More of these coming tonight :) Simple but fun!