Java finds all four-digit vampire digital Base code examples

Source: Internet
Author: User
Tags static class


/**
* Find out four digits of all vampire numbers
* Vampire numbers are numbers with an even number of digits that can be multiplied by a pair of numbers, each of which includes the number of half-digits of the product, in which the numbers selected from the original numbers can be sorted arbitrarily.
* With two 0 The ending number is not agreeable.
* Proportions such as the following numbers are vampire numbers
1260=21*60
1827=21*87
2187=27*81
...
* Rather stupid inefficient approach: traverse all four digits, each generating a four-digit number,
* Traverse two digits in a double loop to infer whether the four-bit number is equal to the outermost loop in the inner loop of the two-digit number. Assuming equality to store these numbers in an array, the comparison

* Two sets of numbers, assuming equal then the output of this number is the number to find;

*/

Read the following English: vampire numbers

An important theoretical result found by Pete Hartley:
If x y is a vampire number thenx y = = x+y (mod 9)
Proof:
LetMoDBe the binary modulo operator andd (x)The sum of the decimal digits of X.
It is well-known this 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 is(x mod 9, y mod 9)inch{(0,0), (2,2), (3,6), (5,8), (6,3), (8,5)}
Only these cases (6 out of Bayi) has to be tested in a vampire search based on testing x y for different values of x an D y.

The following V method, which is still thinkinjava given the most efficient answer, other high-efficiency practices, please netizen master God to add

Import Java.util.arrays;public class Demo3 {static int A;       thousand static int b;       hundred static int C;       10-bit static int D;      single-digit static int A1;      10-bit static int B1;      single-digit static int C1;      10-bit static int d1; digit static int sum = 0; sum static int sum2 = 0;        Two numbers of the product public static void main (string[] args) {Long startTime = System.nanotime ();        Method1 ();        Long endTime = System.nanotime (); System.out.println ("Method1:" + (Endtime-starttime));        method1:185671841 long s = system.nanotime ();        Method2 ();        Long d = system.nanotime (); System.out.println ("METHOD2:" + (D-s));        method2:90556063 Long s3 = System.nanotime ();        Method3 ();        Long D3 = System.nanotime ();        System.out.println ("method3:" + (D3-S3));//method3:574735 Long S4 = System.nanotime ();        METHOD4 ();        Long D4 = System.nanotime (); System.out.println ("method4: "+ (D4-S4)");//method4:22733469 long S5 = System.nanotime ();        Method5 ();        Long d5 = System.nanotime (); System.out.println ("METHOD5:" + (D5-S5));//method4:19871660} private static void Method5 () {New Vampire Numbers ();        The method has repeated numbers} static class Vampirenumbers {static int a (int i) {return i/1000;        } static int B (int i) {return (i% 1000)/100;        } static int C (int i) {return ((i% 1000)% 100)/10;        } static int d (int i) {return ((i% 1000)% 100)% 10;        } static int com (int i, int j) {return (I *) + j;  } static void Producttest (int i, int m, int n) {if (M * n = = i) System.out.println (i + "        = "+ M +" * "+ N); } public Vampirenumbers () {for (int i = 1001; i < 9999; i++) {producttest (I, COM (a) (I  ), B (i)), COM (c (i), d (i)));              Producttest (i, COM (a (i), B (i)), COM (d (i), C (i)));                Producttest (i, COM (a (i), C (i)), COM (b (i), d (i)));                Producttest (i, COM (a (i), C (i)), COM (d (i), B (i)));                Producttest (i, COM (a (i), d (i)), COM (b (i), C (i)));                Producttest (i, COM (a (i), d (i)), COM (c (i), B (i)));                Producttest (i, COM (b (i), a (i)), COM (c (i), d (i)));                Producttest (i, COM (b (i), a (i)), COM (d (i), C (i)));                Producttest (i, COM (b (i), C (i)), COM (d (i), a (i)));                Producttest (i, COM (b (i), d (i)), COM (c (i), a (i)));                Producttest (i, COM (c (i), a (i)), COM (d (i), B (i)));            Producttest (i, COM (c (i), B (i)), COM (d (i), a (i))); }}} private static void Method4 () {//improved for (int i = one; i <; i++) {for (int J = i; J < 100;                J + +) {int k = i * j;                String kstr = integer.tostring (k); String checkstr = integer.tostring(i) + integer.tostring (j);                if (Kstr.length ()! = 4) Continue;                char[] Kchar = Kstr.tochararray ();                char[] Checkchar = Checkstr.tochararray ();                Arrays.sort (Kchar);                Arrays.sort (Checkchar);                Boolean isvampire = Arrays.equals (Kchar, Checkchar);                if (isvampire) {System.out.println (i + "*" + j + "=" + K);        }}}} private static void Method3 () {//Official Test answer int[] startdigit = new Int[4];        int[] Productdigit = new Int[4]; for (int num1 = ten; num1 <=; num1++) for (int num2 = NUM1; num2 <=; num2++) {//Pet                E 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; + +) 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); }} */* * OUTPUT:15 * 93:1395 21 * 60:1260 21 * 87 : 1827 * Bayi: * 2187 * 51:1530 * 41:1435 * 86:6880 *///:~} private S              tatic void Method2 () {//Poor lift 2//traverse four digits, exclude 00 from 1001 for (int i = 1001; I <= 9999; i++) {//exclude 00                        if (i% = xx) {for (int k = 0; k <; k + =) {if (k! = 0) { 10-99 for (int j2 = 0; J2 <= 9; j2++) {//Generate The first two-digit for (int j = 0; J <; J + =) {for (int j3 = 0; J3 <= 9;                                     j3++) {//Generate a second double digit//infer the product of two numbers if ((k + J2) * (j + J3) = = i) {if (Compare2 (i, K/10, J2, J/10, J3  ) {System.out. println (i + "=" + (k +J2) + "*" + (j + J3));                        }                                    }                                }                            } }}}}} public static void Method1 () {//poor lift 1 int x        = 0, y = 0;            for (int i = 1; I <= 9; i++) {a = i * 1000;                for (int j = 0; J <= 9; j + +) {b = j * 100;                    for (int j2 = 0; J2 < j2++) {c = J2 * 10;                        for (int k = 0; k < k++) {d = k;                        sum = a + B + C + D;                            Take the four number of the two two-digit number, assuming that the two two-digit product equals sum, enter this number for (int k2 = 1; k2 < k2++) {                            A1 = K2 * 10; for (int l = 0; l < l++) {if (A1 +B1 >) {break;                                } B1 = l; Get a two-bit number for (int l2 = 1; L2 < l2++) {C1 = L2                                    * 10;                                            for (int m = l; m < m++) {if (c1 + D1 > 100) {                                        Break                                        } d1 = m;                                        Then get a two digit sum2 = (A1 + b1) * (c1 + d1);                                            Calculates the product of two two-bit numbers, assuming equal to sum if (sum2 = = sum) {                                                And the mantissa cannot be an if (c + d! = 0) {                       Is it the same as a few numbers?                         if (compare (a, B, C, D, A1, B1, C1, D1)) {                                                            SYSTEM.OUT.PRINTLN (sum                                                + "=" + (A1 + B1) + "*" + (c1 + d1));                                     }                                            }                                        }                        }                                }                            } }}}}} private static Boolean compare2 (int i, int j, Int J        2, int k, int j3) {int a[] = {i%, i/10%, i/100%, i/1000};        int b[] = {J, J2, K, J3};        Arrays.sort (a);        Arrays.sort (b);        if (Arrays.equals (A, B)) return true;    else return false; } pRivate static Boolean compare (int a2, int b2, int c2, int d2, int a12, int b12, int C12,        int D12) {int[] a = new int[4];        Int[] B = new Int[4];        A[0] = a2/1000;        A[1] = b2/100;        A[2] = C2/10;        A[3] = D2;        B[0] = A12/10;        B[1] = B12;        B[2] = C12/10;        B[3] = D12;        Arrays.sort (a);        Arrays.sort (b);        if (Arrays.equals (A, B)) return true;    else return false; }}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.