Project Euler Problem 20

Factorial digit sum.

n! means n × (n − 1) × … × 3 × 2 × 1
For example, 10! = 10 × 9 × … × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

Find the sum of the digits in the number 100!

Link to original description
Source code examples on Github

Erlang version

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname p20
% vim:syn=erlang

-mode(compile).

main(_) ->
    io:format("Answer: ~p ~n", [lists:foldl(fun(E,A)-> A + E - 48 end, 
                                            0, integer_to_list(fac(100)))]).

fac(1) -> 1;
fac(N) -> N*fac(N-1).

Python version

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/python

import math

def digits(n):
    s = 0
    while n > 0:
        s = s + (n % 10)
        n = n / 10
    return s

print "Answer %s" % digits(math.factorial(100))

print "Answer (onliner) %s " % sum(map(int, str(math.factorial(100))))