Skip to content

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 55 commits into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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 Apr 13, 2025
091b592
Update sol1.py
MaximSmolskiy May 6, 2025
2d04e70
Update sol1.py
MaximSmolskiy May 6, 2025
8b7e69e
Update sol1.py
MaximSmolskiy May 6, 2025
4b7893e
Update sol1.py
MaximSmolskiy May 6, 2025
88c438f
Update sol1.py
MaximSmolskiy May 6, 2025
70def15
Update sol1.py
MaximSmolskiy May 6, 2025
a500695
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 6, 2025
a05f9a4
Update sol1.py
MaximSmolskiy May 6, 2025
28dfdb6
Update sol1.py
MaximSmolskiy May 6, 2025
c2223f5
Update sol1.py
MaximSmolskiy May 6, 2025
bded104
Update sol1.py
MaximSmolskiy May 6, 2025
b22afa5
Update sol1.py
MaximSmolskiy May 6, 2025
cfd347c
Update sol1.py
MaximSmolskiy May 6, 2025
7dbbf03
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 6, 2025
2d32910
Update sol1.py
MaximSmolskiy May 6, 2025
24c99a6
Update sol1.py
MaximSmolskiy May 6, 2025
3ff43b9
Update sol1.py
MaximSmolskiy May 6, 2025
5e62ac6
Update sol1.py
MaximSmolskiy May 6, 2025
3953d1d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 6, 2025
e0f7394
Update sol1.py
MaximSmolskiy May 6, 2025
9293662
Update sol1.py
MaximSmolskiy May 6, 2025
ca6abd9
Update sol1.py
MaximSmolskiy May 6, 2025
7c8138e
Update sol1.py
MaximSmolskiy May 6, 2025
475a907
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 6, 2025
55e4081
Update sol1.py
MaximSmolskiy May 6, 2025
0fa14b6
Update sol1.py
MaximSmolskiy May 6, 2025
e836686
Update sol1.py
MaximSmolskiy May 6, 2025
3887c77
Update sol1.py
MaximSmolskiy May 6, 2025
11a8439
Update sol1.py
MaximSmolskiy May 6, 2025
3522c2c
Update sol1.py
MaximSmolskiy May 6, 2025
09c13a1
Update sol1.py
MaximSmolskiy May 6, 2025
5af1642
Update sol1.py
MaximSmolskiy May 6, 2025
ad68f4c
Update sol1.py
MaximSmolskiy May 6, 2025
265ce55
Update sol1.py
MaximSmolskiy May 6, 2025
ab032d6
Update sol1.py
MaximSmolskiy May 6, 2025
afb78f5
Update sol1.py
MaximSmolskiy May 6, 2025
7f6a6d8
Update sol1.py
MaximSmolskiy May 6, 2025
2375c33
Update sol1.py
MaximSmolskiy May 7, 2025
05c2c51
Update sol1.py
MaximSmolskiy May 7, 2025
41a91c7
Update sol1.py
MaximSmolskiy May 7, 2025
e5a2fbf
Update sol1.py
MaximSmolskiy May 7, 2025
fe0b493
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 7, 2025
1ba90a4
Update sol1.py
MaximSmolskiy May 7, 2025
1500f9b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 7, 2025
1cf3b31
Update sol1.py
MaximSmolskiy May 7, 2025
01417b6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 7, 2025
755cd55
Update sol1.py
MaximSmolskiy May 7, 2025
ab9ae2e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 7, 2025
021ef25
Update sol1.py
MaximSmolskiy May 7, 2025
647d0eb
Update sol1.py
MaximSmolskiy May 7, 2025
256b67c
Update sol1.py
MaximSmolskiy May 10, 2025
ff8f731
Update sol1.py
MaximSmolskiy May 10, 2025
c9d0f70
Update sol1.py
MaximSmolskiy May 10, 2025
92a58d5
Update sol1.py
MaximSmolskiy May 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
145 changes: 145 additions & 0 deletions project_euler/problem_095/sol1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
"""
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 numpy import sqrt


def sum_primes(factor_d, num):
"""
Calculates the sum of factors from all prime exponents.

>>> sum_primes({2: 1, 3: 1}, 6)
6
"""
tot = 1
for p in factor_d:
comp = 0
ex_factor = 1
for _ in range(factor_d[p] + 1):
comp += ex_factor
ex_factor *= p
tot *= comp
return tot - num


def generate_primes(n: int):
"""
Calculates the list of primes up to and including n.

>>> generate_primes(6)
[2, 3, 5]
"""
primes = [True] * (n + 1)
primes[0] = primes[1] = False
for i in range(2, int(sqrt(n + 1)) + 1):
if primes[i]:
j = i * i
while j <= n:
primes[j] = False
j += i
primes_list = []
for i in range(2, len(primes)):
if primes[i]:
primes_list += [i]
return primes_list


def multiply(chain, primes, prime, prev_n, n_max, prev_sum, primes_d):
"""
Run over all prime combinations to generate non-prime numbers.

>>> multiply([None] * 3, {2}, 2, 1, 2, 0, {})
"""

number = prev_n * prime
primes_d[prime] = primes_d.get(prime, 0) + 1
if prev_n % prime != 0:
new_sum = prev_sum * (prime + 1) + prev_n
else:
new_sum = sum_primes(primes_d, number)
chain[number] = new_sum
for p in primes:
if p >= prime:
number_n = p * number
if number_n > n_max:
break
multiply(chain, primes, p, number, n_max, new_sum, primes_d.copy())


def find_longest_chain(chain, n_max):
"""
Finds the smallest element and length of longest chain

>>> find_longest_chain([0, 0, 0, 0, 0, 0, 6], 6)
(6, 1)
"""

length_max = 0
elem_max = 0
for i in range(2, len(chain)):
start = i
length = 1
el = chain[i]
visited = {i}
while el > 1 and el <= n_max and el not in visited:
length += 1
visited.add(el)
el = chain[el]

if el == start and length > length_max:
length_max = length
elem_max = start

return elem_max, length_max


def solution(n_max: int = 1000000) -> int:
"""
Runs the calculation for numbers <= n_max.

>>> solution(10)
6
>>> solution(200000)
12496
"""

primes = generate_primes(n_max)
chain = [0] * (n_max + 1)
for p in primes:
if p * p > n_max:
break
multiply(chain, primes, p, 1, n_max, 0, {})

chain_start, _ = find_longest_chain(chain, n_max)
return chain_start


if __name__ == "__main__":
print(f"{solution() = }")