Java Puzzle: Square Root
corner.squareup.com
corner.squareup.com
SecurityManager.checkSecurityAccess(java.lang.String) is called if you try and mess with the Providers.
Trying to write a timing attack sounds much more interesting.
if (n.divide(root).equals(root)) {
Try to set it to 2^20001 on the first funcall and 0 on the second.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...
Immutability is your friend. Use it or lose it.
edit: Yeah… just ignore this and look at the other answers ;)
I suspect that doing something with the random seed is more what the author had in mind, though.
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?
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.I'm not sure anyone will bother writing the timing attack since he apparently gives out no prize.
I'm familiar with Java, but not so familiar that I can state with authority that such a thing is impossible.