Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add #50

  • Loading branch information...
commit cfec9ae5b1989545263bb97c9f0f13f1744088c9 1 parent 6b6f67c
@mconigliaro authored
Showing with 28 additions and 0 deletions.
  1. +28 −0 lib/project-euler/solutions/050.rb
View
28 lib/project-euler/solutions/050.rb
@@ -0,0 +1,28 @@
+def solution_50
+ max = 1000000
+ primes = primes_up_to(max)
+ primes_length = primes.length
+
+ last = 0
+ sum = 0
+ until sum > max
+ last += 1
+ sum = primes[0, last].inject(0) { |memo,obj| memo + obj }
+ end
+
+ sum = 0
+ until sum.prime?
+ offset = 0
+ until offset + last > primes_length - 1 || sum.prime?
+ sum = primes[offset...last].inject(0) { |memo,obj| memo + obj }
+ sum > max ? break : offset += 1
+ end
+ last -= 1
+ end
+
+ sum
+end
+
+def answer_50
+ 997651
+end
Please sign in to comment.
Something went wrong with that request. Please try again.