• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make it stop at F(56)?  RSS feed

 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
               I want to list all the Fibonacci numbers, which can be expressed as an int.
I know that integer is in the range of [-2147483648, 2147483647] but some how my code is not working.
Please help.
Thanks.


Here's my code so far:

 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you mean by "my code is not working?" That could mean anything from it doesn't compile to it doesn't run to it doesn't output the right answer.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my code is not working.

Please explain.  Post any output that shows the problem and add some comments saying what is wrong.
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's supposed to print

F(0) = 1
F(1) = 1
F(2) = 2
...
F(45) = 1836311903
F(46) is out of the range of int

but I got

D:\learn_java_2016\Excercises>java FibonacciInt
n = 50
2147483647
-2147483648


F(0)=1
F(1)=1
F(2)=2
F(3)=3
F(4)=5
F(5)=8
F(6)=13
F(7)=21
F(8)=34
F(9)=55
F(10)=89
F(11)=144
F(12)=233
F(13)=377
F(14)=610
F(15)=987
F(16)=1597
F(17)=2584
F(18)=4181
F(19)=6765
F(20)=10946
F(21)=17711
F(22)=28657
F(23)=46368
F(24)=75025
F(25)=121393
F(26)=196418
F(27)=317811
F(28)=514229
F(29)=832040
F(30)=1346269
F(31)=2178309
F(32)=3524578
F(33)=5702887
F(34)=9227465
F(35)=14930352
F(36)=24157817
F(37)=39088169
F(38)=63245986
F(39)=102334155
F(40)=165580141
F(41)=267914296
F(42)=433494437
F(43)=701408733
F(44)=1134903170
F(45)=1836311903
F(46)=-1323752223
F(47)=512559680
F(48)=-811192543
F(49)=-298632863
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't test if the value in an int is greater than Integer.MAX_VALUE because that would mean the value was greater than the max. Not possible.
32 bits can hold a value of Integer.MAX_VALUE.  When that value is incremented, there is overflow and the value either goes negative or wraps back into a lower value.

For a simple example without signs, consider a variable that holds a single decimal digit with a max value of 9.  The value of the variable will never be larger than 9.  8+7 = 5 with a 10 overflow
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I fix this?
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you want fixed?
If you use the BigInteger class it will hold larger numbers.

I'm not sure how to detect overflow.  One way could be to test if the new value is less than the old one.
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm supposed to get

F(0) = 1
F(1) = 1
F(2) = 2
...
F(45) = 1836311903
F(46) is out of the range of int
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because of this problem I can print this:
"F(46) is out of the range of int "
 
Paul Clapham
Sheriff
Posts: 22520
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having the fibonacci method return a long value, rather than an int value, would allow it to return values which exceed the maximum possible int value and then you could compare the returned value to that maximum int value.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My suggestion was in the last line of my last post.
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh. Thanks
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:. . . I'm not sure how to detect overflow. . . .
I think there are methods in the Java8 version of the Integer class which throw exceptions if there is overflow, but I am not certain.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!