def fib_up_to(max) i1, i2 = 1, 1 while i1 <= max yield i1 if block_given? i1, i2 = i2, i1 + i2 end end puts "The Fibonacci sequence, up to 1000:" fib_up_to(1000) { |f| print f, " " } puts sum = 0 fib_up_to(1000) { |f| sum += f } puts "The sum of the Fibonacci numbers less than 1000 is #{sum}"