Lucas numbers are numbers in a sequence defined like this:
L(n) = 2 if n = 0
L(n) = 1 if n = 1
otherwise
L(n) = L(n - 1) + L(n - 2)
Here is my code:
public class Lucas {
public static int lucasnum(int n) {
if (n >= 0) {
if (n == 0)
return 2;
else if (n == 1)
return 1;
else
return lucasnum(n - 1) + lucasnum(n - 2);
}
else{
if (n == 0)
return -2;
else if (n == -1)
return -1;
else
return lucasnum(n + 1) - lucasnum(n + 2);
}
}
public static void main(String[] args) {
System.out.println(Lucas.lucasnum(-5));
}
}
But I have some problem with negative number. If n == -5
it must return -11
but my code above return 3
.