1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* Taken from https://projecteuler.net/problem=24
* (Modified slightly so numbers fit in a 32-bit int.) */
public class DigitPermutations {
public static void main(String[] args) {
int digits = 876543210;
int permutationsToSkip = 99_999;
for (int remainingDigits = 8; remainingDigits >= 0; remainingDigits--) {
int possibilities = factorial(remainingDigits);
int index = permutationsToSkip / possibilities;
permutationsToSkip -= index * possibilities;
System.out.println(getDigit(digits, index));
digits = removeDigit(digits, index);
}
}
public static int factorial(int n) {
int product = 1;
while (n > 0) {
product *= n--;
}
return product;
}
public static int getDigit(int digits, int index) {
while (index > 0) {
digits /= 10;
index--;
}
return digits % 10;
}
public static int removeDigit(int digits, int index) {
int powerOfTen = 1;
while (index > 0) {
powerOfTen *= 10;
index--;
}
return digits / powerOfTen / 10 * powerOfTen + digits % powerOfTen;
}
}