+ 1
find the first 20 numbers of the Fibonacci sequence and puts into a list using recursive way.. Example - List<Integer> l1 = ne
// I'm stuck and I could really use the help. import java.util.*; public class answer { public static int fib(int[] f, int n) { int ret = 0; if(n <= 2) ret = 1; else ret = fib(f, n-1) + fib(f, n-2); f[n-1] = ret; return ret; } public static void main(String[] args) { // TODO Auto-generated method stub List<Integer> ll = new ArrayList<Integer>(20); ll.add(0, 1); ll.add(1, 2); int[] f = new int[20]; fib(f, 20); for(int i = 0; i < 20; i++) System.out.print(ll); } }
10 Respuestas
+ 2
make f static field in this class
recuirsive function:
public static int fib(int n){
if(n<2){
f[n]=1;
return 1;
}
else{
fib = fib(n-1)+fib(n-2);
f[n] = fib;
}
You needn't List or ArrayList here
And you should follow java code convention
+ 2
Eliya Ben Baruch in fibonaci no 0, and you have two same numbers at the end
and logic of this function is incorrect
+ 1
Please do not write sentence in Relevant Tags 👍
+ 1
Maria Vasilyova fixed
+ 1
Ify Nonyelu look at the code I sent
+ 1
Ify Nonyelu if you want to use list than delete array and replace f[n] = smth like yourArray.add(smth)
+ 1
Ify Nonyelu ok
+ 1
Ok Ellya and Maria and thank you both
0
I have to use a list but I'm not sure how to do it.