• Post Reply Bookmark Topic Watch Topic
  • New Topic

Write a program to print fibonacci series up to 100  RSS feed

 
Birel Chowdhury
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code:


public class Fibonacci {

/**
* @param args
*/
public static void main(String[] args) {

int f[]=new int[100];

for(int j=0; j<100;j++){
f[j]=j;

}

System.out.println(f[0]);
System.out.println(f[1]);

int i=2;
while( f[i-1]<100){
f[i]=f[i-1]+f[i-2];
if(f[i]>100)
break;
System.out.println(f[i]);
i++;

}

}


}


How can I improve my code? Please advise.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Birel Chowdhury wrote:
How can I improve my code? Please advise.


Well, probably pointing out the obvious, but you don't actually need an array of size 100 to hold Fibonacci numbers up to 100. In fact, you need a lot less than an array of size 100.

Henry
 
Birel Chowdhury
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. I can use upto 15 arraysize, that will do. But my concern is the following part:

int i=2;
while( f[i-1]<100){
f[i]=f[i-1]+f[i-2];
if(f[i]>100)
break;
System.out.println(f[i]);
i++;

}

I am using while loop and again if loop, how can I make it better?
 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a typical problem to be solved using recursion:
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That recursive solution is notorious for its slow performance because it runs in exponential complexity. you should have a Fibonacci program which runs in linear time. There is actually an example in Kaldewaaij's book which runs in logarithmic time, but you would need the book to copy the algorithm.
 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That recursive solution is notorious for its slow performance because it runs in exponential complexity.

Well I haven't been thinking about performance in this case, given the limit of 100, but rather about the simplicity which I assume was the purpose of the exercise. But you are right, I shall have a look at a better way to accomplish the task.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Birel Chowdhury wrote:I am using while loop and again if loop, how can I make it better?

Well, one might be to start one earlier and use a for loop that calculates at the end, viz:Alternatively, do the calculation and check at the same time:
Oh, and you don't have to fill all 100 elements to start with; only the first two.

Winston
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought it meant the first 100 Fibonacci numbers, which would overflow the range of a long, and would take several million years to calculate with that inefficient recursive method. If you have an array it is probably quicker to iterate the array and add the preceding two values.
fib₁₁ < 100 and fib₁₂ > 100, which is why people said you don't need a 100‑element array. Unless you use BigInteger objects, which will work out that fib₁ = 1 and fib₂ = 2 and fib₁₀₀ = 354224848179261915075
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!