Skip to content

Commit 021fefa

Browse files
committed
feat: add algorithm 95 to find the longest amicable chain
1 parent 96be505 commit 021fefa

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

project_euler/problem_095/sol1.py

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,31 @@
1111
chain.
1212
"""
1313

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:
2718
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.
3021

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
3223
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
3425
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)
3728

3829
return total
3930

40-
def find_longest_amicable_chain(limit: int) -> int:
41-
"""Find the smallest member of the longest amicable chain under a given limit.
4231

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."""
4834
sum_divisors = {} # Dictionary to store the sum of proper divisors for each number
4935
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
5139

5240
longest_chain = [] # To store the longest amicable chain found
5341
seen = {} # Dictionary to track numbers already processed
@@ -62,7 +50,9 @@ def find_longest_amicable_chain(limit: int) -> int:
6250
while current <= limit and current not in chain:
6351
chain.append(current) # Add the current number to the chain
6452
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
6656

6757
# Check if we form a cycle and validate the chain
6858
if current in chain and current != start:
@@ -73,16 +63,16 @@ def find_longest_amicable_chain(limit: int) -> int:
7363
if len(chain) > len(longest_chain):
7464
longest_chain = chain # Update longest chain if this one is longer
7565

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
7769

78-
def solution() -> int:
79-
"""Return the smallest member of the longest amicable chain under one million.
8070

81-
>>> solution()
82-
14316
83-
"""
71+
def solution():
72+
"""Return the smallest member of the longest amicable chain under one million."""
8473
return find_longest_amicable_chain(10**6)
8574

75+
8676
if __name__ == "__main__":
8777
smallest_member = solution() # Call the solution function
8878
print(smallest_member) # Output the smallest member of the longest amicable chain

0 commit comments

Comments
 (0)