forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp068.java
More file actions
52 lines (42 loc) · 1.29 KB
/
p068.java
File metadata and controls
52 lines (42 loc) · 1.29 KB
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
43
44
45
46
47
48
49
50
51
52
/*
* Solution to Project Euler problem 68
* Copyright (c) Project Nayuki. All rights reserved.
*
* https://www.nayuki.io/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p068 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p068().run());
}
public String run() {
int[] state = new int[10];
for (int i = 0; i < state.length; i++)
state[i] = i + 1;
String max = null;
do {
int sum = state[0] + state[5] + state[6];
if ( state[1] + state[6] + state[7] != sum
|| state[2] + state[7] + state[8] != sum
|| state[3] + state[8] + state[9] != sum
|| state[4] + state[9] + state[5] != sum)
continue;
int minOuterIndex = -1;
int minOuter = Integer.MAX_VALUE;
for (int i = 0; i < 5; i++) {
if (state[i] < minOuter) {
minOuterIndex = i;
minOuter = state[i];
}
}
String s = "";
for (int i = 0; i < 5; i++)
s += "" + state[(minOuterIndex + i) % 5] + state[(minOuterIndex + i) % 5 + 5] + state[(minOuterIndex + i + 1) % 5 + 5];
if (s.length() == 16 && (max == null || s.compareTo(max) > 0))
max = s;
} while (Library.nextPermutation(state));
if (max == null)
throw new AssertionError();
return max;
}
}