1
1
"""
2
2
Project Euler Problem: https://projecteuler.net/problem=95
3
3
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
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
7
amicable chain under a given limit.
8
8
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
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
11
chain.
12
12
"""
13
13
14
+
14
15
def sum_of_proper_divisors (number : int ) -> int :
15
16
"""Calculate the sum of proper divisors of the given number.
16
17
@@ -37,6 +38,7 @@ def sum_of_proper_divisors(number: int) -> int:
37
38
38
39
return total
39
40
41
+
40
42
def find_longest_amicable_chain (limit : int ) -> int :
41
43
"""Find the smallest member of the longest amicable chain under a given limit.
42
44
@@ -47,7 +49,9 @@ def find_longest_amicable_chain(limit: int) -> int:
47
49
"""
48
50
sum_divisors = {} # Dictionary to store the sum of proper divisors for each number
49
51
for i in range (1 , limit + 1 ):
50
- sum_divisors [i ] = sum_of_proper_divisors (i ) # Calculate and store sum of proper divisors
52
+ sum_divisors [i ] = sum_of_proper_divisors (
53
+ i
54
+ ) # Calculate and store sum of proper divisors
51
55
52
56
longest_chain = [] # To store the longest amicable chain found
53
57
seen = {} # Dictionary to track numbers already processed
@@ -62,7 +66,9 @@ def find_longest_amicable_chain(limit: int) -> int:
62
66
while current <= limit and current not in chain :
63
67
chain .append (current ) # Add the current number to the chain
64
68
seen [current ] = True # Mark this number as seen
65
- current = sum_divisors .get (current , 0 ) # Move to the next number in the chain
69
+ current = sum_divisors .get (
70
+ current , 0
71
+ ) # Move to the next number in the chain
66
72
67
73
# Check if we form a cycle and validate the chain
68
74
if current in chain and current != start :
@@ -73,7 +79,10 @@ def find_longest_amicable_chain(limit: int) -> int:
73
79
if len (chain ) > len (longest_chain ):
74
80
longest_chain = chain # Update longest chain if this one is longer
75
81
76
- return min (longest_chain ) if longest_chain else None # Return the smallest member of the longest chain
82
+ return (
83
+ min (longest_chain ) if longest_chain else None
84
+ ) # Return the smallest member of the longest chain
85
+
77
86
78
87
def solution () -> int :
79
88
"""Return the smallest member of the longest amicable chain under one million.
@@ -83,6 +92,7 @@ def solution() -> int:
83
92
"""
84
93
return find_longest_amicable_chain (10 ** 6 )
85
94
95
+
86
96
if __name__ == "__main__" :
87
97
smallest_member = solution () # Call the solution function
88
98
print (smallest_member ) # Output the smallest member of the longest amicable chain
0 commit comments