Skip to content

Instantly share code, notes, and snippets.

@jish
Created May 21, 2009 06:07
Show Gist options
  • Select an option

  • Save jish/115311 to your computer and use it in GitHub Desktop.

Select an option

Save jish/115311 to your computer and use it in GitHub Desktop.
# Loops are cool I guess...
def fib(num)
return 1 if num == 1
return 1 if num == 2
x = 1
y = 1
a = 0
(num - 2).times do
a = x + y
x = y
y = a
end
a
end
# Recursion works too
def fib(n)
return 1 if n == 1 || n == 2
fib(n - 2) + fib(n - 1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment