Skip to content

Commit 89e57b6

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

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

project_euler/problem_095/sol1.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
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
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
77
amicable chain under a given limit.
88
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
1111
chain.
1212
"""
1313

14+
1415
def sum_of_proper_divisors(number: int) -> int:
1516
"""Calculate the sum of proper divisors of the given number.
1617
@@ -37,6 +38,7 @@ def sum_of_proper_divisors(number: int) -> int:
3738

3839
return total
3940

41+
4042
def find_longest_amicable_chain(limit: int) -> int:
4143
"""Find the smallest member of the longest amicable chain under a given limit.
4244
@@ -47,7 +49,9 @@ def find_longest_amicable_chain(limit: int) -> int:
4749
"""
4850
sum_divisors = {} # Dictionary to store the sum of proper divisors for each number
4951
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
5155

5256
longest_chain = [] # To store the longest amicable chain found
5357
seen = {} # Dictionary to track numbers already processed
@@ -62,7 +66,9 @@ def find_longest_amicable_chain(limit: int) -> int:
6266
while current <= limit and current not in chain:
6367
chain.append(current) # Add the current number to the chain
6468
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
6672

6773
# Check if we form a cycle and validate the chain
6874
if current in chain and current != start:
@@ -73,7 +79,10 @@ def find_longest_amicable_chain(limit: int) -> int:
7379
if len(chain) > len(longest_chain):
7480
longest_chain = chain # Update longest chain if this one is longer
7581

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+
7786

7887
def solution() -> int:
7988
"""Return the smallest member of the longest amicable chain under one million.
@@ -83,6 +92,7 @@ def solution() -> int:
8392
"""
8493
return find_longest_amicable_chain(10**6)
8594

95+
8696
if __name__ == "__main__":
8797
smallest_member = solution() # Call the solution function
8898
print(smallest_member) # Output the smallest member of the longest amicable chain

0 commit comments

Comments
 (0)