简体   繁体   中英

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 . you can also see stackexchange for the math behind it.

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));

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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