From 814fbddabde2b435538a5705a0253dac6a24aec9 Mon Sep 17 00:00:00 2001 From: filifa Date: Sat, 19 Apr 2025 00:33:14 -0400 Subject: [PATCH] make function recursive --- notebooks/problem0030.ipynb | 49 ++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/notebooks/problem0030.ipynb b/notebooks/problem0030.ipynb index 486bba2..a2849e3 100644 --- a/notebooks/problem0030.ipynb +++ b/notebooks/problem0030.ipynb @@ -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)" ] }, {