Permalink
Browse files

Solved 2

  • Loading branch information...
1 parent 5013c90 commit 9a57a416dec98ad74c4847fe2f4b52103813ba2a @ndbroadbent committed Oct 12, 2010
Showing with 30 additions and 0 deletions.
  1. +30 −0 2/solution.rb
View
@@ -0,0 +1,30 @@
+#!/usr/bin/env ruby
+# -----------------------------------------
+# | A solution to a Project Euler problem |
+# -----------------------------------------
+require 'rubygems'
+problem_no = File.dirname(File.expand_path(__FILE__)).split('/').last
+puts "=== Solving Project Euler problem ##{problem_no}\n\n"
+question = %Q{
+
+Each new term in the Fibonacci sequence is generated by adding the previous two terms.
+By starting with 1 and 2, the first 10 terms will be:
+
+1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
+
+Find the sum of all the even-valued terms in the sequence which do not exceed four million.
+
+}
+puts "? #{question.strip}\n\n"
+# --------------------------------------------------------------------
+
+a = 0
+b = 1
+sum = 0
+begin
+ a, b = b, a + b
+ sum += b if b % 2 == 0 and b <= 4000000
+end while b <= 4000000
+
+puts sum
+

0 comments on commit 9a57a41

Please sign in to comment.