Project Euler

Problem #30

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 1^(4) + 6^(4) + 3^(4) + 4^(4)
8208 = 8^(4) + 2^(4) + 0^(4) + 8^(4)
9474 = 9^(4) + 4^(4) + 7^(4) + 4^(4)

As 1 = 1^(4) is not a sum it is not included.

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

Erlang: Running time = 1.56s
+%run_procs

+%digit_split

+%pow

p30()->
	Pows=list_to_tuple(lists:map(fun(X)->pow(X,5) end,lists:seq(0,9))),
	Limit=6*pow(9,5),
	Ans=run_procs(Limit div 1000,fun(I)->[euler,p30,[check,1000*I,1000*(I+1),0,Pows,self()]] end,
				     fun(A,{done,B})->A+B end,0),
	io:format("~w~n",[Ans]).
p30(check,On,On,Total,_,Parent)->Parent!{done,Total};
p30(check,On,Upto,Total,Pows,Parent)->
	Test=lists:sum(lists:map(fun(X)->element(X+1,Pows) end, digit_split(On))),
	if
		Test==On->
			p30(check,On+1,Upto,Total+On,Pows,Parent);
		true->
			p30(check,On+1,Upto,Total,Pows,Parent)
	end.

Ruby: Running time = 6.82s
+#Enumerable

def p30test(n)
  n==n.to_s.split("").map{|i|$fifths[i]}.sum
end

def p30
  $fifths={}
  (0..9).each{|i|$fifths[i.to_s]=i**5}
  puts (2..(6*(9**5))).select{|i|p30test i}.sum
end