I'm surprised nobody's given the obvious algorithm yet: while(i Before anyone complains, this algorithm is correct and does not break any of the rules as far as I can tell. :) I think the timing attack is probably what he's really looking for. Edit: as mattvanhorn pointed out, answer() is void, but that's ok... changed to a "constant time" algorithm. :)
answer() has a void return, although I suppose you might be able to watch System.out to see if it worked.
Java Puzzle: Square Root
41–50 of 61 posts
Re: Java Puzzle: Square Root
#42Consider that SecureRandom is really a facade around multiple providers that can plug in varying implementations. :)
SquareRoot.n is static, so it runs before any code in your main().
Re: Java Puzzle: Square Root
#43Re: Java Puzzle: Square Root
#44Subclass BigInteger, and override equals() on your subclass to "return true;"?
Re: Java Puzzle: Square Root
#45I'd start by trying to create an evil subclass of BigInteger, as it is not a final class.
Re: Java Puzzle: Square Root
#46Subclass BigInteger, and override equals() on your subclass to "return true;"?
equals() isn't being called on your class; it's being called on the result of n.divide()
That said, can't you override the equals() for your subtype? I'm talking about the implementation of BigInteger::equals(MyBigInt x), not MyBigInt::equals(BigInteger x). Or does that need to be done on BigInteger-proper, versus its subclass?
Re: Java Puzzle: Square Root
#47Consider that SecureRandom is really a facade around multiple providers that can plug in varying implementations. :)
...and that you can remove all the existing ones and insert your own.
SecurityManager.checkSecurityAccess(java.lang.String) is called if you try and mess with the Providers.
Re: Java Puzzle: Square Root
#48I'm surprised nobody's given the obvious algorithm yet: while(i Before anyone complains, this algorithm is correct and does not break any of the rules as far as I can tell. :) I think the timing attack is probably what he's really looking for. Edit: as mattvanhorn pointed out, answer() is void, but that's ok... changed to a "constant time" algorithm. :)
An i7 does about 109 gigaFLOPS, or 109 operations per nanosecond. [1]
Suppose we can do 1 guess per operation. There are 2^10,000 possible roots. [2]
2^10,000 / 109 nanoseconds is 5.804×10^2991 years. [3]
The stars will burn out before you brute force it.
[1] http://en.wikipedia.org/wiki/FLOPS
[2] http://bit.ly/ZIWkkt (parens in the original link)
[3] http://www.wolframalpha.com/input/?i=2%5E10%2C000+%2F+109+na...
Re: Java Puzzle: Square Root
#49I'm surprised nobody's given the obvious algorithm yet: while(i Before anyone complains, this algorithm is correct and does not break any of the rules as far as I can tell. :) I think the timing attack is probably what he's really looking for. Edit: as mattvanhorn pointed out, answer() is void, but that's ok... changed to a "constant time" algorithm. :)
The BigInteger is really big. An i7 does about 109 gigaFLOPS, or 109 operations per nanosecond. [1] Suppose we can do 1 guess per operation. There are 2^10,000 possible roots. [2] 2^10,000 / 109 nanoseconds is 5.804×10^2991 years. [3] The stars will burn out before you brute force it. [1] http://en.wikipedia.org/wiki/FLOPS [2] http://bit.ly/ZIWkkt (parens in the original link) [3] http://www.wolframalpha.com/input/?i…
Re: Java Puzzle: Square Root
#50Earlier quoted context omitted.
equals() isn't being called on your class; it's being called on the result of n.divide()
I don't know Java, clearly, as I don't even know what to name a file that's going to do the override. Also, as mentioned elsewhere, SecureRandom seems the easier target as it's a pluggable interface for entropy providers. That said, can't you override the equals() for your subtype? I'm talking about the implementation of BigInteger::equals(MyBigInt x), not MyBigInt::equals(BigInteger x). Or does that need to be done…
if (n.divide(root).equals(root)) {
It never calls root.equals(). It calls .equals() on the result of the .divide(), which is a standard java.math.BigInteger.