Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BigInteger: strange problem

 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4467
3
Java Python Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
never mind. this is the second time i made this mistake.
should be count = count.add(quotient);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic