Saturday, January 24, 2009

Erlang solution for Euler problem 20

Project Euler problem number 20 in which we are asked to find the sum of the digits of 100! is not particularly exiting, so here without further ado is my solution in Erlang:


%% Project Euler, problem 20
%%
%% Find the sum of digits in 100!

-module(euler20).
-author('Cayle Spandon').

-export([solve/0]).

fact(0) -> 1;
fact(N) -> N * fact(N-1).

sum_of_digits(N) when N < 10 -> N;
sum_of_digits(N) -> (N rem 10) + sum_of_digits(N div 10).

solve() ->
solve(100).

solve(N) ->
sum_of_digits(fact(N)).

No comments: