Determining whether a number is a Fibonacci number

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

    If my Java is not too rusty...

    static bool isFib(int x) {
        int a=0;
        int b=1;
        int f=1;
        while (b < x){
            f = a + b;
            a = b;
            b = f;
        }
        return x == f;
    }
    

提交回复
热议问题