# Java 吸血鬼数字

吸血鬼数字是指位数为偶数的数字，可以由一对数字相乘而得到，而这对数字各包含乘积的一半位数的数字，其中从最初的数字中选取的数字可以任意排序.

例如下列数字都是吸血鬼数字

1260=21*60

1827=21*87

2187=27*81

``````private static void vampireNum() { // 官方参考答案

int[] startDigit = new int[4];
int[] productDigit = new int[4];
for (int num1 = 10; num1 <= 99; num1++)
for (int num2 = num1; num2 <= 99; num2++) {
// Pete Hartley's theoretical result: // If x·y is a vampire number then // x·y == x+y (mod 9) if ((num1 * num2) % 9 != (num1 + num2) % 9) continue; int product = num1 * num2; startDigit[0] = num1 / 10; startDigit[1] = num1 % 10; startDigit[2] = num2 / 10; startDigit[3] = num2 % 10; productDigit[0] = product / 1000; productDigit[1] = (product % 1000) / 100; productDigit[2] = product % 1000 % 100 / 10; productDigit[3] = product % 1000 % 100 % 10; int count = 0; for (int x = 0; x < 4; x++) for (int y = 0; y < 4; y++) { if (productDigit[x] == startDigit[y]) { count++; productDigit[x] = -1; startDigit[y] = -2; if (count == 4) System.out.println(num1 + " * " + num2 + " : " + product); } } } } 复制代码``````

Let mod be the binary modulo operator and d(x) the sum of the decimal digits of x.

It is well-known that d(x) mod 9 = x mod 9, for all x.

Assume x·y is a vampire. Then it contains the same digits as x and y, and in particular d(x·y) = d(x)+d(y). This leads to:

(x·y) mod 9 = d(x·y) mod 9 = (d(x)+d(y)) mod 9 = (d(x) mod 9 + d(y) mod 9) mod 9

= (x mod 9 + y mod 9) mod 9 = (x+y) mod 9

The solutions to the congruence are (x mod 9, y mod 9) in {(0,0), (2,2), (3,6), (5,8), (6,3), (8,5)}

Only these cases (6 out of 81) have to be tested in a vampire search based on testing x·y for different values of x and y.

x = a+10b +100c+1000d = a+b+c+d+9b+99c+999d所以xmod 9 = (a+b+c+d)mod 9 = d(x)mod 9,证明完毕。

原文作者：weixin_33980459
原文地址: https://blog.csdn.net/weixin_33980459/article/details/88001645
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。