feat: добавлена рукурсивная функция factorial()

This commit is contained in:
ArtemPlugnokov committed 2026-11-04 16:37:21 +03:00
1 parent 17e198abec
commit 71bccd7fc9
1 file changed
+44 -4
+44 -4
View File
@@ -16,7 +16,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 28,
"metadata": { "metadata": {
"id": "vpCgcTgTjL3H" "id": "vpCgcTgTjL3H"
}, },
@@ -48,7 +48,7 @@
"metadata": { "metadata": {
"id": "8hUGAyi8jrzi" "id": "8hUGAyi8jrzi"
}, },
"execution_count": 23, "execution_count": 29,
"outputs": [] "outputs": []
}, },
{ {
@@ -69,9 +69,9 @@
"base_uri": "https://localhost:8080/" "base_uri": "https://localhost:8080/"
}, },
"id": "wUcGE8CjjzE-", "id": "wUcGE8CjjzE-",
"outputId": "2d59d814-9fdf-484b-d908-04cee289f368" "outputId": "5ce87d69-8cbe-4eef-b5b3-758f4b49eb95"
}, },
"execution_count": 25, "execution_count": 30,
"outputs": [ "outputs": [
{ {
"output_type": "stream", "output_type": "stream",
@@ -87,6 +87,46 @@
] ]
} }
] ]
},
{
"cell_type": "code",
"source": [
"def factorial(n: int) -> int:\n",
" \"\"\"Рекурсивный факториал. n >= 0.\"\"\"\n",
" if n < 0:\n",
" raise ValueError(\"factorial не определён для отрицательных чисел\")\n",
" return 1 if n <= 1 else n * factorial(n - 1)"
],
"metadata": {
"id": "ig6llIpyxFgF"
},
"execution_count": 31,
"outputs": []
},
{
"cell_type": "code",
"source": [
"print(f\"5! = {factorial(5)}\")\n",
"print(f\"10! = {factorial(10)}\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "uY-uaNMdxOBF",
"outputId": "238436d9-a806-45eb-e251-360cf52a8ef5"
},
"execution_count": 32,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"5! = 120\n",
"10! = 3628800\n"
]
}
]
} }
] ]
} }