forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp068.py
More file actions
45 lines (36 loc) · 1.03 KB
/
p068.py
File metadata and controls
45 lines (36 loc) · 1.03 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
#
# 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
#
import eulerlib
def compute():
state = list(range(1, 11))
max = None
while True:
sum = state[0] + state[5] + state[6]
if state[1] + state[6] + state[7] == sum and \
state[2] + state[7] + state[8] == sum and \
state[3] + state[8] + state[9] == sum and \
state[4] + state[9] + state[5] == sum:
minouterindex = 0
minouter = state[0]
for i in range(1, 5):
if state[i] < minouter:
minouterindex = i
minouter = state[i]
s = ""
for i in range(5):
s += str(state[(minouterindex + i) % 5])
s += str(state[(minouterindex + i) % 5 + 5])
s += str(state[(minouterindex + i + 1) % 5 + 5])
if len(s) == 16 and (max is None or s > max):
max = s
if not eulerlib.next_permutation(state):
break
assert max is not None
return max
if __name__ == "__main__":
print(compute())