High-efficiency version of the vampire digital algorithm implemented by Java (some netizens have provided an algorithm description)

Source: Internet
Author: User

 

Today, I sorted out the vampire digital algorithm and adjusted some of it. I saw another one on the Internet, but I didn't understand it. He improved the performance by 10 times.

 

First look at the code I have compiled

Import Java. util. arrays; <br/>/** <br/> * vampire number, high-efficiency version. <br> <br/> * a four-digit number can be split into the product of two-digit numbers. The order is not limited. <Br> <br/> * e.g. 1395 = 15*93 <br/> * @ author laozizhu.com) <br/> */<br/> public class vampire {<br/> Public static void main (string [] Arg) {<br/> string [] ar_str1, ar_str2; <br/> int sum = 0; <br/> int from; <br/> int to; <br/> int I _val; <br/> int COUNT = 0; <br/> // double loop example <br/> for (INT I = 10; I <100; I ++) {<br/> // J = I + 1 to avoid duplication <br/> from = math. max (1000/I, I + 1); <br/> to = m Ath. min (10000/I, 100); <br/> for (Int J = from; j <to; j ++) {<br/> I _val = I * J; <br/> // the following code, I personally do not know why, shame <br/> If (I _val % 100 = 0 | (I _val-I-j) % 9! = 0) {<br/> continue; <br/>}< br/> count ++; <br/> ar_str1 = string. valueof (I _val ). split (""); <br/> ar_str2 = (string. valueof (I) + String. valueof (j )). split (""); <br/> arrays. sort (ar_str1); <br/> arrays. sort (ar_str2); <br/> If (arrays. equals (ar_str1, ar_str2) {// after sorting and comparison, if it is true, find a group <br/> sum ++; <br/> system. out. println ("Number" + sum + "group:" + I + "*" + J + "=" + I _val ); <br/>}< br/> system. out. println ("found" + sum + "Number of vampire groups"); <br/> system. out. println (count); <br/>}< br/>}

 

 

Running result

 

1st groups: 15*93 = 1395
2nd groups: 21*60 = 1260
Group 3rd: 21*87 = 1827
4th groups: 27*81 = 2187
5th groups: 30*51 = 1530
6th groups: 35*41 = 1435
7th groups: 80*86 = 6880
7 groups of vampires found
232

 

 

We can see that the comparison is only 232 times. If there are roughly 4000 times, the key part of the comparison is

// The following code, I personally do not know why, shame <br/> If (I _val % 100 = 0 | (I _val-I-j) % 9! = 0) {<br/> continue; <br/>}

 

Hope that the algorithm experts will give you advice.

Because you cannot reply, you can reply to the Forum, or send me emails or messages on the website.

 

The forum post address is as follows:"

Http://topic.csdn.net/u/20090123/10/8dc0c939-20ae-41f1-a7c1-d05b897b27c7.html

 

 

 

The algorithm is explained by mt502.
Assume val = 1000a + 100b + 10C + D, because val = x * Y is satisfied, x = 10a + B, y = 10C + d
Then Val-x-y = 990a + 99b + 9C = 9 * (110a + 11B + C), So val-x-y can be divisible by 9.
Therefore, the number that meets the condition must be divisible by 9, so other numbers can be filtered directly.

I can do it.
X * Y = val = 1000a + 100b + 10C + D;
Let's assume that
X = 10a + B, y = 10C + d
Then
X * Y-x-y
= Val-x-y
= (1000a + 100b + 10C + D)-(10a + B)-(10C + d) = 990a + 99b + 9C
= 9 * (110a + 11B + C );

The results are the same for other combinations, such
X = 10C + A; y = 10D + B;
X * Y-x-y
= Val-x-y
= (1000a + 100b + 10C + D)-(10C + a)-(10d + B) = 999a + 99b-9d
= 9 * (110a + 11B-D );

Of course, it can also be divisible by 9

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.