Java_ Vampire Digital Multi-method implementation

Source: Internet
Author: User

 Packagetest4;Importjava.util.Arrays;/*** from the Tij in the 4th chapter of exercise 10 See "Vampire numbers", the following methods of implementation and execution time comparison * Find out four digits of all vampire numbers * Vampire numbers are digits with even numbers, which can be multiplied by a pair of numbers, which each contain a number of half-digits of the product, * The numbers selected from the initial numbers can be sorted arbitrarily.  * Numbers ending with two 0 are not allowed. * For example the following numbers are vampire numbers * 1260=21*60 * 1827=21*87 * 2187=27*81*/ Public classTest1 { Public Static voidMain (string[] args) {LongStart =System.nanotime ();        Fun1 (); LongEnd =System.nanotime (); System.out.println ("Method 1 Time:" + (End-start) + "\ n"); Start=System.nanotime ();        Fun2 (); End=System.nanotime (); System.out.println ("Method 2 Time:" + (End-start) + "\ n"); Start=System.nanotime ();        Fun3 (); End=System.nanotime (); System.out.println ("Method 3 Time:" + (End-start) + "\ n"); Start=System.nanotime ();        Fun4 (); End=System.nanotime (); System.out.println ("Method 4 Time:" + (End-start) + "\ n"); }    Private Static voidfun1 () {//Reference Answer        intsum = 0; int[] Startdigit =New int[4]; int[] Productdigit =New int[4];  for(intNUM1 = 10; NUM1 <= 99; num1++)             for(intnum2 = 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; intProduct = 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; intCount = 0;  for(intx = 0; x < 4; X + +)                     for(inty = 0; Y < 4; y++) {                        if(Productdigit[x] = =Startdigit[y]) {Count++; PRODUCTDIGIT[X]=-1; Startdigit[y]=-2; if(Count = = 4) {System.out.println ("First" + Sum + "group:" + NUM1 + "*" + num2 + ":" +product); Sum++; } }}} System.out.println ("Method 1 found" + sum + "Group of Vampires"); }    Private Static voidfun2 () {string[] ar_str1, AR_STR2; intsum = 0; intFrom ; intto ; intI_val;  for(inti = 10; I < 100; i++) { from= Math.max (1000/i, i + 1); to= Math.min (10000/i, 100); //the product of 2 numbers is a 4-digit number (greater than or equal to 1000, less than 10000), and when I determines, the other number range is determined             for(intj = from; J < to; J + +) {I_val= i *J; if(i_val% 100 = = 0 | | (I_val-i-J)% 9! = 0) {                    //(I_val-i-j)% 9! = 0 Understanding://Suppose val = 1000a + 100b + 10c + D, because the val = x * y is satisfied, there is x =//10a + b, y = 10c + D//available Val-x-y = 990a + 99b + 9c = 9 * (110a + 11b + c),//so val-x-y can be divisible by 9. //The number that satisfies the condition must be divisible by 9, and other numbers can be filtered directly.                     Continue; } ar_str1= String.valueof (I_val). Split (""); AR_STR2= (String.valueof (i) + string.valueof (j)). Split ("");                Arrays.sort (AR_STR1);                Arrays.sort (AR_STR2); if(Arrays.equals (AR_STR1, ar_str2)) {sum++; System.out.println ("First" + Sum + "group:" + i + "*" + j + "=" +i_val); }}} System.out.println ("Method 2 Found" + Sum + "Group of Vampires"); }    Private Static voidFun3 () {intsum = 0;  for(inti = 11; I < 100; i++) {             for(intj = i; J < 100; J + +) {                intK = i *J; //there is another way to manipulate the string, and the comparison finds that the following method takes less time                int[] A = {k/1000, k/100%, k/10%, K% 1000 100 10 }; int[] b = {i%, I/10, J, J/10 };                Arrays.sort (a);                Arrays.sort (b); if(Arrays.equals (A, b)) {Sum++; System.out.println ("First" + Sum + "group:" + i + "*" + j + "=" +k); }}} System.out.println ("Method 3 found" + Sum + "Group of Vampires"); }    Private Static voidFun4 () {//Reverse Thinkingstring[] Targetnum =NULL; String[] Gunnum=NULL; intsum = 0;  for(inti = 10; I < 100; i++) {             for(intj = i + 1; J < 100; J + +) {                //no two-digit number satisfies ab*ab=abab, so here J starts with I+1.                intI_target = i *J; if(I_target < | | i_target > 9999)                    Continue;//the product is not a 4-digit skipTargetnum = string.valueof (I_target). Split (""); Gunnum= (String.valueof (i) + string.valueof (j)). Split ("");                Arrays.sort (Targetnum);                Arrays.sort (Gunnum); if(Arrays.equals (Targetnum, gunnum)) {sum++; System.out.println ("First" + Sum + "group:" + i_target + "=" + i + "*" +j); }}} System.out.println ("Method 4 finds" + sum + "vampire numbers. "); }}

Execution Result:

Group No. 0:15 * 93:1395Group 1th:21 * 60:1260Group 2nd:21 * 87:1827Group 3rd:27 * 81:2,187Group 4th:30 * 51:1530Group 5th:35 * 41:1435Group 6th:80 * 86:6,880Method 1 found 7 groups of vampire number method 1 used time:4880538Group 1th:15*93=1395Group 2nd:21*60=1260Group 3rd:21*87=1827Group 4th:27*81=2187Group 5th:30*51=1530Group 6th:35*41=1435Group 7th:80*86=6880Method 2 found 7 groups of vampire number Method 2 used time:43971275Group 1th:15 * 93 = 1395Group 2nd:21 * 60 = 1260Group 3rd:21 * 87 = 1827Group 4th:27 * 81 = 2187Group 5th:30 * 51 = 1530Group 6th:35 * 41 = 1435Group 7th:80 * 86 = 6880Method 3 found 7 groups of Vampire number method 3 used time:19352070Group 1th:1395=15*93Group 2nd:1260=21*60Group 3rd:1827=21*87Group 4th:2187=27*81Group 5th:1530=30*51Group 6th:1435=35*41Group 7th:6880=80*86Method 4 found 7 vampire numbers. Method 4 Time:125098134

Java_ Vampire Digital Multi-method implementation

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.