It looks like you're using an Ad Blocker.

Please white-list or disable AboveTopSecret.com in your ad-blocking tool.

Thank you.

 

Some features of ATS will be disabled while you continue to use an ad-blocker.

 

Fibonacci Series Java

page: 1
1

log in

join
share:

posted on Mar, 13 2023 @ 10:01 AM
link   
I'm attempting to recursively compute the fibonacci sequence to 100, save the resulting values in an array using the buildArray function, and then display the array contents.
Here is the code:

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 using longs since I'm working through this scaler article, and I'm computing the series up to 100, though I'm not sure whether this is 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.
edit on 13-3-2023 by Mobo01 because: (no reason given)



posted on Mar, 13 2023 @ 12:18 PM
link   
Is recursion necessary for this?

You have a known iteration limit, so doesn't it just make more sense to just use a simple loop and append the results to an array?

I think this ultimately increases space complexity with no benefit to time complexity?

Basically, when the code gets compiled, a simple for loop will outperform recursion here, because each instance of your recursive method needs to be added to the stack and then processed. I suppose that would be useful if you wrote this to be multi-threaded?

Maybe in some cases it might seem more concise to write recursive methods, but then you have to be able to debug, and anyone who reads your code or tries to run it also needs to understand the recursion.


public static long F(int N) [
if(N == 0)
return 0;
if(N == 1)
return 1;
return F(N - 1) + F(N - 2);
]

I'm not sure what this is returning?

If you're doing your math on N, it's wrong.

You should be doing your math on A[N]

edit on 13-3-2023 by rounda because: (no reason given)



posted on Mar, 13 2023 @ 03:51 PM
link   
python

arr = [0, 1]
max = 100

for n in range(2, max):
. arr.append(arr[n - 1] + arr[n - 2])

print(arr)



posted on Mar, 13 2023 @ 04:35 PM
link   
a reply to: Mobo01

You are not calling the “buildArray” function! So nothing is assigned to any elements in A[], so the compiler is complaining.

Next, call the build function and maybe add a print A[] debug line to see that your array is assigned values from f().



posted on Mar, 22 2023 @ 08:09 PM
link   

originally posted by: Mobo01
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.

Ask ChatGPT.

Seriously. That is something that apparently it is extremely good at. It will even write the fixed code for you.



new topics

top topics
 
1

log in

join