I want find the positive integer that, when taking the square of it, has the form 1_2_3_4_5_6_7_8_9_0
("_" = random number). I wrote this code:
BigInteger num = new BigInteger("1010000000");
while (String.valueOf(num).length() <= 19) {
if (String.valueOf(num.pow(2)).length() < 19) {
num = num.add(BigInteger.ONE);
} else {
if (check(num)) {
System.out.println("Answer: " + num);
break;
} else {
num = num.add(BigInteger.ONE);
}
}
}
}
public static boolean check(BigInteger contNum) {
boolean control = false;
String temp = String.valueOf((contNum.pow(2)));
String con = "";
for (int i = 0; i < 19; i = i + 2) {
con += temp.substring(i, i + 1);
}
if (con.equals("1234567890")) {
control = true;
}
System.out.println("con: " + con);
return control;
}
It does, however, never reach the form of 1234567890
(The string I extract from the original number). What is wrong with this code? Thanks.
Other version:
long num = 1099999999;
while (true) {
if (String.valueOf(Math.pow(num, 2)).length() < 19) {
System.out.println(String.valueOf(Math.pow(num, 2)).length());
num++;
System.out.println("Loading..");
} else if (String.valueOf(Math.pow(num, 2)).length() > 19) {
System.out.println(String.valueOf(Math.pow(num, 2)).length());
System.out.println("Not found!");
System.exit(0);
} else {
if (check(num)) {
System.out.println("Answer: " + num);
break;
} else {
num++;
}
}
}
}
public static boolean check(long contNum) {
boolean control = false;
String temp = String.valueOf(Math.pow(contNum, 2));
String con = "";
for (int i = 0; i < 19; i = i + 2) {
con += temp.substring(i, i + 1);
}
if (con.equals("1234567890")) {
control = true;
}
System.out.println("t: " + temp);
return control;
}