Test if a number is fibonacci

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

    Towards a solution, take a look at Binet's Formula.
    (Look for "Closed-Form Expression" under Fibonacci Number on Wikipedia)

    It says that the sequence of Fibonacci Number's is created by a simple closed formula:

    alt text

    I believe if you solve for n, and test if n is an integer, you'll have your answer.

    Edit As @psmears points out, the same Wikipedia article also has a section on detecting Fibonacci numbers. Wikipedia is an excellent source.

提交回复
热议问题