project_euler.problem_095.sol1¶
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
Functions¶
|
Finds the smallest element of longest chain |
|
Calculates the list of primes up to and including max_num. |
|
Run over all prime combinations to generate non-prime numbers. |
|
Runs the calculation for numbers <= max_num. |
Module Contents¶
- project_euler.problem_095.sol1.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
- project_euler.problem_095.sol1.generate_primes(max_num: int) list[int] ¶
Calculates the list of primes up to and including max_num.
>>> generate_primes(6) [2, 3, 5]
- project_euler.problem_095.sol1.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}
- project_euler.problem_095.sol1.solution(max_num: int = 1000000) int ¶
Runs the calculation for numbers <= max_num.
>>> solution(10) 6 >>> solution(200000) 12496