Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

BigInteger: strange problem

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4716
9
Scala Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
never mind. this is the second time i made this mistake.
should be count = count.add(quotient);
 
reply
    Bookmark Topic Watch Topic
  • New Topic