Results 1 to 18 of 18
 01012011, 03:14 PM #1Member
 Join Date
 Jan 2011
 Posts
 15
 Rep Power
 0
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;
}
}
 01012011, 03:21 PM #2Senior Member
 Join Date
 Dec 2010
 Posts
 165
 Rep Power
 4
When you deal with big numbers, use BigInteger class.
 01012011, 03:31 PM #3Member
 Join Date
 Jan 2011
 Posts
 15
 Rep Power
 0
 01012011, 03:48 PM #4Senior Member
 Join Date
 Dec 2010
 Posts
 165
 Rep Power
 4
then go to the documentation
 01012011, 04:55 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,730
 Blog Entries
 7
 Rep Power
 21
Integral numbers are stored as a 32 bit signed number so the largest number that can be stored is 2^311 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^631 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,
Joscenosillicaphobia: the fear for an empty beer glass
 01022011, 10:23 AM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,730
 Blog Entries
 7
 Rep Power
 21
Please don't crosspost without mentioning the link to the other post(s): link.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 01022011, 12:27 PM #7Member
 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
 01022011, 12:46 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,730
 Blog Entries
 7
 Rep Power
 21
cenosillicaphobia: the fear for an empty beer glass
 01022011, 12:53 PM #9Member
 Join Date
 Jan 2011
 Posts
 15
 Rep Power
 0
 01022011, 01:01 PM #10Senior Member
 Join Date
 Dec 2010
 Posts
 165
 Rep Power
 4
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 ) ) ); } } }
 01022011, 01:17 PM #11Member
 Join Date
 Jan 2011
 Posts
 15
 Rep Power
 0
 01022011, 01:27 PM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,730
 Blog Entries
 7
 Rep Power
 21
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.
Joscenosillicaphobia: the fear for an empty beer glass
 01022011, 01:28 PM #13Member
 Join Date
 Jan 2011
 Posts
 15
 Rep Power
 0
 01022011, 01:32 PM #14Member
 Join Date
 Jan 2011
 Posts
 15
 Rep Power
 0
Last edited by zoala001; 01022011 at 01:35 PM.
 01022011, 01:41 PM #15Senior Member
 Join Date
 Dec 2010
 Posts
 165
 Rep Power
 4
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; 01022011 at 02:08 PM.
 01022011, 01:43 PM #16Senior Member
 Join Date
 Dec 2010
 Posts
 165
 Rep Power
 4
 01022011, 01:46 PM #17Member
 Join Date
 Jan 2011
 Posts
 15
 Rep Power
 0
 01022011, 01:50 PM #18Senior Member
 Join Date
 Dec 2010
 Posts
 165
 Rep Power
 4
Similar Threads

help with fibonacci
By likemine in forum New To JavaReplies: 8Last Post: 01072010, 03:32 AM 
Fibonacci summation problem
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 12012009, 07:28 PM 
Generating Fibonacci Series with a Multithreaded Java Program
By firesauce in forum Threads and SynchronizationReplies: 1Last Post: 10202009, 08:26 AM 
help with fibonacci problem
By thekrazykid in forum New To JavaReplies: 4Last Post: 12122008, 11:41 PM 
Fibonacci Algorithm
By susan in forum New To JavaReplies: 1Last Post: 08072007, 05:25 AM
Bookmarks