Skip to content

Commit 51d67e7

Browse files
committedSep 15, 2018
Add fibonacci Binet's formula.
1 parent a234003 commit 51d67e7

7 files changed

+58
-36
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ a set of rules that precisely define a sequence of operations.
6161
* **Math**
6262
* `B` [Bit Manipulation](src/algorithms/math/bits) - set/get/update/clear bits, multiplication/division by two, make negative etc.
6363
* `B` [Factorial](src/algorithms/math/factorial)
64-
* `B` [Fibonacci Number](src/algorithms/math/fibonacci)
64+
* `B` [Fibonacci Number](src/algorithms/math/fibonacci) - classic and closed-form versions.
6565
* `B` [Primality Test](src/algorithms/math/primality-test) (trial division method)
6666
* `B` [Euclidean Algorithm](src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD)
6767
* `B` [Least Common Multiple](src/algorithms/math/least-common-multiple) (LCM)

‎src/algorithms/math/fibonacci/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ The Fibonacci spiral: an approximation of the golden spiral created by drawing c
1717

1818
## References
1919

20-
[Wikipedia](https://en.wikipedia.org/wiki/Fibonacci_number)
20+
- [Wikipedia](https://en.wikipedia.org/wiki/Fibonacci_number)

‎src/algorithms/math/fibonacci/__test__/fibonacciClosedForm.test.js

-23
This file was deleted.

‎src/algorithms/math/fibonacci/__test__/fibonacciNth.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ describe('fibonacciNth', () => {
1919
expect(fibonacciNth(73)).toBe(806515533049393);
2020
expect(fibonacciNth(74)).toBe(1304969544928657);
2121
expect(fibonacciNth(75)).toBe(2111485077978050);
22+
expect(fibonacciNth(80)).toBe(23416728348467685);
23+
expect(fibonacciNth(90)).toBe(2880067194370816120);
2224
});
2325
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import fibonacciNthClosedForm from '../fibonacciNthClosedForm';
2+
3+
describe('fibonacciClosedForm', () => {
4+
it('should throw an error when trying to calculate fibonacci for not allowed positions', () => {
5+
const calculateFibonacciForNotAllowedPosition = () => {
6+
fibonacciNthClosedForm(76);
7+
};
8+
9+
expect(calculateFibonacciForNotAllowedPosition).toThrow();
10+
});
11+
12+
it('should calculate fibonacci correctly', () => {
13+
expect(fibonacciNthClosedForm(1)).toBe(1);
14+
expect(fibonacciNthClosedForm(2)).toBe(1);
15+
expect(fibonacciNthClosedForm(3)).toBe(2);
16+
expect(fibonacciNthClosedForm(4)).toBe(3);
17+
expect(fibonacciNthClosedForm(5)).toBe(5);
18+
expect(fibonacciNthClosedForm(6)).toBe(8);
19+
expect(fibonacciNthClosedForm(7)).toBe(13);
20+
expect(fibonacciNthClosedForm(8)).toBe(21);
21+
expect(fibonacciNthClosedForm(20)).toBe(6765);
22+
expect(fibonacciNthClosedForm(30)).toBe(832040);
23+
expect(fibonacciNthClosedForm(50)).toBe(12586269025);
24+
expect(fibonacciNthClosedForm(70)).toBe(190392490709135);
25+
expect(fibonacciNthClosedForm(71)).toBe(308061521170129);
26+
expect(fibonacciNthClosedForm(72)).toBe(498454011879264);
27+
expect(fibonacciNthClosedForm(73)).toBe(806515533049393);
28+
expect(fibonacciNthClosedForm(74)).toBe(1304969544928657);
29+
expect(fibonacciNthClosedForm(75)).toBe(2111485077978050);
30+
});
31+
});

‎src/algorithms/math/fibonacci/fibonacciClosedForm.js

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Calculate fibonacci number at specific position using closed form function (Binet's formula).
3+
* @see: https://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_expression
4+
*
5+
* @param {number} position - Position number of fibonacci sequence (must be number from 1 to 75).
6+
* @return {number}
7+
*/
8+
export default function fibonacciClosedForm(position) {
9+
const topMaxValidPosition = 75;
10+
11+
// Check that position is valid.
12+
if (position < 1 || position > topMaxValidPosition) {
13+
throw new Error(`Can't handle position smaller than 1 or greater than ${topMaxValidPosition}`);
14+
}
15+
16+
// Calculate √5 to re-use it in further formulas.
17+
const sqrt5 = Math.sqrt(5);
18+
// Calculate φ constant (≈ 1.61803).
19+
const phi = (1 + sqrt5) / 2;
20+
21+
// Calculate fibonacci number using Binet's formula.
22+
return Math.floor((phi ** position) / sqrt5 + 0.5);
23+
}

0 commit comments

Comments
 (0)
Please sign in to comment.