Created
February 21, 2012 00:34
-
-
Save juanm55/1872564 to your computer and use it in GitHub Desktop.
fibonacci :D, in ruby, trying to make it smaller and lighter
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
def fibonacci(n) | |
a = [1,1] | |
(n-2).times{ a[a.index(a.min)] = a[0]+a[1]} | |
a.max | |
end |
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
def fibonacci(n, a = [1,1]) | |
(n-2).times{ a[a.index(a.min)] = a[0]+a[1]} | |
a.max | |
end |
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
def fibo_sum(a=[1,1]) | |
[a.max, a.max + a.min] | |
end | |
def fibonacci(n) | |
(n-2).times{ a = fibo_sum(a)}.max | |
end |
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
def fibonacci(n, a=[1,1]) | |
(n-2).times{ a = [a.max, a.max + a.min]}[1] | |
end |
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
def fibonacci(n, a=[1,1]) # where n is the index of number of the fibonacci you want... 1=> 1, 2=> 1, 3 => 2... | |
(n-2).times{ a.replace([a.max, a.max + a.min])}[1] | |
end |
fibonacci5,,, easier on the garbage collector and less memory consumption thanks to replace
and avoided using max
to select result because i think [1]
is more efficient!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fibo3 => looks better,,, longer but... better....
fibo4,,, refactored.....