Project Euler Problem 16

Power digit sum

2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 2^1000?

Link to original description
Source code examples on Github

Erlang solution

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

-mode(compile).

main(_) ->
    io:format("Answer: ~p ~n", [test16(1000)]).

test16(N) ->
    lists:foldl(fun(E,A)-> A + list_to_integer([E]) end, 0, integer_to_list(round(math:pow(2,N)))).


Python solution

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/python
def digits(n):
    s = 0
    while n > 0:
        s = s + (n % 10)
        n = n / 10
    return s

print digits(pow(2,1000))