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[] 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;
}
}[/LEFT]
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.
Code java:
public static void main(String[] args){
int i = 1836311903;
i *= 2;
System.out.println(i);
long l = 1836311903;
l *= 2;
System.out.println(l);
}
Re: problem to get Fibonacci series- please help.
Quote:
Originally Posted by
copeg
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.
Code java:
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 ?
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
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
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