Results 1 to 18 of 18
  1. #1
    zoala001 is offline Member
    Join Date
    Jan 2011
    Posts
    15
    Rep Power
    0

    Unhappy 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




    import java.util.*;

    public class Fibona {

    public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    //Prompt user for length

    System.out.println("How many fib terms would you like to display? ");

    int lenght = scan.nextInt();


    int[] p = new int[lenght]; //set new array p equal the defined user length



    printTerms(p, lenght); //method to print terms

    }


    //main end

    //method to populate and calculate array and fib sequence

    public static int printTerms( int[] p, int 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 == 1 )

    System.out.println(p[1]);
    else if ( lenght == 2 )

    System.out.println( p[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 = 3; index <= lenght; index++) {


    newTerms = p[1] + p[2];


    System.out.print( newTerms + " ");

    p[1] = p[2];


    p[2] = newTerms;

    }

    }

    return newTerms;

    }
    }

  2. #2
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    When you deal with big numbers, use BigInteger class.

  3. #3
    zoala001 is offline Member
    Join Date
    Jan 2011
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by JavaHater View Post
    When you deal with big numbers, use BigInteger class.
    i dont get it. i am not expert in javaD i am just a beginer.

  4. #4
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    then go to the documentation

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by zoala001 View Post
    i dont get it. i am not expert in javaD i am just a beginer.
    Integral numbers are stored as a 32 bit signed number so the largest number that can be stored is 2^31-1 which is a bit larger than 2 billion. If you want to be able to store numbers larger than that you either need longs (they can store numbers up to a value 2^63-1 in eight bytes) but if you want to store numbers without a fixed upper bound you have to use BigIntegers. Read the API documentation for that class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Please don't crosspost without mentioning the link to the other post(s): link.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    zoala001 is offline Member
    Join Date
    Jan 2011
    Posts
    15
    Rep Power
    0

    Default

    Please guysssssssss just give me code:(((((((( i dont know how to change my code to BigInteger:(((((
    i'm going to fail this:(((((( pleaseeeeeeeeee

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by zoala001 View Post
    Please guysssssssss just give me code:(((((((( i dont know how to change my code to BigInteger:(((((
    i'm going to fail this:(((((( pleaseeeeeeeeee
    Have you read the API documentation of the BigInteger class? You have to do your own homework otherwise you won't learn anything; although some poster here may dump an entire boilerplate solution for you but not me.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    zoala001 is offline Member
    Join Date
    Jan 2011
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Have you read the API documentation of the BigInteger class? You have to do your own homework otherwise you won't learn anything; although some poster here may dump an entire boilerplate solution for you but not me.

    kind regards,

    Jos
    i dont need to learn Java. I'm studying Electronics and this the only programming i have for my whole life. i'm done with all of my projects. this the only one left. if i dont do this im gonna fail the whole course. that means i have done 20 projects for nothing

  10. #10
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    although some poster here may dump an entire boilerplate solution for you but not me.
    yeah, you are the righteous one. Hail the righteous one.

    Let me be the devil then.

    @OP, here's how you can do it.
    Java Code:
    import java.math.BigInteger;
    public class Fibonacci {
            private static BigInteger TWO= BigInteger.valueOf(2);
            public static BigInteger fibonacci( BigInteger num){
                    if (num.equals (BigInteger.ZERO) || num.equals( BigInteger.ONE) ){
                            return number;
                    }else{
                            return fibonacci( numsubtract( BigInteger.ONE) ).add( fibonacci( num.subtract(TWO) ));
                    }
            }
            public static void main(String[] args){
                    for( int i = 0 ; i<=50 ;i++){
                            System.out.println( fibonacci( BigInteger.valueOf( i ) ) );
                    }
            }
    
    }
    Not going to explain to you. Its your job to find out for yourself what the code means. Read the API documentation, run and test code and do whatever.....

  11. #11
    zoala001 is offline Member
    Join Date
    Jan 2011
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by JavaHater View Post
    yeah, you are the righteous one. Hail the righteous one.

    Let me be the devil then.

    @OP, here's how you can do it.
    Java Code:
    import java.math.BigInteger;
    public class Fibonacci {
            private static BigInteger TWO= BigInteger.valueOf(2);
            public static BigInteger fibonacci( BigInteger num){
                    if (num.equals (BigInteger.ZERO) || num.equals( BigInteger.ONE) ){
                            return number;
                    }else{
                            return fibonacci( numsubtract( BigInteger.ONE) ).add( fibonacci( num.subtract(TWO) ));
                    }
            }
            public static void main(String[] args){
                    for( int i = 0 ; i<=50 ;i++){
                            System.out.println( fibonacci( BigInteger.valueOf( i ) ) );
                    }
            }
    
    }
    Not going to explain to you. Its your job to find out for yourself what the code means. Read the API documentation, run and test code and do whatever.....


    thanx alot . you r not devil:)) u r my angel:))))

    just a question: the prog. can not resolve number, and numsubtract. what should i change it to? i think i should change number to num but i have no idea how to change the numsubtract

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JavaHater View Post
    yeah, you are the righteous one. Hail the righteous one.

    Let me be the devil then.
    You may not even realize that forums have gotten in trouble because of posters like you: not only did you ruin yet another thread but you ruined the assignments of the student(s). Schools and/or universities do not like that and they have complained about it (e.g. the Sun/Oracle forums). I reported you again.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    zoala001 is offline Member
    Join Date
    Jan 2011
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by zoala001 View Post
    thanx alot . you r not devil:)) u r my angel:))))

    just a question: the prog. can not resolve number, and numsubtract. what should i change it to? i think i should change number to num but i have no idea how to change the numsubtract
    ok i got it:) but just one question left:)
    why does it take long time to print the numbers?

  14. #14
    zoala001 is offline Member
    Join Date
    Jan 2011
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You may not even realize that forums have gotten in trouble because of posters like you: not only did you ruin yet another thread but you ruined the assignments of the student(s). Schools and/or universities do not like that and they have complained about it (e.g. the Sun/Oracle forums). I reported you again.

    Jos
    hey its not your bussinness and this forum is not yours. he is trying to help me. you r such a jerk. poeple should help each other. they shouldnt report eachother like spoiled childs. anyway i'm not going to use this code cuz i have to use arrays to write the code.
    Last edited by zoala001; 01-02-2011 at 12:35 PM.

  15. #15
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    You may not even realize that forums have gotten in trouble because of posters like you: not only did you ruin yet another thread but you ruined the assignments of the student(s). Schools and/or universities do not like that and they have complained about it (e.g. the Sun/Oracle forums). I reported you again.

    Jos
    wrong! this is another way to learn as well. Teachers/Schools are not stupid to pass students who hand in exact code like this without explanation or comments. That's why I expect students/OP to learn how its done by looking at code and testing/running the code, and see for themselves. Its way better than your method.
    why don't you blame sites that allow students to buy homework as well? Like studentoffortune and lots of other similar sites. Students pay for people to do their homework. I think they
    are the biggest culprits for producing what you call shoddy programmers.

    Report me? Go ahead. I don't care. And by the way, you are not God.
    Last edited by JavaHater; 01-02-2011 at 01:08 PM.

  16. #16
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by zoala001 View Post
    thanx alot . you r not devil:)) u r my angel:))))

    just a question: the prog. can not resolve number, and numsubtract. what should i change it to? i think i should change number to num but i have no idea how to change the numsubtract
    I leave that little bug to you. Its basic Java. Try to debug yourself.

  17. #17
    zoala001 is offline Member
    Join Date
    Jan 2011
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by JavaHater View Post
    I leave that little bug to you. Its basic Java. Try to debug yourself.
    i did it . thankx alot however i am not going to use the code. cuz we r supposed to use arrays to write the code. anyways thankx

  18. #18
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by zoala001 View Post
    i did it . thankx alot however i am not going to use the code. cuz we r supposed to use arrays to write the code. anyways thankx
    That code is to show you about BigInteger. Its not the solution to your problem using this recursive approach. But you should note that you can still use BigInteger in your own program using arrays approach.

Similar Threads

  1. help with fibonacci
    By likemine in forum New To Java
    Replies: 8
    Last Post: 01-07-2010, 02:32 AM
  2. Fibonacci summation problem
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 12-01-2009, 06:28 PM
  3. Generating Fibonacci Series with a Multithreaded Java Program
    By firesauce in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-20-2009, 07:26 AM
  4. help with fibonacci problem
    By thekrazykid in forum New To Java
    Replies: 4
    Last Post: 12-12-2008, 10:41 PM
  5. Fibonacci Algorithm
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •