forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp041.java
42 lines (33 loc) · 889 Bytes
/
p041.java
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
39
40
41
42
/*
* Solution to Project Euler problem 41
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p041 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p041().run());
}
public String run() {
for (int n = 9; n >= 1; n--) {
int[] digits = new int[n];
for (int i = 0; i < digits.length; i++)
digits[i] = i + 1;
int result = -1;
do {
if (Library.isPrime(toInteger(digits)))
result = toInteger(digits);
} while (Library.nextPermutation(digits));
if (result != -1)
return Integer.toString(result);
}
throw new RuntimeException("Not found");
}
private static int toInteger(int[] digits) {
int result = 0;
for (int x : digits)
result = result * 10 + x;
return result;
}
}