eulerbooks/notebooks/problem0023.ipynb

97 lines
2.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "22f94a3b",
"metadata": {},
"source": [
"# [Non-Abundant Sums](https://projecteuler.net/problem=23)\n",
"\n",
"Just like in [problem 21](https://projecteuler.net/problem=21), we'll define an `aliquot_sum` function and use that to find all the [abundant numbers](https://en.wikipedia.org/wiki/Abundant_number) below 28,124 (as in problem 21, we could instead use a sieve to compute the divisor sums)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "84f3ad0c",
"metadata": {},
"outputs": [],
"source": [
"aliquot_sum = lambda n: sigma(n) - n\n",
"abundant_numbers = {k for k in range(1, 28124) if aliquot_sum(k) > k}"
]
},
{
"cell_type": "markdown",
"id": "d6586d17",
"metadata": {},
"source": [
"Then we check every integer less than 28,124 to see if it's the sum of any two abundant numbers, and if it is, remove it from a set containing all those integers. Whatever's left in that set are the non-abundant sums."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6747d2a3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4179871"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"non_abundant_sums = set(range(1, 28124))\n",
"for n in range(1, 28124):\n",
" for m in abundant_numbers:\n",
" if n - m in abundant_numbers:\n",
" non_abundant_sums.discard(n)\n",
" break\n",
"\n",
"sum(non_abundant_sums)"
]
},
{
"cell_type": "markdown",
"id": "91a43bd1",
"metadata": {},
"source": [
"## Relevant sequences\n",
"* Sums of divisors: [A000203](https://oeis.org/A000203)\n",
"* Numbers that are not the sum of two abundant numbers: [A048242](https://oeis.org/A048242)\n",
"\n",
"#### Copyright (C) 2025 filifa\n",
"\n",
"This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "SageMath 9.5",
"language": "sage",
"name": "sagemath"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}