source/euler50.rb
def longest_consecutive_prime_under(max)
  primes = (1..max).select {|n| n.prime?}

  length = 1
  candidate = 2
  while length < (primes.length / 2)
    index = 0
    sum = primes[index..length].inject(:+)
    until primes.include?(sum) or sum > max or index + length > primes.length / 2
      index += 1
      sum = primes[index..(index + length)].inject(:+)
    end
    if primes.include?(sum) and sum < max
      # p primes[index..(index + length)]
      candidate = sum 
    end
    length += 1
  end
  candidate
end
# Eh, this was a bit of a stretch for a monkey patch, even for me
Eh, this was a bit of a stretch for a monkey patch, even for me
Eh this was a bit of a stretch for a monkey patch even for me
def longest_consecutive_prime_under(max)
  primes = (1..max).select {|n| n.prime?}
  length = 1
  candidate = 2
  while length < (primes.length / 2)
    index = 0
    sum = primes[index..length].inject(:+)
    until primes.include?(sum) or sum > max or index + length > primes.length / 2
      index += 1
      sum = primes[index..(index + length)].inject(:+)
    end
    if primes.include?(sum) and sum < max
      candidate = sum 
    end
    length += 1
  end
  candidate
end
def longest_consecutive_prime_under ( max ) primes = ( 1 .. max ) . select { | n | n . prime? } length = 1 candidate = 2 while length < ( primes . length / 2 ) index = 0 sum = primes [ index .. length ] . inject ( :+ ) until primes . include? ( sum ) or sum > max or index + length > primes . length / 2 index += 1 sum = primes [ index .. ( index + length ) ] . inject ( :+ ) end if primes . include? ( sum ) and sum < max candidate = sum end length += 1 end candidate end
Eh, this was a bit of a stretch for a monkey patch, even for me
Eh this was a bit of a stretch for a monkey patch even for me
# p primes[index..(index + length)]
max