# How to make a negative Fibonacci sequence in Java using recursion?

I have to write a program that returns Fibonacci number, but not only positive. I don't know what is not right in the way I've wrote it, but my code works with positive numbers and not with negative ones.

public static int negFib(int n) {

if(n==0 || n==1) {

return n;

}

if(n==-1) {

return 1;

}

if(n<0 && n%2==0) {

int a = Math.abs(n);

//return negFib(n+2) - negFib(n+1);

return (-1<<(n+1))*(negFib(n-1,) + negFib(n-2); // Fibonacci negative

//F(n)=F(n+2)−F(n+1)

//F(−1)=F(1)−F(0)=1−0=1 , F(−2)=F(0)−F(1)=0−1=−1

}

return negFib(n-1) + negFib(n-2); //Fibonacci positive

}...

By: StackOverFlow - Tuesday, 6 November

# Related Posts

- Older News
- Given a non-negative int n, compute recursively (no loops) the count of the occurrences of 8 StackOverFlow (Today) - Hi can you please help me with my new assignment on codingbat , its codingbat-recursion-count8Given a non-negative int n, compute recursively (no loops) the count of the occurrences of 8...
- Save recursion values for further calculations StackOverFlow (Yesterday) - Hello :) I have to find out the different combinations for an assignment (how to combine spices with different meals). I used a recursion to get the solution. The recursion...
- Why does tail recursion throwing StackOverflow exception StackOverFlow (2 days ago) - I am writing simple code of Fibonacci using Tail recursion. What I understand from concept of tail recursion that it usages single stack at any moment so should never throw...
- Using recursion to compute a 2d pyramid arrays weight StackOverFlow (2 days ago) - I am working on some homework about recursion and ive been struggling to understand how to do a few parts of the homework.Part of the assignment we have to calculate...
- Checking if sequence is almost strictly in ascending order StackOverFlow (3 days ago) - This was part of my interview questions.Given a sequence of integers as an array, I have to determine whether it is possible to obtain a strictly increasing sequence by removing...