Skip to content

Commit 14db275

Browse files
authored
Improve Vampire Number (#6110)
1 parent 2da56d6 commit 14db275

File tree

2 files changed

+54
-52
lines changed

2 files changed

+54
-52
lines changed
+22-52
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,48 @@
11
package com.thealgorithms.maths;
22

33
import java.util.ArrayList;
4-
import java.util.Collections;
54

65
/**
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
87
* natural number with an even number of digits, that can be factored into two
98
* natural numbers each with half as many digits as the original number and not
109
* both with trailing zeroes, where the two factors contain precisely all the
1110
* 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.
1312
*
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>
1814
*/
1915
public final class VampireNumber {
16+
// Forbid instantiation.
2017
private VampireNumber() {
2118
}
2219

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;
5024
}
5125

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);
5428

55-
return mulDigits.equals(faktorDigits);
29+
return mulDigits.equals(factorDigits);
5630
}
5731

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.
6235
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+
}
7441
}
7542

43+
// Sort all digits and convert to String.
44+
StringBuilder res = new StringBuilder();
45+
digits.stream().sorted().forEach(res::append);
7646
return res.toString();
7747
}
7848
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.thealgorithms.maths;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
class VampireNumberTest {
7+
@Test
8+
void areVampireNumbers() {
9+
Assertions.assertTrue(VampireNumber.isVampireNumber(15, 93, true));
10+
Assertions.assertTrue(VampireNumber.isVampireNumber(135, 801, true));
11+
Assertions.assertTrue(VampireNumber.isVampireNumber(201, 600, true));
12+
}
13+
14+
@Test
15+
void arePseudoVampireNumbers() {
16+
Assertions.assertTrue(VampireNumber.isVampireNumber(150, 93, false));
17+
Assertions.assertTrue(VampireNumber.isVampireNumber(546, 84, false));
18+
Assertions.assertTrue(VampireNumber.isVampireNumber(641, 65, false));
19+
}
20+
21+
@Test
22+
void areNotVampireNumbers() {
23+
Assertions.assertFalse(VampireNumber.isVampireNumber(51, 39, false));
24+
Assertions.assertFalse(VampireNumber.isVampireNumber(51, 39, true));
25+
}
26+
27+
@Test
28+
void testSplitIntoSortedDigits() {
29+
Assertions.assertEquals("123", VampireNumber.splitIntoSortedDigits(321));
30+
Assertions.assertEquals("02234", VampireNumber.splitIntoSortedDigits(20, 324));
31+
}
32+
}

0 commit comments

Comments
 (0)