Test if a number is fibonacci

后端 未结 20 933
深忆病人
深忆病人 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:21

    #include 
    #include 
    
    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?

提交回复
热议问题