The Fibonacci numbers are the numbers in the following integer sequence (Fn):

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ...

such as

F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1.

Given a number, say prod (for product), we search two Fibonacci numbers F(n) and F(n+1) verifying

F(n) * F(n+1) = prod.

Your function productFib takes an integer (prod) and returns an array:

[F(n), F(n+1), true] or {F(n), F(n+1), 1} or (F(n), F(n+1), True)

depending on the language if F(n) * F(n+1) = prod.

If you don't find two consecutive F(n) verifying F(n) * F(n+1) = prodyou will return

[F(n), F(n+1), false] or {F(n), F(n+1), 0} or (F(n), F(n+1), False)

F(n) being the smallest one such as F(n) * F(n+1) > prod.

Some Examples of Return:

depend on the language)

productFib(714) # should return (21, 34, true),

# since F(8) = 21, F(9) = 34 and 714 = 21 * 34

productFib(800) # should return (34, 55, false),

## # since F(8) = 21, F(9) = 34, F(10) = 55 and 21 * 34 < 800 < 34 * 55

productFib(714) # should return [21, 34, true],

## productFib(800) # should return [34, 55, false],

productFib(714) # should return {21, 34, 1},

## productFib(800) # should return {34, 55, 0},

productFib(714) # should return {21, 34, true},

productFib(800) # should return {34, 55, false},

Steps:

- A fib series starts with 0 and 1, so lets create two variables that will hold 0 and 1
- using a while loop. the condition is that first * last must be less than the number given to us which is prod.
- so inside the loop we create a a new variable temp and we set it to the value of last.
- we set the new value of last to first + last. the new value of last is the next value in the fib. series.
- finally we set our first to the value of temp. e.g[0,1] where 0 is first and last is 1. now our temp will be 1, and our last will be 0+1 which is 1, then our first will be temp which is 1. so the new array will have [1,1]
- finally we return an array that bears the first , last and the condition checking if first * last === prod.

Algorithm

```
function productFib(prod) {
let first = 0
let last = 1
while (first * last < prod) {
const temp = last;
last = first + last;
first = temp;
}
return [first, last, first * last === prod];
}
console.log(productFib(5895));
```

## Top comments (0)