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": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"443839\n"
]
"data": {
"text/plain": [
"{4150, 4151, 54748, 92727, 93084, 194979}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import cache\n",
"\n",
"@cache\n",
"def F(n, p):\n",
" total = 0\n",
" while n != 0:\n",
" total += (n % 10) ** p\n",
" n //= 10\n",
" q = n // 10\n",
" if q == 0:\n",
" return n ** p\n",
" \n",
" return total\n",
" return (n % 10) ** p + F(q, p)\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)"
]
},
{