Results 1 to 7 of 7
  1. #1
    6ixth's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Posts
    3,033
    Reputation
    661
    Thanks
    19,904

    Post 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:
    @ForeverRed

  2. The Following User Says Thank You to 6ixth For This Useful Post:

    FUFYN (05-05-2013)

  3. #2
    Saltine's Avatar
    Join Date
    Jun 2011
    Gender
    male
    Posts
    493
    Reputation
    104
    Thanks
    629
    Quote Originally Posted by ForeverRed 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:
    @ForeverRed
    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);
    }

    Oh no! Vortex is gay!

  4. The Following User Says Thank You to Saltine For This Useful Post:

    6ixth (05-08-2013)

  5. #3
    6ixth's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Posts
    3,033
    Reputation
    661
    Thanks
    19,904
    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

  6. #4
    IIunknownII's Avatar
    Join Date
    May 2009
    Gender
    male
    Posts
    446
    Reputation
    3
    Thanks
    26
    Why did you use all this bufferedReader and inputStream stuff? Why not just use a simple scanner?
    I'll change my profile pic when i get around to it, I originally put it there when i was 13, sorry.

  7. The Following User Says Thank You to IIunknownII For This Useful Post:

    ecHarley (06-29-2013)

  8. #5
    ecHarley's Avatar
    Join Date
    Jun 2013
    Gender
    male
    Posts
    7
    Reputation
    10
    Thanks
    0
    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.
    Last edited by ecHarley; 06-29-2013 at 10:02 AM.

  9. #6
    “I fear the day technology will surpass our human interaction. The world will have a generation of idiots.” ~Albert Einstein
    MPGH Member
    SteamAss's Avatar
    Join Date
    Nov 2010
    Gender
    male
    Location
    Crossfire
    Posts
    2,278
    Reputation
    28
    Thanks
    770
    My Mood
    Asleep
    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;}
    }

    }

    }



    If you need my Help:
    PM/VM

    Because The People Who Are Crazy Enough To Think They Can Change The World, Are The Ones Who Do. ~Steve Jobs

  10. #7
    The Void Aroma's Avatar
    Join Date
    May 2014
    Gender
    male
    Location
    United States
    Posts
    20
    Reputation
    10
    Thanks
    1
    One of the biggest things in programming is to be efficient and avoid redundancies. Keep that in mind for next time!

Similar Threads

  1. [Java Tutorial] Need Java Books
    By itz me_ in forum Programming Tutorial Requests
    Replies: 1
    Last Post: 11-26-2012, 08:38 PM
  2. a good java program
    By snipelock in forum Java
    Replies: 18
    Last Post: 04-17-2009, 02:56 PM
  3. Java based multiplayer games
    By Tops in forum Hack Requests
    Replies: 8
    Last Post: 03-13-2008, 11:28 PM
  4. OMG I SO STUCK!!!(Java Script "n" html problem
    By jeremywilms in forum Programming
    Replies: 11
    Last Post: 06-15-2006, 01:23 PM
  5. Java
    By Sean Johnson in forum General Game Hacking
    Replies: 2
    Last Post: 02-07-2006, 01:53 PM