-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
feat: add algorithm 95 to find the longest amicable chain #12113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
1eee73b
3349351
a450acb
7679c20
ab3769c
0d963a1
f467350
96be505
89e57b6
021fefa
f4c0a23
ebccfe3
c501b3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
Project Euler Problem: https://projecteuler.net/problem=95 | ||
|
||
An amicable chain is a sequence of numbers where each number is the sum of the | ||
proper divisors of the previous one, and the chain eventually returns to the | ||
starting number. The problem is to find the smallest member of the longest | ||
amicable chain under a given limit. | ||
|
||
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. | ||
""" | ||
|
||
<<<<<<< HEAD | ||
Check failure on line 14 in project_euler/problem_095/sol1.py
|
||
======= | ||
Check failure on line 15 in project_euler/problem_095/sol1.py
|
||
|
||
def sum_of_proper_divisors(number: int) -> int: | ||
"""Calculate the sum of proper divisors of the given number. | ||
>>>>>>> 89e57b63f62908f575161ca2c077f02c189a363c | ||
|
||
def sum_of_proper_divisors(n): | ||
abda-gaye marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide descriptive name for the parameter: |
||
"""Calculate the sum of proper divisors of n.""" | ||
if n < 2: | ||
return 0 # Proper divisors of 0 and 1 are none. | ||
total = 1 # Start with 1, since it is a proper divisor of any n > 1 | ||
sqrt_n = int(n**0.5) # Calculate the integer square root of n. | ||
|
||
# Loop through possible divisors from 2 to the square root of n | ||
for i in range(2, sqrt_n + 1): | ||
if n % i == 0: # Check if i is a divisor of n | ||
total += i # Add the divisor | ||
if i != n // i: # Avoid adding the square root twice | ||
total += n // i # Add the corresponding divisor (n/i) | ||
|
||
return total | ||
|
||
<<<<<<< HEAD | ||
======= | ||
|
||
def find_longest_amicable_chain(limit: int) -> int: | ||
"""Find the smallest member of the longest amicable chain under a given limit. | ||
>>>>>>> 89e57b63f62908f575161ca2c077f02c189a363c | ||
|
||
def find_longest_amicable_chain(limit): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's okay There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: |
||
"""Find the smallest member of the longest amicable chain under a given limit.""" | ||
sum_divisors = {} # Dictionary to store the sum of proper divisors for each number | ||
for i in range(1, limit + 1): | ||
sum_divisors[i] = sum_of_proper_divisors( | ||
i | ||
) # Calculate and store sum of proper divisors | ||
|
||
longest_chain = [] # To store the longest amicable chain found | ||
seen = {} # Dictionary to track numbers already processed | ||
|
||
# Iterate through each number to find amicable chains | ||
for start in range(1, limit + 1): | ||
if start in seen: # Skip if this number is already processed | ||
continue | ||
|
||
chain = [] # Initialize the current chain | ||
current = start # Start with the current number | ||
while current <= limit and current not in chain: | ||
chain.append(current) # Add the current number to the chain | ||
seen[current] = True # Mark this number as seen | ||
current = sum_divisors.get( | ||
current, 0 | ||
) # Move to the next number in the chain | ||
|
||
# Check if we form a cycle and validate the chain | ||
if current in chain and current != start: | ||
cycle_start_index = chain.index(current) # Find where the cycle starts | ||
if current in sum_divisors and sum_divisors[current] in chain: | ||
# This means we have a valid amicable chain | ||
chain = chain[cycle_start_index:] # Take only the cycle part | ||
if len(chain) > len(longest_chain): | ||
longest_chain = chain # Update longest chain if this one is longer | ||
|
||
return ( | ||
min(longest_chain) if longest_chain else None | ||
) # Return the smallest member of the longest chain | ||
<<<<<<< HEAD | ||
======= | ||
|
||
>>>>>>> 89e57b63f62908f575161ca2c077f02c189a363c | ||
|
||
|
||
def solution(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's okay There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file |
||
"""Return the smallest member of the longest amicable chain under one million.""" | ||
return find_longest_amicable_chain(10**6) | ||
|
||
|
||
if __name__ == "__main__": | ||
smallest_member = solution() # Call the solution function | ||
print(smallest_member) # Output the smallest member of the longest amicable chain |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An error occurred while parsing the file:
project_euler/problem_095/sol1.py