File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes what am i doing wrong here? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "what am i doing wrong here?" Watch "what am i doing wrong here?" New topic
Author

what am i doing wrong here?

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

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


SCJP
Visit my download page
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

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

Joined: Oct 21, 2000
Posts: 4340
    
    2

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
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2271
    
  28

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

Joined: Oct 21, 2000
Posts: 4340
    
    2

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what am i doing wrong here?
 
Similar Threads
is the number just too big?
BigInteger Power/Exponent BigInteger
Finding the largest Prime Factor
Project Euler Problem 25
Fibonacci Sequence Problem