Determining whether a number is a Fibonacci number

前端 未结 14 1381
暗喜
暗喜 2021-01-18 05:35

I need to to write a Java code that checks whether the user inputed number is in the Fibonacci sequence.

I have no issue writing the Fibonacci sequence to output, b

14条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-18 06:38

    Read the section titled "recognizing fibonacci numbers" on wikipedia.

    Alternatively, a positive integer z is a Fibonacci number if and only if one of 5z^2 + 4 or 5z^2 − 4 is a perfect square.[17]

    Alternatively, you can keep generating fibonacci numbers until one becomes equal to your number: if it does, then your number is a fibonacci number, if not, the numbers will eventually become bigger than your number, and you can stop. This is pretty inefficient however.

提交回复
热议问题