Product of consecutive Fib numbers

kata programming

مساله:

اعداد فیبوناچی اعدادی در دنباله صحیح زیر (Fn) هستند:

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

مانند زیر:

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

عددی به شما داده می شود که prod نام دارد، ما باید دو عدد فیبوناچی را جستجو کنیم که شرایط زیر را داشته باشد:

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

تابع productFib شما یک عدد صحیح (prod) می گیرد و یک آرایه را برمی گرداند:

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

F(n) کوچکترین F(n) * F(n+1)> prod است

چند مثال دیگر:

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}, 

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] 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”.


public class ProdFib {
  
  public static long[] productFib(long prod) {
    long a = 0L;
    long b = 1L;
    while (a * b < prod) {
      long tmp = a;
      a = b;
      b = tmp + b;
    }
    return new long[] { a, b, a * b == prod ? 1 : 0 };
   }
}
public class ProdFib { // must be public for codewars 
  
  public static long[] productFib(long prod) {
    long fibProd = 0;
    int i = 1;
    while (fibProd < prod) {
      fibProd = fibNum(i) * fibNum(i + 1);
      i++;
    }
    if (fibProd == prod) return new long[] {fibNum(i - 1),fibNum(i),1};
    return new long[] {fibNum(i - 1),fibNum(i),0};
   }
   public static long fibNum(int n) {
        double a = (Math.sqrt(5) + 1) / 2;
        return (long) ((1 / Math.sqrt(5))*(Math.pow(a, n) + Math.pow(a-1, n)));
    }
}
public class ProdFib { // must be public for codewars 
  
  public static long[] productFib(long prod) {
    long i = 1;
    long j = 1;
    while (i*j < prod) {long k = i; i=j; j=k+i; }
    if (i*j == prod) return new long[] {i, j, 1};
    return new long[] {i, j, 0};
   }
}
function productFib($prod) {
  $a = 0; $b = 1;
  while ($a * $b < $prod) {
    $next = $a + $b;
    $a = $b;
    $b = $next;
  }
  return [$a, $b, $a * $b == $prod];
}
function productFib($prod) {
  $a = 0; $b = 1;
  while ($a * $b < $prod) {
    $next = $a + $b;
    $a = $b;
    $b = $next;
  }
  return [$a, $b, $a * $b == $prod];
}
function productFib(prod){
  let [a, b] = [0, 1];
  while(a * b < prod) [a, b] = [b, a + b];
  return [a, b, a * b === prod];
}
function productFib(prod){
  var a = 1
  var b = 1;
  while (a*b < prod) {
    var next = a+b;
    a = b;
    b = next;
  }
  return [a, b, a*b===prod];
}

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *