简体   繁体   English

"Java:检查数字是否属于斐波那契数列"

[英]Java: check if number belongs to Fibonacci sequence

I'm supposed to write a code which checks if a given number belongs to the Fibonacci sequence.我应该编写一个代码来检查给定数字是否属于斐波那契数列。 After a few hours of hard work this is what i came up with:经过几个小时的努力,这是我想出的:

public class TP2 {

    /**
     * @param args
     */

    public static boolean ehFibonacci(int n) {
        int fib1 = 0;
        int fib2 = 1;
        do {
            int saveFib1 = fib1;
            fib1 = fib2;
            fib2 = saveFib1 + fib2;
            }
        while (fib2 <= n);

        if (fib2 == n)
            return true;
        else
            return false;

    }
    public static void main(String[] args) {
        int n = 8;
        System.out.println(ehFibonacci(n));

    }
}

I must be doing something wrong, because it always returns "false".我一定是做错了什么,因为它总是返回“假”。 Any tips on how to fix this?有关如何解决此问题的任何提示?

你继续循环而fib2 <= n ,所以当你离开循环时,fib2总是> n ,所以它返回false

/**
 * @param args
 */

public static boolean ehFibonacci(int n) {
    int fib1 = 0;
    int fib2 = 1;
    do {
        int saveFib1 = fib1;
        fib1 = fib2;
        fib2 = saveFib1 + fib2;
        }
    while (fib2 < n);

    if (fib2 == n)
        return true;
    else
        return false;

}
public static void main(String[] args) {
    int n = 5;
    System.out.println(ehFibonacci(n));

}

This works. 这有效。 I am not sure about efficiency..but this is a foolproof program, 我不确定效率..但这是一个万无一失的程序,

public class isANumberFibonacci {

public static int fibonacci(int seriesLength) {
    if (seriesLength == 1 || seriesLength == 2) {
        return 1;
    } else {
        return fibonacci(seriesLength - 1) + fibonacci(seriesLength - 2);
    }
}

public static void main(String args[]) {
    int number = 4101;
    int i = 1;
    while (i > 0) {
        int fibnumber = fibonacci(i);
        if (fibnumber != number) {
            if (fibnumber > number) {
                System.out.println("Not fib");
                break;
            } else {
                i++;
            }
        } else {
            System.out.println("The number is fibonacci");
            break;
        }
    }
}

} }

you can also use perfect square to check whether your number is Fibonacci or not.您还可以使用完全平方来检查您的数字是否为斐波那契数。 you can find the code and some explanation at geeksforgeeks .你可以在geeksforgeeks找到代码和一些解释。 you can also see stackexchange for the math behind it.你还可以看到stackexchange它背后的数学。

I'm a beginner but this code runs perfectly fine without any issues.我是初学者,但这段代码运行得很好,没有任何问题。 Checked with test cases hopefully it'll solve your query.检查测试用例希望它能解决您的查询。

public static boolean checkMember(int n) {
    int x = 0;
    int y = 1;
    int sum = 0;
    boolean isTrue = true;

    for (int i = 1; i <= n; i++) {
        x = y;
        y = sum;
        sum = x + y;
        if (sum == n) {
            isTrue=true;
            break;
        } else {
            isTrue=false;
        }
    }
    return isTrue;
}

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    System.out.print(checkMember(n));

}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM