|
1 | 1 | package com.thealgorithms.maths;
|
2 | 2 |
|
3 | 3 | import java.util.ArrayList;
|
4 |
| -import java.util.Collections; |
5 | 4 |
|
6 | 5 | /**
|
7 |
| - * n number theory, a vampire number (or true vampire number) is a composite |
| 6 | + * In number theory, a vampire number (or true vampire number) is a composite |
8 | 7 | * natural number with an even number of digits, that can be factored into two
|
9 | 8 | * natural numbers each with half as many digits as the original number and not
|
10 | 9 | * both with trailing zeroes, where the two factors contain precisely all the
|
11 | 10 | * digits of the original number, in any order, counting multiplicity. The first
|
12 |
| - * vampire number is 1260 = 21 × 60. * |
| 11 | + * vampire number is 1260 = 21 × 60. |
13 | 12 | *
|
14 |
| - * <p> |
15 |
| - * link: https://en.wikipedia.org/wiki/Vampire_number * |
16 |
| - * |
17 |
| - * <p> |
| 13 | + * @see <a href='https://en.wikipedia.org/wiki/Vampire_number'>Vampire number on Wikipedia</a> |
18 | 14 | */
|
19 | 15 | public final class VampireNumber {
|
| 16 | + // Forbid instantiation. |
20 | 17 | private VampireNumber() {
|
21 | 18 | }
|
22 | 19 |
|
23 |
| - public static void main(String[] args) { |
24 |
| - test(10, 1000); |
25 |
| - } |
26 |
| - |
27 |
| - static void test(int startValue, int stopValue) { |
28 |
| - int countofRes = 1; |
29 |
| - StringBuilder res = new StringBuilder(); |
30 |
| - |
31 |
| - for (int i = startValue; i <= stopValue; i++) { |
32 |
| - for (int j = i; j <= stopValue; j++) { |
33 |
| - // System.out.println(i+ " "+ j); |
34 |
| - if (isVampireNumber(i, j, true)) { |
35 |
| - countofRes++; |
36 |
| - res.append("").append(countofRes).append(": = ( ").append(i).append(",").append(j).append(" = ").append(i * j).append(")").append("\n"); |
37 |
| - } |
38 |
| - } |
39 |
| - } |
40 |
| - System.out.println(res); |
41 |
| - } |
42 |
| - |
43 |
| - static boolean isVampireNumber(int a, int b, boolean noPseudoVamireNumbers) { |
44 |
| - // this is for pseudoVampireNumbers pseudovampire number need not be of length n/2 digits |
45 |
| - // for example 126 = 6 x 21 |
46 |
| - if (noPseudoVamireNumbers) { |
47 |
| - if (a * 10 <= b || b * 10 <= a) { |
48 |
| - return false; |
49 |
| - } |
| 20 | + static boolean isVampireNumber(int a, int b, boolean ignorePseudoVampireNumbers) { |
| 21 | + // Pseudo vampire numbers don't have to be of n/2 digits. E.g., 126 = 6 x 21 is such a number. |
| 22 | + if (ignorePseudoVampireNumbers && String.valueOf(a).length() != String.valueOf(b).length()) { |
| 23 | + return false; |
50 | 24 | }
|
51 | 25 |
|
52 |
| - String mulDigits = splitIntoDigits(a * b, 0); |
53 |
| - String faktorDigits = splitIntoDigits(a, b); |
| 26 | + String mulDigits = splitIntoSortedDigits(a * b); |
| 27 | + String factorDigits = splitIntoSortedDigits(a, b); |
54 | 28 |
|
55 |
| - return mulDigits.equals(faktorDigits); |
| 29 | + return mulDigits.equals(factorDigits); |
56 | 30 | }
|
57 | 31 |
|
58 |
| - // methode to Split the numbers to Digits |
59 |
| - static String splitIntoDigits(int num, int num2) { |
60 |
| - StringBuilder res = new StringBuilder(); |
61 |
| - |
| 32 | + // Method to split a pair of numbers to digits and sort them in the ascending order. |
| 33 | + static String splitIntoSortedDigits(int... nums) { |
| 34 | + // Collect all digits in a list. |
62 | 35 | ArrayList<Integer> digits = new ArrayList<>();
|
63 |
| - while (num > 0) { |
64 |
| - digits.add(num % 10); |
65 |
| - num /= 10; |
66 |
| - } |
67 |
| - while (num2 > 0) { |
68 |
| - digits.add(num2 % 10); |
69 |
| - num2 /= 10; |
70 |
| - } |
71 |
| - Collections.sort(digits); |
72 |
| - for (int i : digits) { |
73 |
| - res.append(i); |
| 36 | + for (int num : nums) { |
| 37 | + while (num > 0) { |
| 38 | + digits.add(num % 10); |
| 39 | + num /= 10; |
| 40 | + } |
74 | 41 | }
|
75 | 42 |
|
| 43 | + // Sort all digits and convert to String. |
| 44 | + StringBuilder res = new StringBuilder(); |
| 45 | + digits.stream().sorted().forEach(res::append); |
76 | 46 | return res.toString();
|
77 | 47 | }
|
78 | 48 | }
|
0 commit comments