I wrote a program to store Fibonacci numbers and will retrieve the nth Fibonacci number. It works fine until the 50th Fibonacci where it returns a negative number.
getFibonacci(47) returns 1836311903 but
getFibonacci(48) returns -1323752223. Why is this?
public class Fibonacci {
static HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
public static void main(String[] args) {
int x;
storeFibonacciNumbers(50);
System.out.println(getFibonacci(48));
}
public static void storeFibonacciNumbers (int n){
for (int x=1; x <= n; x++ ){
if(x == 1){
map.put(x, 0);
}
else if (x == 2) {
map.put(x, x-1);
}
else
map.put(x, map.get(x-1) + map.get(x-2));
}
}
public static long getFibonacci (int x){
return map.get(x);
}
}