Project Euler

Problem #50

The prime 41, can be written as the sum of six consecutive primes:

41 = 2 + 3 + 5 + 7 + 11 + 13

This is the longest sum of consecutive primes that adds to a prime below one-hundred.

The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

Which prime, below one-million, can be written as the sum of the most consecutive primes?

Erlang: Running time = 8.65s
+%echo

+%prime_list

p50()->
	prime_list() ! {all_below,1000000,self()},
	put(primes,receive {primes_below,1000000,X}->X end),
	L=p50(initial,get(primes),0,0),
	put(prime_set,sets:from_list(get(primes))),
	p50(L).
p50(Length)->
	{First,Rest}=lists:split(Length,get(primes)),
	case p50(search,Rest,queue:from_list(First),lists:sum(First)) of
		{found,Ans}->
			io:format("~w~n",[Ans]);
		false->
			p50(Length-1)
	end.

p50(search,_,_,Total)when Total >= 1000000->false;
p50(search,[NextPrime|Others],Current,Total)->
	case sets:is_element(Total,get(prime_set)) of
		true->
			{found,Total};
		false->
			{{value,Pop},Future1}=queue:out(Current),
			Future2=queue:in(NextPrime,Future1),
			p50(search,Others,Future2,Total-Pop+NextPrime)
	end;

p50(initial,[NextPrime|_],Sum,L) when Sum+NextPrime >= 1000000 -> L;
p50(initial,[NextPrime|Others],Sum,L)->p50(initial,Others,Sum+NextPrime,L+1).

Ruby: Running time = 0.67s
+#PrimeList

def p50
  ans=0
  length=21
  while true
    length+=1
    break if (res=(0...length).map{|i|PrimeList.get(i)}.sum) >= 1000000
    ans=res if(PrimeList.isPrime? res)
    next if length%2==0
    start=1
    until (res+=(PrimeList.get(length+start-1)-PrimeList.get(start-1))) >= 1000000
      if(PrimeList.isPrime? res)
	ans=res
	break
      end
      start+=1
    end
  end
  puts ans
end