Determining whether a number is a Fibonacci number

前端 未结 14 1206
暗喜
暗喜 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:19

    There are a number of methods that can be employed to determine if a given number is in the fibonacci sequence, a selection of which can be seen on wikipedia.

    Given what you've done already, however, I'd probably use a more brute-force approach, such as the following:

    1. Generate a fibonacci number
    2. If it's less than the target number, generate the next fibonacci and repeat
    3. If it is the target number, then success
    4. If it's bigger than the target number, then failure.

    I'd probably use a recursive method, passing in a current n-value (ie. so it calculates the nth fibonacci number) and the target number.

提交回复
热议问题