Java implementation of 4-digit vampire digital algorithm

Source: Internet
Author: User

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

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.