-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add solution for the Euler project problem 95. #12669
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
Merged
Merged
Changes from all commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
9dd0d84
Add documentation and tests for the Euler project problem 95 solution.
mindaugl 091b592
Update sol1.py
MaximSmolskiy 2d04e70
Update sol1.py
MaximSmolskiy 8b7e69e
Update sol1.py
MaximSmolskiy 4b7893e
Update sol1.py
MaximSmolskiy 88c438f
Update sol1.py
MaximSmolskiy 70def15
Update sol1.py
MaximSmolskiy a500695
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a05f9a4
Update sol1.py
MaximSmolskiy 28dfdb6
Update sol1.py
MaximSmolskiy c2223f5
Update sol1.py
MaximSmolskiy bded104
Update sol1.py
MaximSmolskiy b22afa5
Update sol1.py
MaximSmolskiy cfd347c
Update sol1.py
MaximSmolskiy 7dbbf03
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2d32910
Update sol1.py
MaximSmolskiy 24c99a6
Update sol1.py
MaximSmolskiy 3ff43b9
Update sol1.py
MaximSmolskiy 5e62ac6
Update sol1.py
MaximSmolskiy 3953d1d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e0f7394
Update sol1.py
MaximSmolskiy 9293662
Update sol1.py
MaximSmolskiy ca6abd9
Update sol1.py
MaximSmolskiy 7c8138e
Update sol1.py
MaximSmolskiy 475a907
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 55e4081
Update sol1.py
MaximSmolskiy 0fa14b6
Update sol1.py
MaximSmolskiy e836686
Update sol1.py
MaximSmolskiy 3887c77
Update sol1.py
MaximSmolskiy 11a8439
Update sol1.py
MaximSmolskiy 3522c2c
Update sol1.py
MaximSmolskiy 09c13a1
Update sol1.py
MaximSmolskiy 5af1642
Update sol1.py
MaximSmolskiy ad68f4c
Update sol1.py
MaximSmolskiy 265ce55
Update sol1.py
MaximSmolskiy ab032d6
Update sol1.py
MaximSmolskiy afb78f5
Update sol1.py
MaximSmolskiy 7f6a6d8
Update sol1.py
MaximSmolskiy 2375c33
Update sol1.py
MaximSmolskiy 05c2c51
Update sol1.py
MaximSmolskiy 41a91c7
Update sol1.py
MaximSmolskiy e5a2fbf
Update sol1.py
MaximSmolskiy fe0b493
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1ba90a4
Update sol1.py
MaximSmolskiy 1500f9b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1cf3b31
Update sol1.py
MaximSmolskiy 01417b6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 755cd55
Update sol1.py
MaximSmolskiy ab9ae2e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 021ef25
Update sol1.py
MaximSmolskiy 647d0eb
Update sol1.py
MaximSmolskiy 256b67c
Update sol1.py
MaximSmolskiy ff8f731
Update sol1.py
MaximSmolskiy c9d0f70
Update sol1.py
MaximSmolskiy 92a58d5
Update sol1.py
MaximSmolskiy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
""" | ||
Project Euler Problem 95: https://projecteuler.net/problem=95 | ||
|
||
Amicable Chains | ||
|
||
The proper divisors of a number are all the divisors excluding the number itself. | ||
For example, the proper divisors of 28 are 1, 2, 4, 7, and 14. | ||
As the sum of these divisors is equal to 28, we call it a perfect number. | ||
|
||
Interestingly the sum of the proper divisors of 220 is 284 and | ||
the sum of the proper divisors of 284 is 220, forming a chain of two numbers. | ||
For this reason, 220 and 284 are called an amicable pair. | ||
|
||
Perhaps less well known are longer chains. | ||
For example, starting with 12496, we form a chain of five numbers: | ||
12496 -> 14288 -> 15472 -> 14536 -> 14264 (-> 12496 -> ...) | ||
|
||
Since this chain returns to its starting point, it is called an amicable chain. | ||
|
||
Find the smallest member of the longest amicable chain with | ||
no element exceeding one million. | ||
|
||
Solution is doing the following: | ||
- Get relevant prime numbers | ||
- Iterate over product combination of prime numbers to generate all non-prime | ||
numbers up to max number, by keeping track of prime factors | ||
- Calculate the sum of factors for each number | ||
- Iterate over found some factors to find longest chain | ||
""" | ||
|
||
from math import isqrt | ||
|
||
|
||
def generate_primes(max_num: int) -> list[int]: | ||
""" | ||
Calculates the list of primes up to and including `max_num`. | ||
|
||
>>> generate_primes(6) | ||
[2, 3, 5] | ||
""" | ||
are_primes = [True] * (max_num + 1) | ||
are_primes[0] = are_primes[1] = False | ||
for i in range(2, isqrt(max_num) + 1): | ||
if are_primes[i]: | ||
for j in range(i * i, max_num + 1, i): | ||
are_primes[j] = False | ||
|
||
MaximSmolskiy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return [prime for prime, is_prime in enumerate(are_primes) if is_prime] | ||
|
||
|
||
def multiply( | ||
chain: list[int], | ||
primes: list[int], | ||
min_prime_idx: int, | ||
prev_num: int, | ||
max_num: int, | ||
prev_sum: int, | ||
primes_degrees: dict[int, int], | ||
) -> None: | ||
""" | ||
Run over all prime combinations to generate non-prime numbers. | ||
|
||
>>> chain = [0] * 3 | ||
>>> primes_degrees = {} | ||
>>> multiply( | ||
... chain=chain, | ||
... primes=[2], | ||
... min_prime_idx=0, | ||
... prev_num=1, | ||
... max_num=2, | ||
... prev_sum=0, | ||
... primes_degrees=primes_degrees, | ||
... ) | ||
>>> chain | ||
[0, 0, 1] | ||
>>> primes_degrees | ||
{2: 1} | ||
""" | ||
|
||
min_prime = primes[min_prime_idx] | ||
num = prev_num * min_prime | ||
|
||
min_prime_degree = primes_degrees.get(min_prime, 0) | ||
min_prime_degree += 1 | ||
primes_degrees[min_prime] = min_prime_degree | ||
|
||
new_sum = prev_sum * min_prime + (prev_sum + prev_num) * (min_prime - 1) // ( | ||
min_prime**min_prime_degree - 1 | ||
) | ||
chain[num] = new_sum | ||
|
||
for prime_idx in range(min_prime_idx, len(primes)): | ||
if primes[prime_idx] * num > max_num: | ||
break | ||
|
||
multiply( | ||
chain=chain, | ||
primes=primes, | ||
min_prime_idx=prime_idx, | ||
prev_num=num, | ||
max_num=max_num, | ||
prev_sum=new_sum, | ||
primes_degrees=primes_degrees.copy(), | ||
) | ||
|
||
|
||
def find_longest_chain(chain: list[int], max_num: int) -> int: | ||
""" | ||
Finds the smallest element of longest chain | ||
|
||
>>> find_longest_chain(chain=[0, 0, 0, 0, 0, 0, 6], max_num=6) | ||
6 | ||
""" | ||
|
||
max_len = 0 | ||
min_elem = 0 | ||
for start in range(2, len(chain)): | ||
visited = {start} | ||
elem = chain[start] | ||
length = 1 | ||
|
||
while elem > 1 and elem <= max_num and elem not in visited: | ||
visited.add(elem) | ||
elem = chain[elem] | ||
length += 1 | ||
|
||
if elem == start and length > max_len: | ||
max_len = length | ||
min_elem = start | ||
|
||
return min_elem | ||
|
||
|
||
def solution(max_num: int = 1000000) -> int: | ||
""" | ||
Runs the calculation for numbers <= `max_num`. | ||
|
||
>>> solution(10) | ||
6 | ||
>>> solution(200000) | ||
12496 | ||
""" | ||
|
||
primes = generate_primes(max_num) | ||
chain = [0] * (max_num + 1) | ||
for prime_idx, prime in enumerate(primes): | ||
if prime**2 > max_num: | ||
break | ||
|
||
multiply( | ||
chain=chain, | ||
primes=primes, | ||
min_prime_idx=prime_idx, | ||
prev_num=1, | ||
max_num=max_num, | ||
prev_sum=0, | ||
primes_degrees={}, | ||
) | ||
|
||
return find_longest_chain(chain=chain, max_num=max_num) | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"{solution() = }") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.