I'm attempting to recursively compute the fibonacci sequence to 100, then store the resulting values in an array using the buildArray function, and then print the array contents.
public class MyFibonacci {
public static final int MAX = 100;
public static long[] buildArray(int MAX, int N) {
long[] A = new long[MAX];
A[0] = 0;
A[1] = 1;
for(N = 2; N < MAX; N++)
A[N] = F(N);
return A;
}
public static long F(int N) {
if(N == 0)
return 0;
if(N == 1)
return 1;
return F(N - 1) + F(N - 2);
}
public static void main(String[] args) {
for(int N = 0; N < MAX; N++)
System.out.println(N + " " + A[N]);
}
}
When I try to print A[N] in the main method, I receive a "cannot be resolved to a variable" compilation error. I'm following this article since it says I should use longs because I'm computing the series up to 100, but I'm not sure whether that's essential.
The code works if I replace F(N) for A[N], but I need to put the data into an array and output that array. Is it even possible for this code to save the data in an array? Thank you; I'm just getting started with Java.