programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# How to make it stop at F(56)?

Ranch Hand
Posts: 58
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.
Thanks.

Here's my code so far:

lowercase baba
Bartender
Posts: 12601
50
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.

Rancher
Posts: 2754
31

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
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

Rancher
Posts: 2754
31
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
How do I fix this?

Rancher
Posts: 2754
31
• 1
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
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
Because of this problem I can print this:
"F(46) is out of the range of int "

Sheriff
Posts: 23265
46
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.

Rancher
Posts: 2754
31
My suggestion was in the last line of my last post.

Lindsey Brooks
Ranch Hand
Posts: 58
oh. Thanks

Marshal
Posts: 57443
175

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.

 Hot dog! An advertiser loves us THIS much: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS