Created
November 1, 2012 01:15
-
-
Save emmasteimann/3991001 to your computer and use it in GitHub Desktop.
Nth fibonacci (incomplete)
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
# Given: 8 | |
# Possible solutions - | |
# a + b = given | |
# 7 - 1 = 6 | |
# 6 - 2 = 4 # <- subtraction should be less than either | |
# 5 - 3 = 2 | |
# 4 - 4 = 0 # <- but not equal to 0 | |
# | |
# Given: 13 | |
# Possible solutions - | |
# a + b = given | |
# 12 - 1 = 11 | |
# 11 - 2 = 9 | |
# 10 - 3 = 7 | |
# 9 - 4 = 5 | |
# 8 - 5 = 3 # <- subtraction should be less than either | |
# 7 - 6 = 1 | |
# | |
# So on the first time its less than both it the correct sequence | |
# 0 1 1 2 3 5 8 | |
def nth_fibonacci(fib_num, check_num = 0) | |
if fib_num == 0 | |
return fib_num | |
else | |
a = fib_num - 1 | |
b = check_num + 1 | |
if a < fib_num and b < fib_num | |
else | |
nth_fibonacci fib_num, check_num +1 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment