0
help me.. tell the fibonicci code for java
anyone knows?
2 ответов
+ 1
to give a totally recursive definition:
http://www.sololearn.com/app/java/playground/clzWIOrso9T7
If you're interested in large fib numbers this is not the best approach to take, I would advise looking into dynamic programming. I have rigged up an example for you here:
http://www.sololearn.com/app/java/playground/cGAqfY7KOs9f
Note how much faster the dynamic approach is compared to the recursive.
0
/* Fibonacci Series */
import java.io.*;
class Fibonacci
{
public static void main(String args[])
{
String str;
int no,f1,f2,f3;
f2=0;
f3=1;
try{
System.out.print("Enter number to generate it's Fibonacci Series : ");
System.out.flush();
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
str=obj.readLine();
no=Integer.parseInt(str);
System.out.print(f3+" ");
System.out.flush();
while(no>1)
{
f1=f2;
f2=f3;
f3=f1+f2;
System.out.print(f3+" ");
System.out.flush();
no=no-1;
}
}
catch(Exception e)
{}
}}