5kyu Product of consecutiveFib numbers

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},
Note:
You can see examples for your language in “Sample Tests”.

typedef unsigned long long ull;
#include <stdlib.h>
ull fib( ull n )
{
 ull a = 0, b = 1, c;
  for( ull i = 0; i < n; i++ )
  {
c = a + b;
a = b;
b = c;
  }
  return c;
}
unsigned long long* productFib(ull prod) {
  ull *N = (ull *)malloc( sizeof( ull ) * 3 ), i = 0;

  while( 1 )
  {
N[0] = fib( i );
N[1] = fib( i + 1 );
if( N[0] * N[1] == prod )
{
  N[2] = 1;
  return N;
}
else if( N[0] * N[1] > prod )
{
  N[2] = 0;
  return N;
}

i++;
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值