PostFibonacci in Java

Posts 17 of 7 · Page 1 of 1
Fibonacci in Java
Today I'm sharing a project of my creation.
I hope you enjoy

Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg5.pkg11.pkg3;

import java****.BufferedReader;
import java****.InputStreamReader; 
import java****.Console;
import java********Exception;
public class Principal {
    private static int seg;
/**
 Programa desenvlvido por wallyson Marcos
 * 06/04/2013
 */
     public static void main(String[] args) throws IOException {  
         BufferedReader entrada;
        entrada = new BufferedReader(new InputStreamReader(System.in));
        long Fn=0;  
        long Fn1=1;  
        long b=1;
        long ent;
        long cont = 0;
        int opc ;
       try{ 
            System.out.println("||=========================Menu of project===============================||");
            System.out.println("||Enter 1 to determine how far will the nth Fibonacci number.            ||");
            System.out.println("||Enter 2 to determine the count of the number nth.                  ||");
            System.out.println("||==================================================================||");
            opc = Integer.parseInt(entrada.readLine());
              switch(opc)
              {
                  case 1:{
                      System.out.println("Enter a number to determine how far will the nth Fibonacci number");
                                           ent = Integer.parseInt(entrada.readLine());
                      System.out.println("Its sequel: ");   
                      
                      for(int i=2;i<=1000;i++){            
                          System.out.println(Fn+";");
                          b=Fn;  
                          Fn=Fn+Fn1;  
                          Fn1=b;                             
                          if(Fn>ent){break;    
                          }                            
                      }
                      break;      
                  }  
                  case 2:{
                       System.out.println("Enter a number to determine the count of the number nth. : ");
                        ent = Integer.parseInt(entrada.readLine());
                      System.out.println("Its sequel: ");
                      for(int i=1;i<=ent;i++){            
                          System.out.println(Fn+";"+"  Contador: "+i);
                          b=Fn;  
                          Fn=Fn+Fn1;  
                          Fn1=b; 
                      }
                  }  
                  break;
                  default:
                      System.out.println("Enter the correct option!");
                      
                      }
     
            
    } catch (Exception e){
            System.out.println("Enter a correct value!");
     }
     }

}
Crsdits:
@6ixth
Quote Originally Posted by 6ixth View Post
Today I'm sharing a project of my creation.
I hope you enjoy

Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg5.pkg11.pkg3;

import java****.BufferedReader;
import java****.InputStreamReader; 
import java****.Console;
import java********Exception;
public class Principal {
    private static int seg;
/**
 Programa desenvlvido por wallyson Marcos
 * 06/04/2013
 */
     public static void main(String[] args) throws IOException {  
         BufferedReader entrada;
        entrada = new BufferedReader(new InputStreamReader(System.in));
        long Fn=0;  
        long Fn1=1;  
        long b=1;
        long ent;
        long cont = 0;
        int opc ;
       try{ 
            System.out.println("||=========================Menu of project===============================||");
            System.out.println("||Enter 1 to determine how far will the nth Fibonacci number.            ||");
            System.out.println("||Enter 2 to determine the count of the number nth.                  ||");
            System.out.println("||==================================================================||");
            opc = Integer.parseInt(entrada.readLine());
              switch(opc)
              {
                  case 1:{
                      System.out.println("Enter a number to determine how far will the nth Fibonacci number");
                                           ent = Integer.parseInt(entrada.readLine());
                      System.out.println("Its sequel: ");   
                      
                      for(int i=2;i<=1000;i++){            
                          System.out.println(Fn+";");
                          b=Fn;  
                          Fn=Fn+Fn1;  
                          Fn1=b;                             
                          if(Fn>ent){break;    
                          }                            
                      }
                      break;      
                  }  
                  case 2:{
                       System.out.println("Enter a number to determine the count of the number nth. : ");
                        ent = Integer.parseInt(entrada.readLine());
                      System.out.println("Its sequel: ");
                      for(int i=1;i<=ent;i++){            
                          System.out.println(Fn+";"+"  Contador: "+i);
                          b=Fn;  
                          Fn=Fn+Fn1;  
                          Fn1=b; 
                      }
                  }  
                  break;
                  default:
                      System.out.println("Enter the correct option!");
                      
                      }
     
            
    } catch (Exception e){
            System.out.println("Enter a correct value!");
     }
     }

}
Crsdits:
@6ixth
One cool way to calculate Fibonacci is using the golden ratio:
Code:
long fib(int n){
double phi = (1+Math.sqrt(5))/2.0;
return Math.floor((Math.pow(phi,n)/Math.sqrt(5))+0.5d);
}
Quote Originally Posted by Saltine View Post

One cool way to calculate Fibonacci is using the golden ratio:
Code:
long fib(int n){
double phi = (1+Math.sqrt(5))/2.0;
return Math.floor((Math.pow(phi,n)/Math.sqrt(5))+0.5d);
}
Thanks very much man! /gew
Why did you use all this bufferedReader and inputStream stuff? Why not just use a simple scanner?
Really bad quality code (no offense).

The fact that you repeat code twice in your switch is enough to see that you should use a method. I suggest you learning some about recursion:

Code:
public class Fibonacci{

  public static void main(String args[]){
     Scanner scanner = new Scanner(System.in);
     int opt;
     // menu with the 2 options here.
     if(opt == 1){
       int ent = scanner.nextInt();
       System.out.println(fib(ent));
     }else{
       System.out.println(fib(1000));
     }
  }

  public static long fib(long n){
     long ret=0;
     if(n==0 || n==1){
        ret = pos;
     }else{
        ret = fib(pos-2)+fib(pos-1);
     }
     return ret;
 }

}
This implementation is much better than yours. But if you see the algorithm step by step, you will see it could be much more efficient because some calls would return the same number.

Imagine: 5
ret = fib(3) + fib(4)

Now fib(4) will return:
ret = fib(2) + fib(3) Yeah, fib(3) again.
I did a simple one... you can modify it and make say how many numbers you want it to make.

class main {

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


int x=1;
int anterior = 0;
int temp;
while (true)
{
System.out.println(x);
temp = x;
x= x+anterior;
anterior = temp;
if(x>30)
{break;}
}

}

}
One of the biggest things in programming is to be efficient and avoid redundancies. Keep that in mind for next time!
Posts 17 of 7 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?