Created
May 4, 2014 21:21
-
-
Save jamesmichiemo/849c2d90685526b5cdbf to your computer and use it in GitHub Desktop.
Project Euler: problem 2 http://projecteuler.net/problem=2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| puts 'Enter maximum range of Fibonacci sequence:' | |
| num = gets.chomp.to_i | |
| def fib(num) | |
| sum = 0· | |
| first = 0 | |
| second = 1 | |
| even = 0 | |
| (1..num).each do |i| | |
| sum = first + second | |
| #puts "fib: #{sum}" | |
| even += sum if (sum%2 == 0) | |
| #puts "even sum: #{even}" | |
| first = second | |
| second = sum | |
| end | |
| puts "The sum of even-valued Fibonacci numbers up to #{num} is equal to #{even}." | |
| end | |
| fib(num) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment