Skip to content

Commit a450acb

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 3349351 commit a450acb

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

project_euler/problem_095/sol1.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,31 @@
88
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.
99
"""
1010

11+
1112
def sum_of_proper_divisors(n):
1213
"""Calculate the sum of proper divisors of n."""
1314
if n < 2:
1415
return 0 # Proper divisors of 0 and 1 are none.
1516
total = 1 # Start with 1, since it is a proper divisor of any n > 1
1617
sqrt_n = int(n**0.5) # Calculate the integer square root of n.
17-
18+
1819
# Loop through possible divisors from 2 to the square root of n
1920
for i in range(2, sqrt_n + 1):
2021
if n % i == 0: # Check if i is a divisor of n
2122
total += i # Add the divisor
2223
if i != n // i: # Avoid adding the square root twice
2324
total += n // i # Add the corresponding divisor (n/i)
24-
25+
2526
return total
2627

28+
2729
def find_longest_amicable_chain(limit):
2830
"""Find the smallest member of the longest amicable chain under a given limit."""
2931
sum_divisors = {} # Dictionary to store the sum of proper divisors for each number
3032
for i in range(1, limit + 1):
31-
sum_divisors[i] = sum_of_proper_divisors(i) # Calculate and store sum of proper divisors
33+
sum_divisors[i] = sum_of_proper_divisors(
34+
i
35+
) # Calculate and store sum of proper divisors
3236

3337
longest_chain = [] # To store the longest amicable chain found
3438
seen = {} # Dictionary to track numbers already processed
@@ -43,7 +47,9 @@ def find_longest_amicable_chain(limit):
4347
while current <= limit and current not in chain:
4448
chain.append(current) # Add the current number to the chain
4549
seen[current] = True # Mark this number as seen
46-
current = sum_divisors.get(current, 0) # Move to the next number in the chain
50+
current = sum_divisors.get(
51+
current, 0
52+
) # Move to the next number in the chain
4753

4854
# Check if we form a cycle and validate the chain
4955
if current in chain and current != start:
@@ -54,12 +60,16 @@ def find_longest_amicable_chain(limit):
5460
if len(chain) > len(longest_chain):
5561
longest_chain = chain # Update longest chain if this one is longer
5662

57-
return min(longest_chain) if longest_chain else None # Return the smallest member of the longest chain
63+
return (
64+
min(longest_chain) if longest_chain else None
65+
) # Return the smallest member of the longest chain
66+
5867

5968
def solution():
6069
"""Return the smallest member of the longest amicable chain under one million."""
6170
return find_longest_amicable_chain(10**6)
6271

72+
6373
if __name__ == "__main__":
6474
smallest_member = solution() # Call the solution function
6575
print(smallest_member) # Output the smallest member of the longest amicable chain

0 commit comments

Comments
 (0)