wood burning stoves 2.0*
The moose likes Java in General and the fly likes BigInteger: strange problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "BigInteger: strange problem" Watch "BigInteger: strange problem" New topic
Author

BigInteger: strange problem

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

i finally found a formula for solving euler116. it worked fine using longs and a smaller input but i had to change to BigInteger. the result is strange. i have these lines of code
BigInteger count = BigInteger.ZERO;

BigInteger quotient = num.divide(den);
System.out.println(quotient);
count.add(quotient);
System.out.println(count);

it prints out (for example)
9
0

how is this possible???
if quotient is 9 and count starts out being 0 it should print out 9!!!
i can post entire code if necessary


SCJP
Visit my download page
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

here is entire code


it prints out
362880
40320
9
0
40320
1440
28
0
5040
144
35
0
720
48
15
0
120
120
1
0
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

never mind. this is the second time i made this mistake.
should be count = count.add(quotient);
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: BigInteger: strange problem
 
Similar Threads
Recursion Problem
Base Conversions
Question About Nested Loops
BigInteger Operations
prime numbers