I have a code that prints out the nth number of a fibonacci sequence, and here it is..
public class Fib {
public static BigInteger fibonacci(int n) {
if (n <= 1) return BigInteger.valueOf(n);
BigInteger previous = BigInteger.ZERO, next = BigInteger.ONE, sum;
for (int i = 2; i <= n; i++) {
sum = previous;
previous = next;
next = sum.add(previous);
}
return next;
}
public static void main(String[] args) {
System.out.print(fibonacci(156));
}
}
When I run this code, I get:
178890334785183168257455287891792
However, I want to modify my code so that it prints out every number in the sequence from 0 until the number n. So I want my output to look like this when n is the same
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, .... 178890334785183168257455287891792
What changes to my code do I need to make?