Problem 50

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?

[code=ruby]require ‘prime’
Primes = Prime.take(78499) # do miliona

dupa = []
p,c,i = 0,0,0
loop do
p += Primes[i]
dupa << [p, i-c + 1] if p.prime?

i += 1
if p > 1_000_000
p = 0
c += 1
i = c
end

break if c > Math::PI
end

p dupa.max_by {|l| l.last} #=> [997651, 543][/code]
]