Skip to content

Instantly share code, notes, and snippets.

@bil-bas
Created October 17, 2012 02:32
Show Gist options
  • Save bil-bas/3903400 to your computer and use it in GitHub Desktop.
Save bil-bas/3903400 to your computer and use it in GitHub Desktop.
# List all values in fibonacci series <= n
def fib_up_to(n)
return [] if n < 0 # Idiot-proofing.
vals, succ = [0], 1
while succ <= n
vals << succ
succ = vals[-1] + vals[-2]
end
vals
end
(-1..25).each do |i|
puts "#{i}: #{fib_up_to i}"
end
=begin
-1: []
0: [0]
1: [0, 1, 1]
2: [0, 1, 1, 2]
3: [0, 1, 1, 2, 3]
4: [0, 1, 1, 2, 3]
5: [0, 1, 1, 2, 3, 5]
6: [0, 1, 1, 2, 3, 5]
7: [0, 1, 1, 2, 3, 5]
8: [0, 1, 1, 2, 3, 5, 8]
9: [0, 1, 1, 2, 3, 5, 8]
10: [0, 1, 1, 2, 3, 5, 8]
11: [0, 1, 1, 2, 3, 5, 8]
12: [0, 1, 1, 2, 3, 5, 8]
13: [0, 1, 1, 2, 3, 5, 8, 13]
14: [0, 1, 1, 2, 3, 5, 8, 13]
15: [0, 1, 1, 2, 3, 5, 8, 13]
16: [0, 1, 1, 2, 3, 5, 8, 13]
17: [0, 1, 1, 2, 3, 5, 8, 13]
18: [0, 1, 1, 2, 3, 5, 8, 13]
19: [0, 1, 1, 2, 3, 5, 8, 13]
20: [0, 1, 1, 2, 3, 5, 8, 13]
21: [0, 1, 1, 2, 3, 5, 8, 13, 21]
22: [0, 1, 1, 2, 3, 5, 8, 13, 21]
23: [0, 1, 1, 2, 3, 5, 8, 13, 21]
24: [0, 1, 1, 2, 3, 5, 8, 13, 21]
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment