Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: problem to get Fibonacci series- please help.

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem to get Fibonacci series- please help.

    hi guys.

    i have written a prog. wich prints out the the first 5o numbers. but unfortunatly i get negative and diffrent number in the last 3 number instead of getting postive number.
    please help otherwise i will fail the whole course(

    i got
    1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 -1323752223 512559680 -811192543


    but i have to get 2971215073, 4807526976 and 7778742049




    PHP Code:
    [LEFT]public class Fibona {
             public static 
    void main(String[] args) {
                 
    Scanner scan = new Scanner(System.in); //create new scanner
             
         

                //Prompt user for length
                
    System.out.println("How many fib terms would you like to display? ");
                
    int lenght scan.nextInt();

            
    int[] = new int[lenght]; //set new array p equal the defined user length
            
         
            
    printTerms(plenght); //method to print terms

             
    }
            
            
    //main end
        //method to populate and calculate array and fib sequence
                
    public static int printTermsint[] pint lenght ) {
             
                    
    //print first two terms
                
    p[1] = 0;
                    
    p[2] = 1;
                    
                    
    int newTerms 0//new value to calculate new terms on the sequence other that the first two
             
                    
    if ( lenght == )
                    
    System.out.println(p[1]);
                    else if ( 
    lenght == )
                        
    System.out.printlnp[1] + " " p[2]);
                    else { 
    //print rest of terms
             
                        
    System.out.print( p[1] + " " p[2] + " ");
         
                    
    //for loop to calculate the rest of the terms
                        
    for ( int index 3index <= lenghtindex++) {
             
                            
    newTerms p[1] + p[2];
                            
    System.out.print( newTerms " ");
                        
    p[1] = p[2];
                        
                        
    p[2] = newTerms;
                        
                        }
             
                    }
        
                return 
    newTerms;
             
                } 
                }[/
    LEFT


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem to get Fibonacci series- please help.

    See Data Types, as well as the demo below...an int is 32 bit, and can only hold a max value that can fit into the 32 bits.
    	public static void main(String[] args){		
    		int i = 1836311903;
    		i *= 2;
    		System.out.println(i);
    		long l = 1836311903;
    		l *= 2;
    		System.out.println(l);
    	}

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem to get Fibonacci series- please help.

    Quote Originally Posted by copeg View Post
    See Data Types, as well as the demo below...an int is 32 bit, and can only hold a max value that can fit into the 32 bits.
    	public static void main(String[] args){		
    		int i = 1836311903;
    		i *= 2;
    		System.out.println(i);
    		long l = 1836311903;
    		l *= 2;
    		System.out.println(l);
    	}
    I have no idea what you are talking about. what is the code yoou have written ?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem to get Fibonacci series- please help.

    Did you read the link? Did you run the code? Did you read my description? An int is limited in the amount of information it can hold (32 bit), and long value can hold more information (64 bit). The values turn negative because you've maxed out that information. In other words, think about using a primitive other than an int and see what happens

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Location
    Voorschoten, the Netherlands
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem to get Fibonacci series- please help.

    @OP: please don't crosspost without mentioning a link to the other post: link

    kind regards,

    Jos

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: problem to get Fibonacci series- please help.

    Instead of using an int try using a longto store your result.

    This means changing the array p to type long and changing the variable newTerms to long
    Last edited by Orthidax; January 3rd, 2011 at 01:16 AM.

Similar Threads

  1. Generate prime numbers within fiboncacci series?
    By Manish87 in forum Algorithms & Recursion
    Replies: 5
    Last Post: August 7th, 2010, 12:24 PM
  2. Fibonacci Spiral Help?
    By cmh0114 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 12th, 2010, 09:21 PM
  3. Replies: 1
    Last Post: October 19th, 2009, 11:53 PM
  4. [SOLVED] displaying sum of harmonic series
    By sriraj.kundan in forum Algorithms & Recursion
    Replies: 2
    Last Post: June 15th, 2009, 11:42 PM
  5. [SOLVED] Problem in generating Fibonacci sequence in java
    By big_c in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 24th, 2009, 08:52 AM