-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Improvements to Vampire Number #6110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3a1749f
Add a unit test for VampireNumber
Shisoik 9bf7661
Fix comments in VampireNumber
Shisoik 65cad79
Refactor test method in VampireNumber
Shisoik d814c6f
Refactor other methods in VampireNumber
Shisoik bdaf869
Add a test for VampireNumb.splitIntoSortedDigits
Shisoik 82828cd
Slightly rearrange methods n VampireNumber moving important bits clos…
Shisoik 41a7067
Reformat the test with static imports first
Shisoik c8f0a66
Reformat VampireNumberTest without * imports for asserts
Shisoik 357da09
Reformat VampireNumberTest not to use static imports at all to make M…
Shisoik 5c36007
Removed main and printVampireNumbers methods per siriak request.
Shisoik 77c5c91
Merge branch 'master' into vampire
siriak e25809d
Merge branch 'master' into vampire
siriak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,65 @@ | ||
package com.thealgorithms.maths; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
|
||
/** | ||
* n number theory, a vampire number (or true vampire number) is a composite | ||
* In number theory, a vampire number (or true vampire number) is a composite | ||
* natural number with an even number of digits, that can be factored into two | ||
* natural numbers each with half as many digits as the original number and not | ||
* both with trailing zeroes, where the two factors contain precisely all the | ||
* digits of the original number, in any order, counting multiplicity. The first | ||
* vampire number is 1260 = 21 × 60. * | ||
* vampire number is 1260 = 21 × 60. | ||
* | ||
* <p> | ||
* link: https://en.wikipedia.org/wiki/Vampire_number * | ||
* | ||
* <p> | ||
* @see <a href='https://en.wikipedia.org/wiki/Vampire_number'>Vampire number on Wikipedia</a> | ||
*/ | ||
public final class VampireNumber { | ||
// Forbid instantiation. | ||
private VampireNumber() { | ||
} | ||
|
||
public static void main(String[] args) { | ||
test(10, 1000); | ||
} | ||
static boolean isVampireNumber(int a, int b, boolean ignorePseudoVampireNumbers) { | ||
// Pseudo vampire numbers don't have to be of n/2 digits. E.g., 126 = 6 x 21 is such a number. | ||
if (ignorePseudoVampireNumbers && String.valueOf(a).length() != String.valueOf(b).length()) { | ||
return false; | ||
} | ||
|
||
static void test(int startValue, int stopValue) { | ||
int countofRes = 1; | ||
StringBuilder res = new StringBuilder(); | ||
String mulDigits = splitIntoSortedDigits(a * b); | ||
String factorDigits = splitIntoSortedDigits(a, b); | ||
|
||
for (int i = startValue; i <= stopValue; i++) { | ||
for (int j = i; j <= stopValue; j++) { | ||
// System.out.println(i+ " "+ j); | ||
if (isVampireNumber(i, j, true)) { | ||
countofRes++; | ||
res.append("").append(countofRes).append(": = ( ").append(i).append(",").append(j).append(" = ").append(i * j).append(")").append("\n"); | ||
} | ||
} | ||
} | ||
System.out.println(res); | ||
return mulDigits.equals(factorDigits); | ||
} | ||
|
||
static boolean isVampireNumber(int a, int b, boolean noPseudoVamireNumbers) { | ||
// this is for pseudoVampireNumbers pseudovampire number need not be of length n/2 digits | ||
// for example 126 = 6 x 21 | ||
if (noPseudoVamireNumbers) { | ||
if (a * 10 <= b || b * 10 <= a) { | ||
return false; | ||
// Method to split a pair of numbers to digits and sort them in the ascending order. | ||
static String splitIntoSortedDigits(int... nums) { | ||
// Collect all digits in a list. | ||
ArrayList<Integer> digits = new ArrayList<>(); | ||
for (int num : nums) { | ||
while (num > 0) { | ||
digits.add(num % 10); | ||
num /= 10; | ||
} | ||
} | ||
|
||
String mulDigits = splitIntoDigits(a * b, 0); | ||
String faktorDigits = splitIntoDigits(a, b); | ||
|
||
return mulDigits.equals(faktorDigits); | ||
// Sort all digits and convert to String. | ||
StringBuilder res = new StringBuilder(); | ||
digits.stream().sorted().forEach(res::append); | ||
return res.toString(); | ||
} | ||
|
||
// methode to Split the numbers to Digits | ||
static String splitIntoDigits(int num, int num2) { | ||
StringBuilder res = new StringBuilder(); | ||
static void printVampireNumbers(int startValue, int stopValue, boolean ignorePseudoVampireNumbers) { | ||
int resultCounter = 0; | ||
|
||
ArrayList<Integer> digits = new ArrayList<>(); | ||
while (num > 0) { | ||
digits.add(num % 10); | ||
num /= 10; | ||
} | ||
while (num2 > 0) { | ||
digits.add(num2 % 10); | ||
num2 /= 10; | ||
} | ||
Collections.sort(digits); | ||
for (int i : digits) { | ||
res.append(i); | ||
for (int i = startValue; i <= stopValue; i++) { | ||
for (int j = i; j <= stopValue; j++) { | ||
if (isVampireNumber(i, j, ignorePseudoVampireNumbers)) { | ||
resultCounter++; | ||
System.out.printf("%d: %d = %d * %d%n", resultCounter, i * j, i, j); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return res.toString(); | ||
public static void main(String[] args) { | ||
printVampireNumbers(10, 1000, true); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/test/java/com/thealgorithms/maths/VampireNumberTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.thealgorithms.maths; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class VampireNumberTest { | ||
@Test | ||
void areVampireNumbers() { | ||
Assertions.assertTrue(VampireNumber.isVampireNumber(15, 93, true)); | ||
Assertions.assertTrue(VampireNumber.isVampireNumber(135, 801, true)); | ||
Assertions.assertTrue(VampireNumber.isVampireNumber(201, 600, true)); | ||
} | ||
|
||
@Test | ||
void arePseudoVampireNumbers() { | ||
Assertions.assertTrue(VampireNumber.isVampireNumber(150, 93, false)); | ||
Assertions.assertTrue(VampireNumber.isVampireNumber(546, 84, false)); | ||
Assertions.assertTrue(VampireNumber.isVampireNumber(641, 65, false)); | ||
} | ||
|
||
@Test | ||
void areNotVampireNumbers() { | ||
Assertions.assertFalse(VampireNumber.isVampireNumber(51, 39, false)); | ||
Assertions.assertFalse(VampireNumber.isVampireNumber(51, 39, true)); | ||
} | ||
|
||
@Test | ||
void testSplitIntoSortedDigits() { | ||
Assertions.assertEquals("123", VampireNumber.splitIntoSortedDigits(321)); | ||
Assertions.assertEquals("02234", VampireNumber.splitIntoSortedDigits(20, 324)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.