mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 22:46:20 +08:00
Add fibonnaci sequence closed form function (#130)
* Add fibonnaci sequence closed form function * Revert package-lock.json
This commit is contained in:
parent
dea368cb16
commit
a23400380d
@ -0,0 +1,23 @@
|
||||
import fibonacciClosedForm from '../fibonacciClosedForm';
|
||||
|
||||
describe('fibonacciClosedForm', () => {
|
||||
it('should calculate fibonacci correctly', () => {
|
||||
expect(fibonacciClosedForm(1)).toBe(1);
|
||||
expect(fibonacciClosedForm(2)).toBe(1);
|
||||
expect(fibonacciClosedForm(3)).toBe(2);
|
||||
expect(fibonacciClosedForm(4)).toBe(3);
|
||||
expect(fibonacciClosedForm(5)).toBe(5);
|
||||
expect(fibonacciClosedForm(6)).toBe(8);
|
||||
expect(fibonacciClosedForm(7)).toBe(13);
|
||||
expect(fibonacciClosedForm(8)).toBe(21);
|
||||
expect(fibonacciClosedForm(20)).toBe(6765);
|
||||
expect(fibonacciClosedForm(30)).toBe(832040);
|
||||
expect(fibonacciClosedForm(50)).toBe(12586269025);
|
||||
expect(fibonacciClosedForm(70)).toBe(190392490709135);
|
||||
expect(fibonacciClosedForm(71)).toBe(308061521170129);
|
||||
expect(fibonacciClosedForm(72)).toBe(498454011879264);
|
||||
expect(fibonacciClosedForm(73)).toBe(806515533049393);
|
||||
expect(fibonacciClosedForm(74)).toBe(1304969544928657);
|
||||
expect(fibonacciClosedForm(75)).toBe(2111485077978050);
|
||||
});
|
||||
});
|
11
src/algorithms/math/fibonacci/fibonacciClosedForm.js
Normal file
11
src/algorithms/math/fibonacci/fibonacciClosedForm.js
Normal file
@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Calculate fibonacci number at specific position using closed form function.
|
||||
*
|
||||
* @param n n-th number of fibonacci sequence (must be number from 1(inclusive) to 75(inclusive))
|
||||
* @return {number}
|
||||
*/
|
||||
export default function fibonacciClosedForm(n) {
|
||||
const sqrt5 = Math.sqrt(5);
|
||||
const phi = (1 + sqrt5) / 2;
|
||||
return Math.floor((phi ** n) / sqrt5 + 0.5);
|
||||
}
|
Loading…
Reference in New Issue
Block a user