/**
* 雷劈数:两个数和的平方恰好等于这两个数直接拼接起来
* 举例:(494+209)² = 494209
* 思路:设上式为(x+y)² = z, z = x * 10^N+y,其中N为y的位数减1。
*/
public class 雷劈数 {
static final long[] POWERS_OF_10 = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
public static void main(String[] args) {
for (long x = 0; x < 10000; x++) {
for (long y = 0; y < 10000; y++) {
long sumPower = (x + y) * (x + y);
long powerSum = x * (power10(log10(y))) + y;
if (sumPower == powerSum)
System.out.printf("(%d+%d)² = %d\n", x, y, sumPower);
}
}
}
static long power10(long pow) {
return POWERS_OF_10[(int) pow];
}
static long log10(long num) {
if (num < 10) return 1;
if (num < 100) return 2;
if (num < 1000) return 3;
if (num < 10000) return 4;
if (num < 100000) return 5;
if (num < 1000000) return 6;
if (num < 10000000) return 7;
if (num < 100000000) return 8;
if (num < 1000000000) return 9;
return 0;
}
}