-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
46 lines (43 loc) · 1.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
Given a Fibonacci number, give the previous Fibonacci number.
If the number given is not a Fibonacci number, return -1.
*/
const phi = (1 + Math.sqrt(5)) / 2;
const prevFibonacci = (val) => {
let n = 1;
// Formula: F_n = Math.floor((phi)^n/sqrt(5) + 0.5)
while (Math.floor(Math.pow(phi, n) / Math.sqrt(5) + 0.5) < val) {
n++;
}
if (Math.floor(Math.pow(phi, n) / Math.sqrt(5) + 0.5) === val) {
return Math.floor(Math.pow(phi, n - 1) / Math.sqrt(5) + 0.5);
} else {
return -1;
}
};
console.log(prevFibonacci(3));
console.log(prevFibonacci(5));
console.log(prevFibonacci(8));
console.log(prevFibonacci(13));
console.log(prevFibonacci(21));
console.log(prevFibonacci(34));
console.log(prevFibonacci(55));
console.log(prevFibonacci(89));
console.log(prevFibonacci(144));
console.log(prevFibonacci(233));
console.log(prevFibonacci(377));
console.log(prevFibonacci(610));
console.log(prevFibonacci(987));
console.log(prevFibonacci(1597));
console.log(prevFibonacci(2584));
console.log(prevFibonacci(4181));
console.log(prevFibonacci(6765));
console.log(prevFibonacci(10946));
console.log(prevFibonacci(17711));
console.log(prevFibonacci(28657));
console.log(prevFibonacci(46368));
console.log(prevFibonacci(75025));
console.log(prevFibonacci(121393));
console.log(prevFibonacci(196418));
console.log(prevFibonacci(317811));
console.log(prevFibonacci(317812));