Skip to content

Instantly share code, notes, and snippets.

@jamesmichiemo
Created May 4, 2014 21:21
Show Gist options
  • Select an option

  • Save jamesmichiemo/849c2d90685526b5cdbf to your computer and use it in GitHub Desktop.

Select an option

Save jamesmichiemo/849c2d90685526b5cdbf to your computer and use it in GitHub Desktop.
Project Euler: problem 2 http://projecteuler.net/problem=2
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