+ 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); } }

5th Sep 2019, 10:29 PM
Ify Nonyelu
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
5th Sep 2019, 11:03 PM
Marina Vasilyova
Marina Vasilyova - avatar
+ 2
Eliya Ben Baruch in fibonaci no 0, and you have two same numbers at the end and logic of this function is incorrect
5th Sep 2019, 11:12 PM
Marina Vasilyova
Marina Vasilyova - avatar
5th Sep 2019, 11:01 PM
Eliya Ben Baruch
Eliya Ben Baruch - avatar
+ 1
Please do not write sentence in Relevant Tags 👍
5th Sep 2019, 11:03 PM
Ipang
5th Sep 2019, 11:17 PM
Eliya Ben Baruch
Eliya Ben Baruch - avatar
+ 1
Ify Nonyelu look at the code I sent
5th Sep 2019, 11:17 PM
Eliya Ben Baruch
Eliya Ben Baruch - avatar
+ 1
Ify Nonyelu if you want to use list than delete array and replace f[n] = smth like yourArray.add(smth)
5th Sep 2019, 11:18 PM
Marina Vasilyova
Marina Vasilyova - avatar
5th Sep 2019, 11:19 PM
Marina Vasilyova
Marina Vasilyova - avatar
+ 1
Ok Ellya and Maria and thank you both
5th Sep 2019, 11:20 PM
Ify Nonyelu
0
I have to use a list but I'm not sure how to do it.
5th Sep 2019, 11:16 PM
Ify Nonyelu