inf-abi2027/01 Einführung/07 Rekursion/02 Fakultät.ipynb

183 lines
4.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "0609fa9a-6337-43c0-a8d2-f947558066ac",
"metadata": {
"editable": false,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"import { assertEquals } from \"jsr:@std/assert\"\n",
"import \"https://git.amgdhg.de/kg/tslib/raw/branch/main/logger.ts\""
]
},
{
"cell_type": "markdown",
"id": "e2253715-005d-4a31-b577-4ba32a314da6",
"metadata": {
"editable": false,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"# Aufgabe 01.07.1 - Fakultät\n",
"\n",
"Eine wichtige mathematische Funktion ist die Fakultät:\n",
"$$n! = 1\\cdot2\\cdot3\\cdots(n-2)\\cdot(n-1)\\cdot n$$\n",
"zum Beispiel:\n",
"$$5!=1\\cdot2\\cdot3\\cdot4\\cdot5=120$$\n",
"\n",
"Ziel soll sein, die Funktion `fakultaet` rekursiv zu programmieren. Dafür gibt es zunächst einige Vorüberlegungen:"
]
},
{
"cell_type": "markdown",
"id": "81e736ba-89cd-4d1a-9b98-8475de6820bd",
"metadata": {
"editable": false,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"## Teil 1\n",
"Notiere zunächst die rekursiven Funktionsaufrufe für das konkrete Zahlenbeispiel $n=5$:"
]
},
{
"cell_type": "raw",
"id": "58f5ec36-ae74-4ff0-9e47-4f0f83adbd7b",
"metadata": {
"editable": true,
"raw_mimetype": "",
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": []
},
{
"cell_type": "markdown",
"id": "ba4b1f2a-51cc-47ff-8b1f-f8444cddd7b9",
"metadata": {
"editable": false,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"## Teil 2\n",
"Wann bricht die Rekursion ab, d.h. wann wird nicht mehr erneut die Funktion `fakultaet` aufgerufen?"
]
},
{
"cell_type": "raw",
"id": "261e0c80-0169-424f-840a-47c774029ced",
"metadata": {
"editable": true,
"raw_mimetype": "",
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": []
},
{
"cell_type": "markdown",
"id": "37316573-632d-440f-9674-1dddbd5039f3",
"metadata": {
"editable": false,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"## Teil 3\n",
"Programmiere die Funktion `fakultaet`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b8c40034-1b97-458a-9e30-648c41cb3d7c",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e114cca7-8b8a-4f7e-8861-1a507a679d3d",
"metadata": {
"editable": false,
"jupyter": {
"source_hidden": true
},
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"let _nr = \"01.07.1\"\n",
"Deno.test(`${_nr}: function`, () => {\n",
" assertEquals(typeof fakultaet, 'function')\n",
"})\n",
"Deno.test(`${_nr}: Parameter`, () => {\n",
" assertEquals(fakultaet.length, 1)\n",
"})\n",
"Deno.test(`${_nr}: fakultaet(1)=1`, () => {\n",
" assertEquals(fakultaet(1), 1)\n",
"})\n",
"Deno.test(`${_nr}: fakultaet(3)=6`, () => {\n",
" assertEquals(fakultaet(3), 6)\n",
"})\n",
"Deno.test(`${_nr}: fakultaet(5)=120`, () => {\n",
" assertEquals(fakultaet(5), 120)\n",
"})\n",
"Deno.test(`${_nr}: fakultaet(11)=39916800`, () => {\n",
" assertEquals(fakultaet(11), 39916800)\n",
"})"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Deno",
"language": "typescript",
"name": "deno"
},
"language_info": {
"codemirror_mode": "typescript",
"file_extension": ".ts",
"mimetype": "text/x.typescript",
"name": "typescript",
"nbconvert_exporter": "script",
"pygments_lexer": "typescript",
"version": "5.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}