### Concealed Square题号：206 难度： 5 中英对照

Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0,
where each “_” is a single digit.

### Code

public class p206 {

public static void main(String[] args) {
long start = System.nanoTime();
long result = run();
long end = System.nanoTime();
System.out.println(result);
System.out.println((end - start) / 1000000 + "ms");
}

public static long run() {
// Initialize
// [nMin,nMax] 为X'取值的区间,所求答案为: X=10*(10*X'+ 3|7)
long nMin = 10101010;
long nMax = 13890266;
long num;  //表达式中的(10*X'+ 3|7)
long temp;
long temp2;
// ndigits 数组 保存 num 的平方,
int[] ndigits = new int[17];  // Based on length of pattern

for (long n = nMin; n <= nMax; n++) {
num = 10 * n + 3; // X倒数第二位为3时
temp = num;
temp2 = temp * temp;
//for 循环将平方结果从低位到高位存储在数组中
for (int i = 0; i < ndigits.length; i++, temp2 /= 10)
ndigits[i] = (int) (temp2 % 10);
if (isConcealedSquare(ndigits)) {
return 10 * temp;
}

num = 10 * n + 7; // X倒数第二位为7时
temp = num;
temp2 = temp * temp;
for (int i = 0; i < ndigits.length; i++, temp2 /= 10)
ndigits[i] = (int) (temp2 % 10);
if (isConcealedSquare(ndigits)) {
return 10 * num;
}

}
return -1;
}

//判断n是否是满足条件的平方数,即 n 满足形式 1_2_3_4_5_6_7_8_9
private static boolean isConcealedSquare(int[] n) {
for (int i = 1; i <= 9; i++) {  // Scan for 1 to 9
if (n[18 - i * 2] != i)
return false;
}
return true;  // 符合条件
}

}
1389019170
273ms