Skip to content

Instantly share code, notes, and snippets.

@juanm55
Created February 21, 2012 00:34
Show Gist options
  • Save juanm55/1872564 to your computer and use it in GitHub Desktop.
Save juanm55/1872564 to your computer and use it in GitHub Desktop.
fibonacci :D, in ruby, trying to make it smaller and lighter
def fibonacci(n)
a = [1,1]
(n-2).times{ a[a.index(a.min)] = a[0]+a[1]}
a.max
end
def fibonacci(n, a = [1,1])
(n-2).times{ a[a.index(a.min)] = a[0]+a[1]}
a.max
end
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
def fibonacci(n, a=[1,1])
(n-2).times{ a = [a.max, a.max + a.min]}[1]
end
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
@juanm55
Copy link
Author

juanm55 commented Feb 21, 2012

I want to make it in one line, yet readable (no ; )....

fibonacci1 good for starters....

@juanm55
Copy link
Author

juanm55 commented Feb 21, 2012

saved a line in the second one, plus generalized for any seed on the fibonacci-like series

@juanm55
Copy link
Author

juanm55 commented Feb 21, 2012

fibo3 => looks better,,, longer but... better....

fibo4,,, refactored.....

@juanm55
Copy link
Author

juanm55 commented Feb 21, 2012

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