Test if a number is fibonacci

后端 未结 20 880
深忆病人
深忆病人 2020-11-30 17:58

I know how to make the list of the Fibonacci numbers, but i don\'t know how can i test if a given number belongs to the fibonacci list - one way that comes in mind is genera

相关标签:
20条回答
  • 2020-11-30 18:16

    Positive integer ω is a Fibonacci number

    If and only if one of2 + 4 and 5ω2 - 4 is a perfect square

    from The (Fabulous) FIBONACCI Numbers by Alfred Posamentier and Ingmar Lehmann

    bool isFibonacci(int  w)
    {
           double X1 = 5 * Math.Pow(w, 2) + 4;
           double X2 = 5 * Math.Pow(w, 2) - 4;
    
           long X1_sqrt = (long)Math.Sqrt(X1);
           long X2_sqrt = (long)Math.Sqrt(X2);   
    
           return (X1_sqrt*X1_sqrt == X1) || (X2_sqrt*X2_sqrt == X2) ;
    }
    

    I copied it from this source


    Snippet that prints Fibonacci numbers between 1k and 10k.

    for (int i = 1000; i < 10000; i++)
    {
             if (isFibonacci(i))
                  Console.Write(" "+i);
    }
    

    OMG There are only FOUR!!!

    With other method

    from math import *
    
    phi = 1.61803399
    sqrt5 = sqrt(5)
    
    def F(n):
        return int((phi**n - (1-phi)**n) /sqrt5)
    
    def isFibonacci(z):
        return F(int(floor(log(sqrt5*z,phi)+0.5))) == z
    
    print [i for i in range(1000,10000) if isFibonacci(i)]
    
    0 讨论(0)
  • 2020-11-30 18:18

    A very nice test is that N is a Fibonacci number if and only if 5 N^2 + 4 or 5N^2 – 4 is a square number. For ideas on how to efficiently test that a number is square refer to the SO discussion.

    Hope this helps

    0 讨论(0)
  • 2020-11-30 18:20

    This is my solution I'm not sure if it benchmarks. I hope this helps!

    def is_fibonacci?(i)
      a,b=0,1
        until b >= i
            a,b=b,a+b
            return true if b == i
        end
    end
    

    what a,b=b,a+b is doing

     0, 1 = 1, 0 +1
     1, 1 = 1, 1 + 1
     1, 2 = 2, 1 + 2
     2, 3 = 3, 2 + 3
    
    fib1 = fib2
    fib2 = fib1 + fib2
    
    0 讨论(0)
  • 2020-11-30 18:21
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    int number_entered, x, y;
    
    printf("Please enter a number.\n");
    scanf("%d", &number_entered);
    x = y = 5 * number_entered^2 + 4;        /*Test if 5N^2 + 4 is a square number.*/
    x = sqrt(x);
    x = x^2;
    if (x == y)
    {
            printf("That number is in the Fibonacci sequence.\n");
        }
    x = y = 5 * number_entered^2 - 4;        /*Test if 5N^2 - 4 is a square number.*/
    x = sqrt(x);
    x = x^2;
    if (x == y)
    {
        printf("That number is in the Fibonacci sequence.\n");
    }
    else
    {
        printf("That number isn't in the Fibonacci sequence.\n");
    }
    return 0;
    }
    

    Will this work?

    0 讨论(0)
  • 2020-11-30 18:22

    A positive integer ω is a Fibonacci number if and only if either 5ω2 + 4 or 5ω2 - 4 is a perfect square.

    See The Fabulous Fibonacci Numbers for more.

    alt text

    0 讨论(0)
  • 2020-11-30 18:25

    Based on earlier answers by me and psmears, I've written this C# code.

    It goes through the steps slowly, and it can clearly be reduced and optimized:

    // Input: T: number to test.
    // Output: idx: index of the number in the Fibonacci sequence.
    //    eg: idx for 8 is 6. (0, 1, 1, 2, 3, 5, 8)
    // Return value: True if Fibonacci, False otherwise.
    static bool IsFib(long T, out int idx)
    {
        double root5 = Math.Sqrt(5);
        double PSI = (1 + root5) / 2;
    
        // For reference, IsFib(72723460248141) should show it is the 68th Fibonacci number
    
        double a;
    
        a = T*root5;
        a = Math.Log(a) / Math.Log(PSI);
        a += 0.5;
        a = Math.Floor(a);
        idx = (Int32)a;
    
        long u = (long)Math.Floor(Math.Pow(PSI, a)/root5 + 0.5);
    
        if (u == T)
        {
            return true;
        }
        else
        {
            idx = 0;
            return false;
        }
    }
    

    Testing reveals this works for the first 69 Fibonacci numbers, but breaks down for the 70th.

    F(69) = 117,669,030,460,994 - Works
    F(70) = 190,392,490,709,135 - Fails
    

    In all, unless you're using a BigInt library of some kind, it is probably better to have a simple lookup table of the Fibonacci Numbers and check that, rather than run an algorithm.

    A list of the first 300 Numbers is readily available online.

    But this code does outline a workable algorithm, provided you have enough precision, and don't overflow your number representation system.

    0 讨论(0)
提交回复
热议问题