MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_049/sol1.py:95–149  ·  view source on GitHub ↗

Return the solution of the problem. >>> solution() 296962999629

()

Source from the content-addressed store, hash-verified

93
94
95def solution():
96 """
97 Return the solution of the problem.
98 >>> solution()
99 296962999629
100 """
101 prime_list = [n for n in range(1001, 10000, 2) if is_prime(n)]
102 candidates = []
103
104 for number in prime_list:
105 tmp_numbers = []
106
107 for prime_member in permutations(list(str(number))):
108 prime = int("".join(prime_member))
109
110 if prime % 2 == 0:
111 continue
112
113 if search(prime, prime_list):
114 tmp_numbers.append(prime)
115
116 tmp_numbers.sort()
117 if len(tmp_numbers) >= 3:
118 candidates.append(tmp_numbers)
119
120 passed = []
121 for candidate in candidates:
122 length = len(candidate)
123 found = False
124
125 for i in range(length):
126 for j in range(i + 1, length):
127 for k in range(j + 1, length):
128 if (
129 abs(candidate[i] - candidate[j])
130 == abs(candidate[j] - candidate[k])
131 and len({candidate[i], candidate[j], candidate[k]}) == 3
132 ):
133 passed.append(
134 sorted([candidate[i], candidate[j], candidate[k]])
135 )
136 found = True
137
138 if found:
139 break
140 if found:
141 break
142 if found:
143 break
144
145 answer = set()
146 for seq in passed:
147 answer.add("".join([str(i) for i in seq]))
148
149 return max(int(x) for x in answer)
150
151
152if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls 5

sortMethod · 0.80
is_primeFunction · 0.70
searchFunction · 0.70
appendMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected