1+ package Maths ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .Collections ;
5+ import java .util .List ;
6+
7+ /**
8+ n number theory, a vampire number (or true vampire number) is a composite natural number with an even number of digits,
9+ that can be factored into two natural numbers each with half as many digits as the original number
10+ and not both with trailing zeroes, where the two factors contain precisely
11+ all the digits of the original number, in any order, counting multiplicity.
12+ The first vampire number is 1260 = 21 × 60.
13+ * *
14+ * <p>
15+ * * link: https://en.wikipedia.org/wiki/Vampire_number
16+ * * </p>
17+ * <p>
18+ *
19+ */
20+
21+
22+
23+
24+
25+
26+
27+ public class VampireNumber {
28+
29+ public static void main (String [] args ) {
30+
31+ test (10 ,1000 );
32+ }
33+
34+ static void test (int startValue , int stopValue ) {
35+ int countofRes = 1 ;
36+ StringBuilder res = new StringBuilder ();
37+
38+
39+ for (int i = startValue ; i <= stopValue ; i ++) {
40+ for (int j = i ; j <= stopValue ; j ++) {
41+ // System.out.println(i+ " "+ j);
42+ if (isVampireNumber (i , j ,true )) {
43+ countofRes ++;
44+ res .append ("" + countofRes + ": = ( " + i + "," + j + " = " + i *j + ")" + "\n " );
45+ }
46+ }
47+ }
48+ System .out .println (res );
49+ }
50+
51+
52+
53+
54+ static boolean isVampireNumber (int a , int b , boolean noPseudoVamireNumbers ) {
55+
56+ // this is for pseudoVampireNumbers pseudovampire number need not be of length n/2 digits for example
57+ // 126 = 6 x 21
58+ if (noPseudoVamireNumbers ) {
59+ if (a * 10 <= b || b * 10 <= a ) {
60+ return false ;
61+ }
62+ }
63+
64+ String mulDigits = splitIntoDigits (a *b ,0 );
65+ String faktorDigits = splitIntoDigits (a ,b );
66+
67+ return mulDigits .equals (faktorDigits );
68+ }
69+
70+
71+
72+ // methode to Split the numbers to Digits
73+ static String splitIntoDigits (int num , int num2 ) {
74+
75+ StringBuilder res = new StringBuilder ();
76+
77+ ArrayList <Integer > digits = new ArrayList <>();
78+ while (num > 0 ) {
79+ digits .add (num %10 );
80+ num /= 10 ;
81+ }
82+ while (num2 > 0 ) {
83+ digits .add (num2 %10 );
84+ num2 /= 10 ;
85+ }
86+ Collections .sort (digits );
87+ for (int i : digits ) {
88+ res .append (i );
89+ }
90+
91+
92+ return res .toString ();
93+ }
94+ }
0 commit comments