Java Puzzle: Square Root

Show your Java-fu by calculating the unknown.

Reddit
LinkedIn

Written by Wouter Coekaerts.

There are several algorithms to calculate a square root. But to solve this puzzle, you’ll need a different approach.

Can you find the square root of a huge number, without even looking at it?

package square**;**

import java.math.BigInteger;
import java.security.SecureRandom;

public class SquareRoot {
  public static final int BITS = 10000;

  private static BigInteger n =
    new BigInteger(BITS, new SecureRandom()).pow(2);

  public static void answer(BigInteger root) {
    if (n.divide(root).equals(root)) {
      // The goal is to reach this line
      System.out.println("Square root!");
    }
  }
}

Write a class that calls SquareRoot.answer, and reaches that line in the code. The rules:

  • Using setAccessible takes all the fun out of the problem, so your code must run with the security manager enabled (java -Djava.security.manager your.Class).
  • Solve the problem in a single separate .java file which compiles and runs with JDK 6 or 7.
  • Finding and exploiting security vulnerabilities in the JDK itself is interesting, but not the point of this puzzle.

Put your solution in a secret gist, and add a link to it in a comments below. To give everyone a chance to participate without spoilers the comments will stay private for a week.

Good luck!

Update: See this post about the solution Wouter Coekaerts Follow the latest activity of Wouter Coekaerts on Medium to see their stories and recommends.medium.com