Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d267d72

Browse files
ohepworthbelltrekhleb
authored andcommitted
Added fibonacci sequence as well as fibonacci nth, renamed functions accordingly (trekhleb#36)
1 parent a63bc67 commit d267d72

File tree

4 files changed

+59
-27
lines changed

4 files changed

+59
-27
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ import fibonacci from '../fibonacci';
22

33
describe('fibonacci', () => {
44
it('should calculate fibonacci correctly', () => {
5-
expect(fibonacci(1)).toBe(1);
6-
expect(fibonacci(2)).toBe(1);
7-
expect(fibonacci(3)).toBe(2);
8-
expect(fibonacci(4)).toBe(3);
9-
expect(fibonacci(5)).toBe(5);
10-
expect(fibonacci(6)).toBe(8);
11-
expect(fibonacci(7)).toBe(13);
12-
expect(fibonacci(8)).toBe(21);
13-
expect(fibonacci(20)).toBe(6765);
5+
expect(fibonacci(1)).toEqual([1]);
6+
expect(fibonacci(2)).toEqual([1, 1]);
7+
expect(fibonacci(3)).toEqual([1, 1, 2]);
8+
expect(fibonacci(4)).toEqual([1, 1, 2, 3]);
9+
expect(fibonacci(5)).toEqual([1, 1, 2, 3, 5]);
10+
expect(fibonacci(6)).toEqual([1, 1, 2, 3, 5, 8]);
11+
expect(fibonacci(7)).toEqual([1, 1, 2, 3, 5, 8, 13]);
12+
expect(fibonacci(8)).toEqual([1, 1, 2, 3, 5, 8, 13, 21]);
1413
});
1514
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import fibonacciNth from '../fibonacciNth';
2+
3+
describe('fibonacciNth', () => {
4+
it('should calculate fibonacci correctly', () => {
5+
expect(fibonacciNth(1)).toBe(1);
6+
expect(fibonacciNth(2)).toBe(1);
7+
expect(fibonacciNth(3)).toBe(2);
8+
expect(fibonacciNth(4)).toBe(3);
9+
expect(fibonacciNth(5)).toBe(5);
10+
expect(fibonacciNth(6)).toBe(8);
11+
expect(fibonacciNth(7)).toBe(13);
12+
expect(fibonacciNth(8)).toBe(21);
13+
expect(fibonacciNth(20)).toBe(6765);
14+
});
15+
});
Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
// Calculate fibonacci number at specific position using Dynamic Programming approach.
2-
export default function fibonacci(numberPosition) {
3-
if (numberPosition === 1) {
4-
return 1;
5-
}
1+
// Return a fibonacci sequence as an array
2+
export default function fibonacci(n) {
3+
const fibSequence = [1];
4+
5+
let currentValue = 1;
6+
let previousValue = 0;
67

7-
let iterationsCounter = numberPosition - 1;
8+
if (n === 1) {
9+
return fibSequence;
10+
}
811

9-
// Calculated fibonacci number.
10-
let fib = null;
11-
// Previous fibonacci number.
12-
let fibPrev = 1;
13-
// Before previous fibonacci number.
14-
let fibPrevPrev = 0;
12+
let iterationsCounter = n - 1;
1513

1614
while (iterationsCounter) {
17-
// Calculate current value using two previous ones.
18-
fib = fibPrev+fibPrevPrev;
19-
// Shift previous values.
20-
fibPrevPrev=fibPrev;
21-
fibPrev=fib;
15+
currentValue+=previousValue;
16+
previousValue = (currentValue-previousValue);
17+
18+
fibSequence.push(currentValue);
19+
2220
iterationsCounter -= 1;
2321
}
2422

25-
return fib;
23+
return fibSequence;
2624
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Calculate fibonacci number at specific position using Dynamic Programming approach.
2+
export default function fibonacciNth(n) {
3+
let currentValue = 1;
4+
let previousValue = 0;
5+
6+
if (n === 1) {
7+
return 1;
8+
}
9+
10+
let iterationsCounter = n - 1;
11+
12+
while (iterationsCounter) {
13+
currentValue += previousValue;
14+
previousValue = (currentValue - previousValue);
15+
16+
iterationsCounter -= 1;
17+
}
18+
19+
return currentValue;
20+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /