definition: a vampire number is an even number of digits, which can be multiplied by a pair of numbers, which each contain a number of half-digits of the product, where the number selected from the initial number can be arbitrarily ordered, and a number ending in two 0 is not allowed. such as 1260 = 21 * 60,2187 = 27 * 81, etc.
In this example, all vampire numbers in the 4-digit number are implemented.
Core code:
public static void Main (string[] args) {//Vampire number counter int count = 0;//product number int m = 0;//character group, matching char[] a,b;//loop cycles int loop = 0;for (int i=10;i<100;i++) {//math.max (1000/i, i+1) can be understood as i*j>1000 && j>ifor (int j=math.max (1000/i, i+1 ); j<100;j++) {//calculated product M = i*j;//filter if (m<1000 | | m%100==0 | | (M-i-J)% 9! = 0) {continue;} The number of active cycles loop++;//the product number to the character array a = String.valueof (M). ToCharArray ();//two digit to character array B = (string.valueof (i) +string.valueof (j)) . ToCharArray ();//Sort Arrays.sort (a); Arrays.sort (b); if (Arrays.equals (A, B)) {count++; System.out.println ("+count+" vampire number, "+m+": "+i+" * "+j");}} System.out.println ("Number of active cycles:" +loop+ ".");
Operation Result:
1th Vampire number, 1395:15 * 93 2nd Vampire number, 1260:21 * 60 3rd Vampire number, 1827:21 * 87 4th Vampire number, 2,187:27 * 81 5th Vampire number, 1530: 30 * 51 6th Vampire number, 1435:35 * 41 7th Vampire number, 6,880:80 * 86 effective cycle count: 229.
Description:
About (M-i-j)% 9!= = 1000a+100b+10c-d any 2-bit combination, such as AC,DB, represented as 10a+c,10d+-ac-db = 990a+99b+9c-
-i-j)% 9! = 0
Java implementation of 4-digit vampire digital algorithm