Last active
September 22, 2018 10:11
-
-
Save kopylovvlad/76830f3b8d9085f533f06452b5ef4bf2 to your computer and use it in GitHub Desktop.
This file contains 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
# Fibonacci sequence with recursion | |
def fib(number) | |
if number < 0 | |
raise ArgumentError, 'fib(n) defined for number>=0' | |
end | |
return fib(number - 1) + fib(number - 2) if number > 1 | |
number | |
end | |
# Fibonacci sequence without recursion | |
def fib2(number) | |
raise ArgumentError, 'fib(n) defined for number>=0' if number < 0 | |
n0, n1 = 0, 1 | |
(0...number).each do | |
n0, n1 = n1, n0 + n1 | |
end | |
n0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment