Last active
October 19, 2020 16:42
-
-
Save yogonza524/5aefab4cefec15000a794516301775d1 to your computer and use it in GitHub Desktop.
There's a 10-digit number, ABCDEFGHIJ, whose digits are all different, and that has an interesting property: if you take the first N digits from left to right, the resulting number is exactly divisible by N. What's the number? To clarify: A is divisible by 1, AB is divisible by 2, ABC is divisible by 3, etc., up to ABCDEFGHIJ that is divisible b…
This file contains 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
@Test | |
public void findNumber() { | |
int[] CD = new int[]{12,16,24,28,32,36,48,64,68,72,76,84,92,96}; | |
int[] B = new int[]{2,4,6,8}; | |
int[] F = new int[]{2,4,6,8}; | |
int[] FGH = new int[]{216, 248, 264, 296, 416, 432, 472, 496, 624, 632, 648, 672, 816, 824, 832, 864, 872, 896}; | |
int[] valid = new int[]{1,2,3,4,6,7,8,9}; | |
validate(CD, B, F, FGH, valid); | |
} | |
private void validate(int[] CD, int[] B, int[] F, int[] FGH, int[] valid) { | |
for(int i=0; i < FGH.length; i++) { | |
int fgh = FGH[i]; | |
int[] CDExtracted = extract(fgh, CD); | |
for (int j=0; j < CDExtracted.length; j++) { | |
String n = fgh + "" + CDExtracted[j]; | |
int[] BExtracted = extract(Integer.valueOf(n), B); | |
for (int k=0; k < BExtracted.length; k++) { | |
String l = n + "" + BExtracted[k]; | |
int[] validExtracted = extract(Integer.valueOf(l), valid); | |
int preNumber = validExtracted[0]; | |
int lastNumber = validExtracted[1]; | |
String number = "" + preNumber + BExtracted[k] + CDExtracted[j] + "5" + fgh + lastNumber + "0"; | |
if (isValidNumber(number)) { | |
System.out.println(number); | |
} | |
} | |
} | |
} | |
} | |
private int[] extract(int fgh, int[] cd) { | |
String _fgh = fgh + ""; | |
List<Integer> result = new ArrayList<>(); | |
for(int i=0; i < cd.length; i++) { | |
boolean exists = false; | |
String n = cd[i] + ""; | |
for (int j=0; j < _fgh.length(); j++) { | |
if (n.contains(_fgh.charAt(j) + "")) { | |
exists = true; | |
} | |
} | |
if (!exists) { | |
result.add(cd[i]); | |
} | |
} | |
return result.stream().mapToInt(i->i).toArray(); | |
} | |
private boolean isValidNumber(String number) { | |
boolean result = true; | |
String n = ""; | |
for (int i=1; i <= number.length(); i++) { | |
n += "" + number.charAt(i - 1); | |
long division = Long.valueOf(n) % i; | |
if (division != 0) { | |
result = false; | |
} | |
} | |
return result; | |
} |
The number has the format ABCD5FGHI0
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
3816547290