Reputation: 5476
I know that the coding for the Fibonacci series is:
int fib(int n)
{
if (n==0 || n==1) return n;
else return fib(n-1)+fib(n-2);
}
I was wondering if there is a way, using the aforementioned code, to print the previous results of the series, but neither using a void function (that acts only like a printer of the series) or calling the fibonacci funcion for each calculation
I do not want to do this
for (int i=0;i<4;i++){
System.out.prinntln(fib(i));
}
Intead I want to only call the function once, like this:
fib(4);
and that the printing is:
0,1,1,2,3
of course using recursion
Any idea? Thanks
Upvotes: 4
Views: 7657
Reputation: 1241
[EDIT1] https://www.jdoodle.com/iembed/v0/2N3
I fixed my function for print N_0, it's cool:
public class Main
{
public static int f(int n, boolean p, int level, final int deep) {
if (n < 2) {
if (p || level == deep) System.out.println(1);
return 1;
} else {
int res = f(n-1, true && p, level + 1, deep) + f(n-2, false, level + 1, deep);
if (p) {
System.out.println(res);
}
return res;
}
}
public static void main(String[] args) {
int n = 10;
f(n, true, 0, n-1); // print recursive
}
}
I can do it in one func, but lost number N_0, like this:
https://onlinegdb.com/S1M0SuEPv
I explain it like stack of recursive function when it be called:
So the value of return function will like this stack:
We need to print the number in circle, so I include a boolean param for do it:
public class Main
{
public static int f(int n, boolean p) {
if (n < 2) {
if (p) System.out.println(1);
return 1;
} else {
int res = f(n-1, true && p) + f(n-2, false);
if (p) {
System.out.println(res);
}
return res;
}
}
public static void main(String[] args) {
System.out.println(1);
f(10, true); // print recursive
}
}
The result is very beautiful :))
1 1 2 3 5 8 13 21 34 55 89
Upvotes: 0
Reputation: 7686
So far, this is the most efficient and fastest way I could came out for the Fibonacci Sequence:
FIBONACCI DYNAMIC VERSION
public static BigInteger getFibonacciDynamic(long n) {
BigInteger[] fibonacci = new BigInteger[(int) n + (n == 0 ? 2 : 1)];
fibonacci[0] = BigInteger.valueOf(0);
fibonacci[1] = BigInteger.valueOf(1);
for (int i = 2; i <= n; i++) {
fibonacci[i] = fibonacci[i - 1].add(fibonacci[i - 2]);
}
return fibonacci[(int) n];
}
You could run it this way for the first 1000 values:
public static void main(String[] args) {
int index = 0;
while (index < 1000) {
long time = System.currentTimeMillis();
BigInteger value = getFibonacciDynamic(index);
System.out.println("VALUE: " + value + " TOOK: " + (System.currentTimeMillis() - time) + "ms" + " OF INDEX: " + index);
index++;
}
}
And will print out:
VALUE: 0 TOOK: 0ms OF INDEX: 0
VALUE: 1 TOOK: 0ms OF INDEX: 1
VALUE: 1 TOOK: 0ms OF INDEX: 2
VALUE: 2 TOOK: 0ms OF INDEX: 3
VALUE: 3 TOOK: 0ms OF INDEX: 4
VALUE: 5 TOOK: 0ms OF INDEX: 5
VALUE: 8 TOOK: 0ms OF INDEX: 6
VALUE: 13 TOOK: 0ms OF INDEX: 7
VALUE: 21 TOOK: 0ms OF INDEX: 8
VALUE: 34 TOOK: 0ms OF INDEX: 9
VALUE: 55 TOOK: 0ms OF INDEX: 10
VALUE: 89 TOOK: 0ms OF INDEX: 11
VALUE: 144 TOOK: 0ms OF INDEX: 12
VALUE: 233 TOOK: 0ms OF INDEX: 13
VALUE: 377 TOOK: 0ms OF INDEX: 14
VALUE: 610 TOOK: 0ms OF INDEX: 15
VALUE: 987 TOOK: 0ms OF INDEX: 16
VALUE: 1597 TOOK: 0ms OF INDEX: 17
VALUE: 2584 TOOK: 0ms OF INDEX: 18
VALUE: 4181 TOOK: 0ms OF INDEX: 19
VALUE: 6765 TOOK: 0ms OF INDEX: 20
... till index 1000 with always < 10ms for each value
If you use recursive at index 45 is going to take this long:
FIBONACCI RECURSIVE
public static long getFibonacci(long n) {
if(n <= 1) {
return n;
} else {
return getFibonacci(n - 1) + getFibonacci(n - 2);
}
}
---- PRINT ----
VALUE: 1134903170 TOOK: 9991ms OF INDEX: 45
Upvotes: 0
Reputation: 1
I have used int array of size howManyDigit
.
This is the best way i can write Fibonacci series using less variables
int[] array = new int[howManyDigit];
for(int i=0;i<howManyDigit;i++){
if(i>1)
array [i]=array [i-1]+array [i-2];
else
array [i]=i;
}
System.out.println(Arrays.toString(array));
Upvotes: 0
Reputation: 323
This could be nice one...
public static void febonassi(int range, int pre, int nxt) {
System.out.println(pre);
if (nxt <= range)
febonassi(range, nxt, nxt + pre);
}
Upvotes: 0
Reputation: 1
I hope its easy way to print Fibonacci Series
Fibonacci Series
public class Fib
{
public static void main(String[] args)
{
int first=0,second=1;next;
System.out.prinf("%d %d ",first,second);
for(int i=0;i<=10;i++)
{
next=first+second;
first=second;
second=next;
System.out.printf("%d ",second);
}
}
}
Output
0 1 1 2 3 5 8 13 21 34 55 89
Upvotes: 0
Reputation: 21
package Practice;
public class Fabonacci {
public static void main(String[] args)
{
int a,b,c;
a=0;
b=1;
c=2;
for(int i=1; i<=10; i++)
{
c=a+b;
System.out.println(a);
a=b;
b=c;
}
}
}
This will output as:
0 1 1 2 3 5 8 13 21 34
Upvotes: 2
Reputation: 991
Here is a simple example that prints each calculation:
int x = 1;
int a = 0;
int t;
while(true)
{
System.out.println(x);
t = x;
x = x + a;
a = t;
if (x > 30)
{
break;
}
}
This will print the following:
1
1
2
3
5
8
13
21
This is the list of Fibonacci numbers less than 30.
Upvotes: 1
Reputation: 1
int sum = 1;
int n1 = 0 , n2 = 1;
for(int i = 0; i < 10; i++)
{
if(i < 1)
{
System.out.print(i);
System.out.print(" ");
}
else if(i > 1)
{
System.out.print(sum);
System.out.print(" ");
sum = n1 + n2;
n1 = n2;
n2 = sum;
}
}
Upvotes: -1
Reputation: 11
Writing Fibonacci series in one line .
public class Fib {
public static void main(String[] args) {
for(int i=1,f=0,n=0; n<15; n++,System.out.println(i=(f+(f+i))-(f=f+i)));
}
}
Upvotes: 1
Reputation: 237
A simple example on Fibonnaci series.
class Fibonacci {
public static void main(String args[]) {
fibonacciSeries(6);
}
static void fibonacciSeries(int num) {
int a = 0, b = 1, i, c;
for (i = 0; i <= num; i++) {
if (i <= 1) {
c = i;
} else {
c = a + b;
a = b;
b = c;
}
System.out.print(c);
if(i != num) {
System.out.print(",");
}
}
}
Output:
0,1,1,2,3,5,8
Upvotes: 0
Reputation: 926
Ok, so I didn't see you would like to keep the recursion.
Then how about this:
public class fib {
static int fibonacci(int value, boolean printThis) {
int result;
if (value==0 || value==1) {
result = value;
if (printThis) {
System.out.print(result);
System.out.print(", ");
}
} else {
if (printThis) {
result = fibonacci(value-1, true)+fibonacci(value-2, false);
System.out.print(result);
System.out.print(", ");
} else {
result = fibonacci(value-1, false)+fibonacci(value-2, false);
}
}
return result;
}
public static void main(String []args) {
fibonacci(7, true);
System.out.println();
}
}
I don't see that you can do without the boolean printThis
to control that only one path of the tree spawned by the recursion over two values is printed. To make this a little clearer, see how you could do this for faculty recursively.
The recursion calls the computation backwards, so faculty(n)
is called before of faculty(n-1)
. As you want print the value of faculty(n-1)
before faculty(n)
, you need to print before you return the value, like this:
static int faculty(int v) {
int result;
if (v==0) {
result = 1;
} else {
result = v*faculty(v-1);
}
System.out.print(result);
System.out.print(", ");
return result;
}
This will give you the values in ascending order. I hope you can forgive the last comma, you will need to define an additional function if you want to get rid of this without a boolean parameter controlling this.
So you see you can do for faculty without the boolean to control the printing.
But that is due to the fact that faculty
will not span a tree of recursive calls, but is just a single sequence of calls. As said before, I only see that you can control the printing if you have a whole tree of calls by adding a boolean functions.
Is that what you were after?
Anyway, here's still my first answer (which is more efficient, and will give you the same output faster):
Compute the Fibonacci numbers iteratively instead of recursively.
The easiest way is to use an array fib[]. Initialize with fib[0] = 0, fib[1] = 1. Then iterate over i = 2 to n, where fib[i] = fib[i-1] + fib [i-2].
It's also easy to tune this so you don't need the full array, but only two variables to store fib[i-1], fib[i-2].
And in fact, you could take this iterative loop and then again make it a recursive loop, but it would have a different structure than your original fibonacci function.
Upvotes: 1
Reputation: 76
You could write another method void printFib(int n)
and then use the loop to print each fib(n)
within this method. A method that returns a value really shouldn't be printing anything. But if you compute each number iteratively as Zane said, you can have a void method that computes and prints the values. I don't see any way to print the values within the recursive Fibonacci method.
Upvotes: 0