make function recursive

This commit is contained in:
filifa 2025-04-19 00:33:14 -04:00
parent 53d201e591
commit 814fbddabd
1 changed files with 38 additions and 11 deletions

View File

@ -21,24 +21,51 @@
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "data": {
"output_type": "stream", "text/plain": [
"text": [ "{4150, 4151, 54748, 92727, 93084, 194979}"
"443839\n" ]
] },
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
} }
], ],
"source": [ "source": [
"from functools import cache\n",
"\n",
"@cache\n",
"def F(n, p):\n", "def F(n, p):\n",
" total = 0\n", " q = n // 10\n",
" while n != 0:\n", " if q == 0:\n",
" total += (n % 10) ** p\n", " return n ** p\n",
" n //= 10\n",
" \n", " \n",
" return total\n", " return (n % 10) ** p + F(q, p)\n",
"\n", "\n",
"\n", "\n",
"print(sum(n for n in range(2, 354295) if n == F(n, 5)))" "pdis = {n for n in range(2, 354295) if n == F(n, 5)}\n",
"pdis"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "bda685a5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"443839"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(pdis)"
] ]
}, },
{ {