• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what am i doing wrong here?

 
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
euler 76 and 78 are very similar. i thought it would be a piece of cake. just have to use BigInteger instead of int
76


using BigInteger and adding one

this answer is correct
78

this returns 5997, and the System.out.println says 3000000
the correct answer is 55374
i tried changing
System.out.println(ways[j]);
to
System.out.println(ways[j].toString());
but get the same answer
i am guessing
if(ways[j].mod(limit) == BigInteger.ZERO)
is where the problem is, but not sure
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd say you've nailed it. int is a primitive type and can be compared using ==. BigInteger is an object, for which the == operator compares references. You need to use the equals method instead.
 
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
if(ways[j].mod(limit).equals(BigInteger.ZERO))
hmmm...i thought that would fix it but i get the same answer. i tried this also
if(ways[j].remainder(limit).equals(BigInteger.ZERO))
same result
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you print out the intermediate values within the loop in both implementations. You might see why it's diverging

Also, I see there is slight differrence in code

In the first version you have



In the next 2 versions you have


 
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
that difference is correct. #76 wants all combinations of 2 numbers that equal 100(up to 99 + 1).
#78 includes the case of all coins in one stack(100)
i might try it using long, but my guess is the number will be too big
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic