11
11
chain.
12
12
"""
13
13
14
- def sum_of_proper_divisors (number : int ) -> int :
15
- """Calculate the sum of proper divisors of the given number.
16
-
17
- >>> sum_of_proper_divisors(6)
18
- 6
19
- >>> sum_of_proper_divisors(28)
20
- 28
21
- >>> sum_of_proper_divisors(12)
22
- 16
23
- >>> sum_of_proper_divisors(1)
24
- 0
25
- """
26
- if number < 2 :
14
+
15
+ def sum_of_proper_divisors (n ):
16
+ """Calculate the sum of proper divisors of n."""
17
+ if n < 2 :
27
18
return 0 # Proper divisors of 0 and 1 are none.
28
- total = 1 # Start with 1, since it is a proper divisor of any number > 1
29
- sqrt_n = int (number ** 0.5 ) # Calculate the integer square root of number .
19
+ total = 1 # Start with 1, since it is a proper divisor of any n > 1
20
+ sqrt_n = int (n ** 0.5 ) # Calculate the integer square root of n .
30
21
31
- # Loop through possible divisors from 2 to the square root of number
22
+ # Loop through possible divisors from 2 to the square root of n
32
23
for i in range (2 , sqrt_n + 1 ):
33
- if number % i == 0 : # Check if i is a divisor of number
24
+ if n % i == 0 : # Check if i is a divisor of n
34
25
total += i # Add the divisor
35
- if i != number // i : # Avoid adding the square root twice
36
- total += number // i # Add the corresponding divisor (number /i)
26
+ if i != n // i : # Avoid adding the square root twice
27
+ total += n // i # Add the corresponding divisor (n /i)
37
28
38
29
return total
39
30
40
- def find_longest_amicable_chain (limit : int ) -> int :
41
- """Find the smallest member of the longest amicable chain under a given limit.
42
31
43
- >>> find_longest_amicable_chain(10**3)
44
- 624
45
- >>> find_longest_amicable_chain(10**6)
46
- 14316
47
- """
32
+ def find_longest_amicable_chain (limit ):
33
+ """Find the smallest member of the longest amicable chain under a given limit."""
48
34
sum_divisors = {} # Dictionary to store the sum of proper divisors for each number
49
35
for i in range (1 , limit + 1 ):
50
- sum_divisors [i ] = sum_of_proper_divisors (i ) # Calculate and store sum of proper divisors
36
+ sum_divisors [i ] = sum_of_proper_divisors (
37
+ i
38
+ ) # Calculate and store sum of proper divisors
51
39
52
40
longest_chain = [] # To store the longest amicable chain found
53
41
seen = {} # Dictionary to track numbers already processed
@@ -62,7 +50,9 @@ def find_longest_amicable_chain(limit: int) -> int:
62
50
while current <= limit and current not in chain :
63
51
chain .append (current ) # Add the current number to the chain
64
52
seen [current ] = True # Mark this number as seen
65
- current = sum_divisors .get (current , 0 ) # Move to the next number in the chain
53
+ current = sum_divisors .get (
54
+ current , 0
55
+ ) # Move to the next number in the chain
66
56
67
57
# Check if we form a cycle and validate the chain
68
58
if current in chain and current != start :
@@ -73,16 +63,16 @@ def find_longest_amicable_chain(limit: int) -> int:
73
63
if len (chain ) > len (longest_chain ):
74
64
longest_chain = chain # Update longest chain if this one is longer
75
65
76
- return min (longest_chain ) if longest_chain else None # Return the smallest member of the longest chain
66
+ return (
67
+ min (longest_chain ) if longest_chain else None
68
+ ) # Return the smallest member of the longest chain
77
69
78
- def solution () -> int :
79
- """Return the smallest member of the longest amicable chain under one million.
80
70
81
- >>> solution()
82
- 14316
83
- """
71
+ def solution ():
72
+ """Return the smallest member of the longest amicable chain under one million."""
84
73
return find_longest_amicable_chain (10 ** 6 )
85
74
75
+
86
76
if __name__ == "__main__" :
87
77
smallest_member = solution () # Call the solution function
88
78
print (smallest_member ) # Output the smallest member of the longest amicable chain
0 commit comments