📜  在恒定时间内查找给定斐波那契数的索引

📅  最后修改于: 2021-04-27 19:24:59             🧑  作者: Mango

我们得到一个斐波那契数。前几个斐波那契数是0、1、1、2、3、5、8、13、21、34、55、89、144,…..
我们必须找到给定斐波那契数的索引,即像斐波那契数8一样位于索引6。

例子 :

Input : 13
Output : 7

Input : 34
Output : 9

方法1(简单)
一种简单的方法是找到不超过给定斐波纳契数的斐波那契数和执行的迭代计数。

C++
// A simple C++ program to find index of given
// Fibonacci number.
#include
  
int findIndex(int n)
{
    // if Fibonacci number is less than 2,
    // its index will be same as number
    if (n <= 1)
        return n;
  
    int a = 0, b = 1, c = 1;
    int res = 1;
  
    // iterate until generated fibonacci number 
    // is less than given fibonacci number
    while (c < n)
    {
        c = a + b;
          
        // res keeps track of number of generated 
        // fibonacci number
  
        res++;
        a = b;
        b = c;
    }
    return res;
}
  
// Driver program to test above function
int main()
{
    int result = findIndex(21);
    printf("%d\n", result);
}
  
// This code is contributed by Saket Kumar


Java
// A simple Java program to find index of 
// given Fibonacci number.
import java.io.*;
  
class GFG {
      
    static int findIndex(int n)
    {
          
        // if Fibonacci number is less 
        // than 2, its index will be
        // same as number
        if (n <= 1)
            return n;
      
        int a = 0, b = 1, c = 1;
        int res = 1;
      
        // iterate until generated fibonacci
        // number is less than given 
        // fibonacci number
        while (c < n)
        {
            c = a + b;
              
            // res keeps track of number of
            // generated fibonacci number
            res++;
            a = b;
            b = c;
        }
          
        return res;
    }
      
    // Driver program to test above function
    public static void main (String[] args) 
    {
        int result = findIndex(21);
        System.out.println( result);
    }
}
  
// This code is contributed by anuj_67.


C#
// A simple C# program to 
// find index of given 
// Fibonacci number.
using System;
class GFG 
{
    static int findIndex(int n)
    {
          
        // if Fibonacci number 
        // is less than 2, its 
        // index will be same 
        // as number
        if (n <= 1)
            return n;
      
        int a = 0, b = 1, c = 1;
        int res = 1;
      
        // iterate until generated 
        // fibonacci number is less 
        // than given fibonacci number
        while (c < n)
        {
            c = a + b;
              
            // res keeps track of 
            // number of generated
            // fibonacci number
            res++;
            a = b;
            b = c;
        }
          
        return res;
    }
      
    // Driver Code
    public static void Main () 
    {
        int result = findIndex(21);
        Console.WriteLine(result);
    }
}
  
// This code is contributed
// by anuj_67.


Python3
# A simple Python 3 program to find 
# index of given Fibonacci number.
  
def findIndex(n) :
      
    # if Fibonacci number is less than 2,
    # its index will be same as number
    if (n <= 1) :
        return n
   
    a = 0
    b = 1
    c = 1
    res = 1
   
    # iterate until generated fibonacci number 
    # is less than given fibonacci number
    while (c < n) :
        c = a + b
           
        # res keeps track of number of  
        # generated fibonacci number
        res = res + 1
        a = b
        b = c
          
    return res
  
# Driver program to test above function
result = findIndex(21)
print(result)
  
# this code is contributed by Nikita Tiwari


PHP


C++
// C++ program to find index of given Fibonacci
// nunber
#include
  
int findIndex(int n)
{
    float fibo = 2.078087 * log(n) + 1.672276;
  
    // returning rounded off value of index
    return round(fibo);
}
  
// Driver program to test above function
int main()
{
    int n = 55;
    printf("%d\n", findIndex(n));
}


Java
// A simple Java program to find index of given
// Fibonacci number
public class Fibonacci
{
  
  static int findIndex(int n)
  {
    float fibo = 2.078087F * (float) Math.log(n) + 1.672276F;
  
    // returning rounded off value of index
    return Math.round(fibo);
  }
  
  public static void main(String[] args)
  {
    int result = findIndex(55);
    System.out.println(result);
  }
}


Python
# Python 3 program to find index of given Fibonacci
# nunber
  
import math
def findIndex(n) :
    fibo = 2.078087 * math.log(n) + 1.672276
   
    # returning rounded off value of index
    return round(fibo)
  
  
# Driver program to test above function
n = 21
print(findIndex(n))
  
  
# This code is contributed by Nikita Tiwari.


C#
// A simple C# program to find 
// index of given Fibonacci number
using System;
  
class Fibonacci {
  
static int findIndex(int n)
{
    float fibo = 2.078087F * (float) Math.Log(n) +
                                        1.672276F;
  
    // returning rounded off value of index
    return (int)(Math.Round(fibo));
}
  
  // Driver code
  public static void Main()
  {
    int result = findIndex(55);
    Console.Write(result);
  }
}
  
// This code is contributed by nitin mittal


PHP


输出:

8

方法2(基于公式)

但是在这里,我们需要生成所有斐波那契数,直到提供的斐波那契数。但是,有解决此问题的快速方法。让我们看看如何!

请注意,在大多数平台上,数字的计算日志是O(1)运算。[来源:Stackoverflow]

斐波那契数被描述为
F n = 1 / sqrt(5)(pow(a,n)– pow(b,n))其中
a = 1/2(1 + sqrt(5))和b = 1/2(1 – sqrt(5))

忽略pow(b,n)对于大的n值来说很小,我们得到
n =舍入{2.078087 * log(Fn)+ 1.672276}
其中round表示舍入到最接近的整数。

以下是上述想法的实现。

C++

// C++ program to find index of given Fibonacci
// nunber
#include
  
int findIndex(int n)
{
    float fibo = 2.078087 * log(n) + 1.672276;
  
    // returning rounded off value of index
    return round(fibo);
}
  
// Driver program to test above function
int main()
{
    int n = 55;
    printf("%d\n", findIndex(n));
}

Java

// A simple Java program to find index of given
// Fibonacci number
public class Fibonacci
{
  
  static int findIndex(int n)
  {
    float fibo = 2.078087F * (float) Math.log(n) + 1.672276F;
  
    // returning rounded off value of index
    return Math.round(fibo);
  }
  
  public static void main(String[] args)
  {
    int result = findIndex(55);
    System.out.println(result);
  }
}

Python

# Python 3 program to find index of given Fibonacci
# nunber
  
import math
def findIndex(n) :
    fibo = 2.078087 * math.log(n) + 1.672276
   
    # returning rounded off value of index
    return round(fibo)
  
  
# Driver program to test above function
n = 21
print(findIndex(n))
  
  
# This code is contributed by Nikita Tiwari.

C#

// A simple C# program to find 
// index of given Fibonacci number
using System;
  
class Fibonacci {
  
static int findIndex(int n)
{
    float fibo = 2.078087F * (float) Math.Log(n) +
                                        1.672276F;
  
    // returning rounded off value of index
    return (int)(Math.Round(fibo));
}
  
  // Driver code
  public static void Main()
  {
    int result = findIndex(55);
    Console.Write(result);
  }
}
  
// This code is contributed by nitin mittal

的PHP


输出:

10

参考 :
https://math.stackexchange.com/questions/848691/how-to-tell-if-a-fibonacci-number-has-an-even-or-odd-index