Faffing round the edges Its easier than getting stuck in

28Dec/080

Problem 20: Sum of the digits in 100!

This is so similar to problem 16. The problem is:

n! means n (n 1) ... 3 2 1

Find the sum of the digits in the number 100!

Which is cool 'cos I have learned what n! means. My code is:

prob20(N) ->
	L = lists:seq(N, 1, -1),
	NBang = lists:foldl(fun(X, Acc) -> Acc * X end, 1, L),
	sum_the_digits(NBang).

sum_the_digits(N) ->
	L = integer_to_list(N),
	lists:foldl(fun(X, Acc) -> Acc + list_to_integer([X]) end, 0, L).

I reuse the sum_the_digits function from problem 16. These recent problems have caused me nothing like the trouble of the first 10. Either I am over some initial erlang/fp/maths hump or they admins have the difficulty order wrong.

Oh yeah. I find that I am using lists:foldl all the time.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

You must be logged in to post a comment.

No trackbacks yet.

 

December 2008
M T W T F S S
« Nov   Mar »
1234567
891011121314
15161718192021
22232425262728
293031  

Archives