Skip to content

Instantly share code, notes, and snippets.

@joriki
Created May 14, 2020 16:55
Show Gist options
  • Save joriki/c28395034f6a3cae67fd3309d85b72b5 to your computer and use it in GitHub Desktop.
Save joriki/c28395034f6a3cae67fd3309d85b72b5 to your computer and use it in GitHub Desktop.
Find perfect squares whose difference is a repunit; see https://math.stackexchange.com/questions/3674784.
import java.math.BigInteger;
public class Question3674784 {
public static void main(String [] args) {
for (int n = 1;;n++) {
System.out.println(n);
BigInteger diff = BigInteger.TEN.pow(n).subtract(BigInteger.ONE).divide(BigInteger.TEN.subtract(BigInteger.ONE));
BigInteger x = BigInteger.TWO;
BigInteger x2 = x.multiply(x);
BigInteger y = BigInteger.ONE;
BigInteger y2 = y.multiply(y);
do {
while (x2.subtract(y2).compareTo(diff) < 0) {
x = x.add(BigInteger.ONE);
x2 = x.multiply(x);
}
if (x2.subtract(y2).equals(diff)) {
System.out.println(x + "^2 - " + y + "^2 = " + x2 + " - " + y2 + " = " + diff);
x = x.add(BigInteger.ONE);
x2 = x.multiply(x);
}
while (x2.subtract(y2).compareTo(diff) > 0) {
y = y.add(BigInteger.ONE);
y2 = y.multiply(y);
}
} while (!x.equals(y));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment