Find all vampire numbers with four digits (JAVA)

/**
* Find all four-digit vampire numbers
* A vampire number is a number with an even number of digits, which can be obtained by multiplying a pair of numbers that each contain half the number of digits of the product, where The numbers chosen from the initial numbers can be ordered arbitrarily.
* Numbers ending in two zeros are not allowed.
* For example, the following numbers are all vampire numbers
1260=21*60
1827=21*87
2187=27*81

* A stupid and inefficient approach: Iterate over all four digits, and each time a four-digit number is generated,
* in The double loop traverses two digits, and judges whether it is equal to the four digits of the outermost loop in the inner loop of the two digits. If they are equal, store these numbers in the array, sort and compare
* the two sets of numbers, if they are equal, then the output is the number you are looking for;

*/

Check out this English reference: Vampire Numbers

An important theoretical result found by Pete Hartley:
1. If x·y is a vampire number then x·y == x+y (mod 9) Proof: Let mod be the binary modulo operator and d(x) the sum of the decimal
digits of x.
2. It is well-known that d(x) mod 9 = x mod 9, for all x.
Assume x·y is a vampire.
3.Then it contains the same digits as x and y,and in particular d(x·y) = d(x)+d(y).
4.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.
public class Exercise10 {

JAVA implementation:

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        int num1, num2, result, i, j, 
            count = 0;
        int[] predata = new int[4];
        int[] lastdata = new int[4];

        for(num1 = 10; num1 < 99; num1++)
            for(num2 = num1; num2 < 99; num2++){
                result = num1 *num2;
                count = 0;
                if(((num1 * num2) %9) !=((num1 + num2) %9))
                    continue;
                predata[0] = num1 / 10;
                predata[1] = num1 % 10;
                predata[2] = num2 / 10;
                predata[3] = num2 % 10;
                lastdata[0] = result / 1000;
                lastdata[1] = (result % 1000) / 100;
                lastdata[2] = (result % 1000 % 100) / 10;
                lastdata[3] = (result % 1000 % 100 % 10);
                for(i = 0; i < 4 ; i++)
                    for(j = 0; j < 4; j++){
                        if(predata[i] == lastdata[j]){
                            count++;
                            predata[i] = -1;
                            lastdata[j] = -2;
                        }
                    }
                if(count == 4)
                    System.out.println(num1 +" * " + num2 + " = "+ result);
            }

    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325925854&siteId=291194637