Skip to content

Instantly share code, notes, and snippets.

@rjchicago
Created August 31, 2010 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjchicago/559680 to your computer and use it in GitHub Desktop.
Save rjchicago/559680 to your computer and use it in GitHub Desktop.
/** Fibonacci 0.0.1 */
/**
* Fibonacci
*
* @author Ryan Jones <rjchicago@gmail.com>
* @license The MIT license.
* @copyright Copyright (c) 2010 RJChicago <rjchicago@gmail.com>
*/
// Fibonacci sequence = 0, 1, 1, 2, 3, 5, 8, 13, 21...
// See Wikipedia for information about the Fibonacci number:
// http://en.wikipedia.org/wiki/Fibonacci_number
var Fibonacci = {
// Returns the Fibonacci value at the Nth index position.
GetAt: function (n) {
if (n < 2)
return n;
var nm1 = this.GetAt(n - 1);
var nm2 = this.GetAt(n - 2);
return nm1 + nm2;
},
// Returns an Array for the zero to Nth Fibonacci values.
GetTo: function (n) {
var _sequence = [];
for (var i = 0; i <= n; i++)
{
if (i < 2)
_sequence.Push[i];
else
_sequence.Push(sequence[i - 1] + _sequence[i - 2]);
}
return _sequence;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment