Reputation: 9
Any idea why my code is giving out the wrong answer after the first few?
My university professor provided us with this to go by and I feel I followed it?
If for example I was to use:
System.out.println(fibr(8));
System.out.println(fibr(9));
System.out.println(fibr(10));
The console prints out: 11 34 20
Which of course is not the fibonacci numbers in those places
public static int fibr(int n) {
if(n<0) return 0;
if(n==0) return 0;
if(n==1) return 1;
if(n==2) return 1;
//is odd
// n is = or > 3 and NOT (n divided by 2 with remainder of 0 (making it even))
if(n >= 3 && !(n % 2 == 0)) {
int a;
a = fibr((n+1)/2) * fibr((n+1)/2);
a = a + (fibr((n-1)/2) * fibr((n-1)/2));
return a;
}
//is even
if(n >= 3 && (n % 2 == 0)) {
int a;
a = fibr((n/2)+1) + fibr((n/2)-1) * fibr(n/2);
return a;
}
return 0;
}
Help what's wrong cri
Upvotes: 0
Views: 529
Reputation: 139
If your professor wants you to use the provided formula, then you should rewrite your code like this:
public class FibClass {
// main
public static void main(String[] args) {
System.out.println(fibr(7)); // Returns 13
}
// Fibonacci function
public static int fibr(int n) {
if ( n < 0 ) {
return 0; //returned value for negative integers
} else if (n == 0) {
return 0; //returned value for 0
} else if (n == 1) {
return 1; //1st number in the Fibonacci sequence
} else if (n == 2) {
return 1; //2nd number in the Fibonacci sequence
}
//is odd
else if (n >= 3 && !(n % 2 == 0)) {
int a;
a = (int) Math.pow(fibr((n + 1) / 2), 2);
a += (int) Math.pow(fibr((n - 1) / 2), 2);
return a;
}
//is even
else if (n >= 3 && (n % 2 == 0)) {
int a;
a = (fibr((n / 2) + 1) + fibr((n / 2) - 1)) * fibr(n / 2);
return a;
}
return 0;
} // END Fibonacci function fibr()
}
This is based on the formula you provided in the photo:
Although there are more simple functions to find the n-th number in the Fibonacci sequence.
Upvotes: 1
Reputation: 950
Your code is pretty difficult to read which, in practice, makes debugging very difficult. Try to take a simpler approach. Here is an example to find the n-th
Fibonacci number:
double fibbonaci(int n){
double prev = 0d, next = 1d, result = 0d;
for (int i = 0; i < n; i++) {
result = prev + next;
prev = next;
next = result;
}
return result;
}
Additionally, there is a more elegant method to this problem which involves recursion:
int fibonacci(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
Edit:
To fully answer your question, your error is in calculating even Fibonacci numbers. fib(8) = 21, fib(9) = 34, fib(10) = 55
.
For some unknown reason, you are calculating the n-th
even Fibonacci number using:
int a;
a = fibr((n/2)+1) + fibr((n/2)-1) * fibr(n/2);
return a;
By the Fibonacci sequence, the n-th
Fibonacci number is the sum of the previous two. So the above code can become:
return fibr(n - 1) + fibr(n - 2);
Therefore, your entire function can be reduced to:
public static int fibr(int n) {
if (n <= 0) return 0;
if (n == 1) return 1;
if (n == 2) return 1;
return fibr(n - 1) + fibr(n - 2);
}
Upvotes: 3