Skip to content

Commit 320f8f2

Browse files
committed
Project euler 95
1 parent 5f8d1cb commit 320f8f2

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

project_euler/problem_095/__init__.py

Whitespace-only changes.

project_euler/problem_095/sol1.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Problem 95
3+
Url: https://projecteuler.net/problem=95
4+
"""
5+
def find_smallest_member(n: int) -> int:
6+
"""
7+
Returns the smallest member of the longest amicable chain with no element exceeding one million
8+
>> 14316
9+
"""
10+
11+
sum_of_div = [0] * (n + 1)
12+
for i in range(1, n // 2 + 1):
13+
for j in range(i * 2, n + 1, i):
14+
sum_of_div[j] += i
15+
16+
checked = [False] * (n + 1)
17+
max_len_of_chain = 0
18+
result = 0
19+
for i in range(2, n + 1):
20+
possible_chain = []
21+
j = i
22+
while not checked[j]:
23+
checked[j] = True
24+
possible_chain.append(j)
25+
j = sum_of_div[j]
26+
if j > n:
27+
break
28+
if j in possible_chain:
29+
len_of_chain = len(possible_chain) - possible_chain.index(j)
30+
if len_of_chain > max_len_of_chain:
31+
max_len_of_chain = len_of_chain
32+
result = min(possible_chain[-len_of_chain:])
33+
break
34+
return result
35+
36+
37+
if __name__ == "__main__":
38+
print(f"Solution : {find_smallest_member(10**6)}")

0 commit comments

Comments
 (0)