<jupyter_start><jupyter_text>Problem 21 Amicable numbersLet $d(n)$ be defined as the sum of proper divisors of $n$ (numbers less than $n$ which divide evenly into $n$).If $d(a) = b$ and $d(b) = a$, where $a \ne b$, then $a$ and $b$ are an amicable pair and each of $a$ and $b$ are called amicable numbers.For example, the proper divisors of $220$ are $1, 2, 4, 5, 10, 11, 20, 22, 44, 55$ and $110$; therefore $d(220) = 284$. The proper divisors of $284$ are $1, 2, 4, 71$ and $142$; so $d(284) = 220$.Evaluate the sum of all the amicable numbers under $10000$.OEIS Sequence: [A063990](https://oeis.org/A063990) Solution<jupyter_code>from math import isqrt
from euler.primes import prime_numbers
from euler.numbers import sum_proper_divisors
def compute(n: int) -> int:
    primes = list(prime_numbers(isqrt(n)))
    sum_factors = [0] * (n + 1)
    result = 0
    for i in range(1, n + 1):
        sum_factors[i] = sum_proper_divisors(i)
    for i in range(2, n + 1):
        j = sum_factors[i]
        if j != i and j <= n and sum_factors[j] == i:
            result += i
    return result
compute(1_000)
compute(10_000)
%timeit -n 100 -r 1 -p 6 compute(10_000)<jupyter_output>132.57 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 100 loops each)
{'fraction_non_alphanumeric': 0.13402889245585875, 'fraction_numerical': 0.08025682182985554, 'mean_word_length': 3.5345454545454547, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '7193', 'license': 'MIT', 'n_tokens_mistral': 550, 'n_tokens_neox': 451, 'n_words': 178, 'path': 'problems/0021/solution.ipynb', 'repo_name': 'Dynortice/didactic-rotary-phone', 'size': '1246'}