1. Member
Join Date
Jan 2011
Posts
15
Rep Power
0

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.

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. Senior Member
Join Date
Dec 2010
Posts
165
Rep Power
6
When you deal with big numbers, use BigInteger class.

3. Member
Join Date
Jan 2011
Posts
15
Rep Power
0
Originally Posted by JavaHater
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. Senior Member
Join Date
Dec 2010
Posts
165
Rep Power
6
then go to the documentation

5. Originally Posted by zoala001
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

kind regards,

Jos

7. Member
Join Date
Jan 2011
Posts
15
Rep Power
0
Please guysssssssss just give me code:(((((((( i dont know how to change my code to BigInteger:(((((
i'm going to fail this:(((((( pleaseeeeeeeeee

8. Originally Posted by zoala001
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

9. Member
Join Date
Jan 2011
Posts
15
Rep Power
0
Originally Posted by JosAH
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. Senior Member
Join Date
Dec 2010
Posts
165
Rep Power
6
Originally Posted by JosAH
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. Member
Join Date
Jan 2011
Posts
15
Rep Power
0
Originally Posted by JavaHater
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. Originally Posted by JavaHater
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

13. Member
Join Date
Jan 2011
Posts
15
Rep Power
0
Originally Posted by zoala001
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. Member
Join Date
Jan 2011
Posts
15
Rep Power
0
Originally Posted by JosAH
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 01:35 PM.

15. Senior Member
Join Date
Dec 2010
Posts
165
Rep Power
6
Originally Posted by JosAH
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 02:08 PM.

16. Senior Member
Join Date
Dec 2010
Posts
165
Rep Power
6
Originally Posted by zoala001
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. Member
Join Date
Jan 2011
Posts
15
Rep Power
0
Originally Posted by JavaHater
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. Senior Member
Join Date
Dec 2010
Posts
165
Rep Power
6
Originally Posted by zoala001
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.

#### Posting Permissions

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