Thinking in Java vampire number problem

Source: Internet
Author: User

First explain the vampire numbers: vampire numbers are numbers that are even digits, which can be multiplied by a pair of numbers, which contain the number of half-digits of the product, and the number ending with two 0 is not allowed.

Four-digit vampire number example: 1260=21*60,1827=21*87,2187=27*81 ...

List the results first: altogether 7:1260=21*60,1395=15*93,1435=41*35,1530=51*30,1827=87*21,2187=27*81,6880=86*80

Method One:

This method is "thinking in Java" the official answer, because the chapter is very forward, so the use of the traversal four-digit method, positive thinking, that is, the first four digits, then split, four combinations of numbers multiplied, if the product is equal to the original number, then output, and calculated as a vampire number.

  1. Public class Searchforvampirethinkinginjava {
  2. //Control/vampirenumbers.java
  3. //TIJ4 Chapter Control, Exercise, page 154
  4. / * A vampire number have an even number of digits and are formed by multiplying a
  5. * Pair of numbers containing half the number of digits of the result. The
  6. * Digits is taken from the original number in any order. Pairs of trailing
  7. * Zeroes is not allowed. Examples include:1260 = 21 * 60, 1827 = 21 * 87,
  8. * 2187 = 27 * 81. Write A program This finds all the 4-digit vampire numbers.
  9. * (Suggested by Dan Forhan.)
  10. */
  11. This method is the forward thinking, that is, there are four digits, then split, four number combinations multiplied, if the product is equal to the original number, then output, and calculated as a vampire number. TMJG Add this line and comment
  12. In fact, the result of Sum is 107,976 times, very large, the algorithm is very inefficient, and there is a repetition (6880 = 86 * 80,6880 = 80 * 86). TMJG Add this line and comment
  13. static int sum=0; Record the number of times the call was judged, TMJG add this line and comment
  14. static int a (int i) {
  15. return i/1000; //For thousands of numbers, below, TMJG add this line and comment
  16. }
  17. static int B (int i) {
  18. return (i%)/100;
  19. }
  20. static int C (int i) {
  21. return ((i%)%)/10;
  22. }
  23. static int d (int i) {
  24. return ((i%)%)%10;
  25. }
  26. static int COM (int i, int j) { //to form two digits of 10~99, TMJG adds this line and notes
  27. return (i * ) + j;
  28. }
  29. static void producttest (int i, int m, int n) {
  30. sum++;
  31. if (M * n = = i) System.out.println (i + "=" + M + "*" + N);
  32. }
  33. public static void Main (string[] args) {
  34. For (int i = 1001; i < 9999; i++) {
  35. Producttest (i, COM (a (i), B (i)), COM (c (i), d (i)));
  36. Producttest (i, COM (a (i), B (i)), COM (d (i), C (i)));
  37. Producttest (i, COM (a (i), C (i)), COM (b (i), d (i)));
  38. Producttest (i, COM (a (i), C (i)), COM (d (i), B (i)));
  39. Producttest (i, COM (a (i), d (i)), COM (b (i), C (i)));
  40. Producttest (i, COM (a (i), d (i)), COM (c (i), B (i)));
  41. Producttest (i, COM (b (i), a (i)), COM (c (i), d (i)));
  42. Producttest (i, COM (b (i), a (i)), COM (d (i), C (i)));
  43. Producttest (i, COM (b (i), C (i)), COM (d (i), a (i)));
  44. Producttest (i, COM (b (i), d (i)), COM (c (i), a (i)));
  45. Producttest (i, COM (c (i), a (i)), COM (d (i), B (i)));
  46. Producttest (i, COM (c (i), B (i)), COM (d (i), a (i)));
  47. }
  48. System.out.println ("Total number of calls judged:" +sum); TMJG Add this line and comment
  49. }
  50. }

Method Two:

This method is an improvement of method one, skipping some numbers (such as 1100 at the end of two 0, such as 1010, 1001 is obviously not the number of vampire numbers), and avoid the possibility of duplication, but the efficiency is still very low, need to determine 104,942 times.

public class Searchforvampirenumberslj {
public static int count = 0;//record How many vampire numbers are there?
public static int k=0;//How many times the record call is judged
public static void Main (string[] args) {
for (int i = 1001; i < 10000; i++) {
If the number is like 1100 at least 2 at the end of the 0, then skip
if (i% 100 = = 0) {
Continue
}
Get the number on the number four numeric digits, here we assume that the four digits are represented as ABCD
int a = i/1000;
int B = (I-A * 1000)/100;
int c = (I-A * 1000-b * 100)/10;
int d = i-a * 1000-b * 100-c * 10;
Determine whether there are two 0 cases in four positions, such as 1010, and skip these numbers, since thousands cannot be 0, so it is only necessary to determine if the other three bits have 2 0.
When there are 2 0 o'clock in the 3 number, there is bound to be "3 number of the sum equal to one of the number", as a basis for judgement, and then two 0 is also excluded, in fact, only need to judge such as 1001, and 1010 This situation can be
if (b + c + d = = C | | b + c + d = = d) {
Continue
}
After you've ruled out a variety of situations, you can start a real vampire number screening.
Then there are 12 kinds of situations: ABCD,ABDC,ACBD,ACDB,ADBC,ADCB,BACD,BADC,BCDA,BDCA,CADB,CBDA
if (Search (I, a, B, C, D)) {
} else if (search (I, a, B, D, c)) {
} else if (search (I, A, C, B, D)) {
} else if (search (I, A, C, D, b)) {
} else if (search (I, A, d, B, c)) {
} else if (search (I, A, D, C, b)) {
} else if (search (I, B, A, C, D)) {
} else if (search (I, B, a, d, c)) {
} else if (search (I, B, C, D, a)) {
} else if (search (i, B, D, C, a)) {
} else if (search (I, C, a, d, b)) {
} else if (search (I, C, B, D, a)) {
}
}
System.out.println ("Four-digit vampire numbers have a total of" + Count + "each.) ");
SYSTEM.OUT.PRINTLN ("Total number of calls to judgment" + K);
}

Determine if the condition is met
static Boolean search (int i, int a, int b, int c, int d) {
k++;
if ((A * ten + b) * (c * + D) = = i) {
Searchfor (i,a,b,c,d);//If the condition is met, the result is printed
return true;
}else{
return false;
}
}

Print and count the number of items that meet the criteria
static void searchfor (int i, int a, int b, int c, int d) {
count++;
SYSTEM.OUT.PRINTLN (i + "=" + (A * + b) + "*" + (c * + D));
}
}

Method Three:

The following is the online search for code, the algorithm using reverse thinking, the 4-digit vampire number can only be split into two 2-digit number, so traverse all two two-digit multiplication of the case, remove the non-conformance of the situation without judgment, the other judgment can be.

public class Searchforvampirefrominternet {
/**
* Code from the network, slightly modified and added comments
* This algorithm only needs to execute 3,721 times
*/
public static void Main (string[] args) {
string[] Targetnum = null;
String[] Gunnum=null; Target numbers and gun numbers (i.e. contrasting numbers)
int sum = 0; Total number of vampire numbers
int count=0; The number of valid judgments, the product is not 4 digits of the platoon
for (int i = ten; i <; i++) {
for (int j = i+1; J < + j + +) {//No two-digit number satisfies ab*ab=abab (not programmable), so here J starts with I+1.
int i_target = i * j;
if (I_target < | | i_target > 9999)
Continue The product is not a 4-digit skip
count++;
Targetnum = string.valueof (I_target). Split (""); Convert it to a string array
Gunnum = (string.valueof (i) + string.valueof (j)). Split ("");
Java.util.Arrays.sort (Targetnum); In ascending order, because only the arrangement of the comparison can guarantee not miss ABCD=BA*DC such a situation
Java.util.Arrays.sort (Gunnum);
if (Java.util.Arrays.equals (Targetnum, gunnum)) {
Compare after sort, find a group for true
sum++;
System.out.println ("first" + Sum + "one:" + i_target+ "=" +i + "*" + j);
}
}
}
System.out.println ("A total of" +count+ "judgment, found" + Sum + "a vampire number. ");
}
}

Thinking in Java vampire number problem

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.