|
13 | 13 | * @see <a href='https://en.wikipedia.org/wiki/Vampire_number'>Vampire number on Wikipedia</a>
|
14 | 14 | */
|
15 | 15 | public final class VampireNumber {
|
| 16 | + // Forbid instantiation. |
16 | 17 | private VampireNumber() {
|
17 | 18 | }
|
18 | 19 |
|
19 |
| - public static void main(String[] args) { |
20 |
| - printVampireNumbers(10, 1000, true); |
21 |
| - } |
22 |
| - |
23 |
| - static void printVampireNumbers(int startValue, int stopValue, boolean ignorePseudoVampireNumbers) { |
24 |
| - int resultCounter = 0; |
25 |
| - |
26 |
| - for (int i = startValue; i <= stopValue; i++) { |
27 |
| - for (int j = i; j <= stopValue; j++) { |
28 |
| - if (isVampireNumber(i, j, ignorePseudoVampireNumbers)) { |
29 |
| - resultCounter++; |
30 |
| - System.out.printf("%d: %d = %d * %d%n", resultCounter, i * j, i, j); |
31 |
| - } |
32 |
| - } |
33 |
| - } |
34 |
| - } |
35 |
| - |
36 | 20 | static boolean isVampireNumber(int a, int b, boolean ignorePseudoVampireNumbers) {
|
37 | 21 | // Pseudo vampire numbers don't have to be of n/2 digits. E.g., 126 = 6 x 21 is such a number.
|
38 | 22 | if (ignorePseudoVampireNumbers && String.valueOf(a).length() != String.valueOf(b).length()) {
|
@@ -61,4 +45,21 @@ static String splitIntoSortedDigits(int... nums) {
|
61 | 45 | digits.stream().sorted().forEach(res::append);
|
62 | 46 | return res.toString();
|
63 | 47 | }
|
| 48 | + |
| 49 | + static void printVampireNumbers(int startValue, int stopValue, boolean ignorePseudoVampireNumbers) { |
| 50 | + int resultCounter = 0; |
| 51 | + |
| 52 | + for (int i = startValue; i <= stopValue; i++) { |
| 53 | + for (int j = i; j <= stopValue; j++) { |
| 54 | + if (isVampireNumber(i, j, ignorePseudoVampireNumbers)) { |
| 55 | + resultCounter++; |
| 56 | + System.out.printf("%d: %d = %d * %d%n", resultCounter, i * j, i, j); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public static void main(String[] args) { |
| 63 | + printVampireNumbers(10, 1000, true); |
| 64 | + } |
64 | 65 | }
|
0 commit comments