Skip to content

Commit 7679c20

Browse files
committed
feat: add algorithm 95 to find the longest amicable chain
1 parent 3349351 commit 7679c20

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

project_euler/problem_095/sol1.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""
22
Project Euler Problem: https://projecteuler.net/problem=95
33
4-
An amicable chain is a sequence of numbers where each number is the sum of the proper divisors of the previous one, and the chain eventually returns to the starting number.
4+
An amicable chain is a sequence of numbers where each number is the sum of the
5+
proper divisors of the previous one, and the chain eventually returns to the
6+
starting number. The problem is to find the smallest member of the longest
7+
amicable chain under a given limit.
58
6-
The problem is to find the smallest member of the longest amicable chain under a given limit.
7-
8-
In this implementation, we aim to identify all amicable chains and find the one with the maximum length, while also returning the smallest member of that chain.
9+
In this implementation, we aim to identify all amicable chains and find the
10+
one with the maximum length, while also returning the smallest member of that
11+
chain.
912
"""
1013

1114
def sum_of_proper_divisors(n):
@@ -14,14 +17,14 @@ def sum_of_proper_divisors(n):
1417
return 0 # Proper divisors of 0 and 1 are none.
1518
total = 1 # Start with 1, since it is a proper divisor of any n > 1
1619
sqrt_n = int(n**0.5) # Calculate the integer square root of n.
17-
20+
1821
# Loop through possible divisors from 2 to the square root of n
1922
for i in range(2, sqrt_n + 1):
2023
if n % i == 0: # Check if i is a divisor of n
2124
total += i # Add the divisor
2225
if i != n // i: # Avoid adding the square root twice
2326
total += n // i # Add the corresponding divisor (n/i)
24-
27+
2528
return total
2629

2730
def find_longest_amicable_chain(limit):

0 commit comments

Comments
 (0)