fibonacci-fast
A fast node.js module for working with the Fibonacci sequence
Install
$ npm install fibonacci-fast
Usage
Documentation can be found here
fibonacci.get(k)
Get the kth index of the Fibonacci sequence
var fibonacci = ;var result = fibonacci;
The result
will be a JavaScript object
containing the following:
number: ... next: ... index: 3000 ms: 21
Where number
is a big number representing Fibonacci[3000]
and next
is also a big number representing Fibonacci[3001]. The ms
is the amount of time (in milliseconds) that it took to get the result.
fibonacci.find(num)
Find the index of a given Fibonacci number
var result = fibonacci;var index = resultindex; // index = 7
The result
will be the same form as the result of the fibonacci.get(n)
method, so the index
can be retrieved easily with result.index
. This also means that the number
and next
values are returned in the result
variable as well.
fibonacci.is(num)
Checks if a given number is a Fibonacci number
fibonacci; // truefibonacci; // false
fibonacci.iterator([k, n])
Create a Fibonacci sequence iterator. Where k
and n
are optional parameters defining the starting index of the iterator and the max number of iterations respectively. If k
is not given, then the iterator will start at index 0. If n
is not given the iterator will allow infinite iterations.
With starting index and limit
var fibs = fibonacci; for let fib of fibs console;
The result:
F[5] = 5F[6] = 8F[7] = 13F[8] = 21F[9] = 34
No starting index or limit
var fibs = fibonacci; for let i = 0; i < 5; i++ var fib = fibsnextvalue; console;
The result:
F[0] = 0F[1] = 1F[2] = 1F[3] = 2F[4] = 3
fibonacci.array(k0, k1)
Grab an array of the Fibonacci sequence. Where k0
is the starting index, and k1
is the ending index.
NOTE: the number at
k1
will not be included in thearray
var array = fibonacci;
The result will be an array of objects with the following form:
number: ... next: ... index: ...
In order to get an array containing the actual Fibonacci numbers and not the objects, you could map the array like so:
var array = fibonacci;
and the resulting array would look like this:
'5' '8' '13' '21' '34' '55'
Fast Doubling Algorithm
This module utilizes the fast doubling algorithm which allows for much faster calculations of Fibonacci numbers than the traditional recurrence method.
The fast doubling algorithm takes Θ(log n) operations, and the recurrence algorithm takes Θ(n).
Big.js
The fibonacci-fast module utilizes the big.js library to handle large numbers.