Problem Description: 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]
if F(n) * F(n+1) = prod or returns
[F(m), F(m+1), false]
if you don't find two consecutive F(m) verifying F(m) * F(m+1) = prod. F(m) will be the smallest one such as
F(m) * F(m+1) > prod.
Level:5kyu
Link To Kata: Product of consecutive Fib numbers
Here we have to find if the given number is a product of two consecutive numbers. We start by looping through a fibonacci series and check for product of every two terms of the series to be same as the given number. We also keep a check if the product comes out to be greater than the given number. If so then we break out of the loop and push both the numbers and a false boolean value to an empty array.
If we find two such consecutive terms whose product is same as the number then we push those two numbers and a true boolean value to the an empty array.
In the end we return the array as the solution to our Kata.
Here is the link to code: Kata Day 57
Please mention your suggestions or your doubts in the comment below.
Happy coding. :)
No comments:
Post a Comment