Skip to content

add : Project Euler 95 solution #9172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Empty file.
41 changes: 41 additions & 0 deletions project_euler/problem_095/sol1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Problem 95
Url: https://projecteuler.net/problem=95
"""
def solution(n: int = 10**6) -> int:
"""
Returns the smallest member when n = 1000000
>> 14316
"""

sum_of_div = [0] * (n + 1)
for i in range(1, n // 2 + 1):
for j in range(i * 2, n + 1, i):
sum_of_div[j] += i

checked = [False] * (n + 1)
max_len_of_chain = 0
result = 0
for i in range(2, n + 1):
possible_chain = []
j = i
while not checked[j]:
checked[j] = True
possible_chain.append(j)
j = sum_of_div[j]
if j > n:
break
if j in possible_chain:
len_of_chain = len(possible_chain) - possible_chain.index(j)
if len_of_chain > max_len_of_chain:
max_len_of_chain = len_of_chain
result = min(possible_chain[-len_of_chain:])
break
return result


if __name__ == "__main__":
import doctest

doctest.testmod()
print(f"{solution() = }")