Rekursion

main
Alexander Kimmig 2025-11-10 09:57:31 +00:00
parent 08c364b3e8
commit 98820db55f
5 changed files with 725 additions and 3 deletions

View File

@ -124,9 +124,6 @@
"id": "58a9cc6d-683d-4f86-84b7-600d80d7b06d", "id": "58a9cc6d-683d-4f86-84b7-600d80d7b06d",
"metadata": { "metadata": {
"editable": false, "editable": false,
"jupyter": {
"source_hidden": true
},
"slideshow": { "slideshow": {
"slide_type": "" "slide_type": ""
}, },

View File

@ -0,0 +1,130 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "95d22754-cc8b-4187-9bf2-214aa285c737",
"metadata": {
"editable": false,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"# Rekursion\n",
"\n",
"*\"Wer Rekursion verstehen will, muss vorher Rekursion verstehen.\"*\n",
"\n",
"Unter einer **rekursiven Funktion** versteht man eine Funktion, die **sich selbst wieder aufruft**.\n",
"\n",
"Damit diese Aufrufe nicht *unendlich* weitergehen und das Programm zu einem Ergebnis kommt, brauchen wir eine **Abbruchbedingung**."
]
},
{
"cell_type": "markdown",
"id": "4390feea-e740-4eac-bc30-2170b5a50c6b",
"metadata": {
"editable": false,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"## Hinweis: Abbrechen der Ausführung\n",
"\n",
"Kommt es im Jupyter-Notbook zu einer *Endlosschleife* bzw. nicht abbrechende Rekursion, so würde das Programm ohne Ende weiter laufen.\n",
"\n",
"Läuft die Ausführung im Jupyter immer weiter (erkennbar am Stern vor der Zeile), so muss die Ausführung manuell gestoppt werden!"
]
},
{
"cell_type": "markdown",
"id": "ad00d229-4993-46b4-950b-2638d5632853",
"metadata": {
"editable": false,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"## Beispiel\n",
"\n",
"Die Summe von $1$ bis zu einer Zahl $n$ lässt sich auch rekursiv berechnen, denn es gilt:\n",
"$$\\mathrm{summe}(1) = 1$$\n",
"sowie\n",
"$$\\mathrm{summe}(n) = n + \\mathrm{summe}(n-1)$$\n",
"\n",
"Eine rekursive Funktion lässt sich also folgendermaßen programmieren:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f83dd8bc-1f01-4a34-8ea3-6bc2822ff47d",
"metadata": {
"editable": false,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[33m21\u001b[39m"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function summe(n) {\n",
" // Abbruchbedingung: wenn n===1 ist das Ergebnis auch 1\n",
" if (n === 1) return 1\n",
"\n",
" // andernfalls ist das Ergebnis n + summe(n-1)\n",
" return n + summe( n-1 )\n",
"}\n",
"summe(6)"
]
},
{
"cell_type": "markdown",
"id": "9116f0d3-2c22-4356-88de-3fa960ac5cc4",
"metadata": {
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"Die Zeile `return n + summe( n-1 )` sorgt mit einem rekursiven Aufruf dazu, dass die Summe berechnet wird.\n",
"\n",
"Die erste Zeile `if (n === 1) return 1` ist dafür verantwortlich, dass die Aufrufe abbrechen."
]
}
],
"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
}

View File

@ -0,0 +1,182 @@
{
"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
}

View File

@ -0,0 +1,192 @@
{
"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.2 - Fibonacci\n",
"\n",
"Die Fibonacci Folge $F$\n",
"$$0,1,1,2,3,5,8,13,21,34,55,\\dots$$\n",
"ist folgendermaßen definiert:\n",
"* $F(0)=0$\n",
"* $F(1)=1$\n",
"* $F(2)=F(0)+F(1)$\n",
"* $F(3)=F(1)+F(2)$\n",
"* bzw. allgemein: $F(n)=F(n-2)+F(n-1)$\n",
"\n",
"Diese soll nun mit einer Funktion `fibo` programmiert werden, die einen Parameter $n$ übergeben bekommt und dann die $n$-te Fibonacci-Zahl berechnet.\n",
"\n",
"### Hinweis:\n",
"$0$ ist die \"nullte\" Zahl, 1 die erste."
]
},
{
"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 `fibo` 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 Funtion `fibo`:"
]
},
{
"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": true,
"jupyter": {
"source_hidden": true
},
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"let _nr = \"01.07.2\"\n",
"Deno.test(`${_nr}: function`, () => {\n",
" assertEquals(typeof fibo, 'function')\n",
"})\n",
"Deno.test(`${_nr}: Parameter`, () => {\n",
" assertEquals(fibo.length, 1)\n",
"})\n",
"Deno.test(`${_nr}: fibo(1)=1`, () => {\n",
" assertEquals(fibo(1), 1)\n",
"})\n",
"Deno.test(`${_nr}: fibo(3)=2`, () => {\n",
" assertEquals(fibo(3), 2)\n",
"})\n",
"Deno.test(`${_nr}: fibo(5)=5`, () => {\n",
" assertEquals(fibo(5), 5)\n",
"})\n",
"Deno.test(`${_nr}: fibo(11)=89`, () => {\n",
" assertEquals(fibo(11), 89)\n",
"})\n",
"Deno.test(`${_nr}: fibo(23)=28657`, () => {\n",
" assertEquals(fibo(23), 28657)\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
}

View File

@ -0,0 +1,221 @@
{
"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.3 - Pascal'sches Dreieck\n",
"\n",
"Das Pascal'sche Dreieck sieht folgendermaßen aus:\n",
"\n",
"$$\\begin{matrix}1\\\\1\\quad1\\\\1\\quad2\\quad1\\\\1\\quad3\\quad3\\quad1\\\\1\\quad4\\quad6\\quad4\\quad1\\\\1\\quad5\\quad10\\quad10\\quad5\\quad1\\\\1\\quad6\\quad15\\quad20\\quad15\\quad6\\quad1\\\\\\vdots\\qquad\\qquad\\vdots\\qquad\\qquad\\vdots\\end{matrix}$$\n",
"\n",
"Die Zahlen darin sollen mit der Methode `binom(n,k)` berechnet werden, wobei $n$ die Zeile und $k$ die Spalte (innerhalb der Zeile) angibt.\n",
"\n",
"### Hinweis:\n",
"Die Zeilen- und Spaltennummerierung beginnt bei 0!"
]
},
{
"cell_type": "markdown",
"id": "b84a4891-d9e8-43ca-a0cd-b7b7a1e700ed",
"metadata": {
"editable": false,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"## Teil 1\n",
"Recherchiere im Internet, wie das Pascal'sche Dreieck bzw. dessen Inhalte **rekursiv** berechnet werden können."
]
},
{
"cell_type": "raw",
"id": "9a45ec13-32cd-420c-9c45-3f317edce6fb",
"metadata": {
"editable": true,
"raw_mimetype": "",
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": []
},
{
"cell_type": "markdown",
"id": "81e736ba-89cd-4d1a-9b98-8475de6820bd",
"metadata": {
"editable": false,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"## Teil 2\n",
"Notiere zunächst die rekursiven Funktionsaufrufe für das konkrete Zahlenbeispiel $n=3$ und $k=2$:"
]
},
{
"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 3\n",
"Wann bricht die Rekursion ab, d.h. wann wird nicht mehr erneut die Funktion `binom` 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 Funtion `binom`:"
]
},
{
"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.3\"\n",
"Deno.test(`${_nr}: function`, () => {\n",
" assertEquals(typeof binom, 'function')\n",
"})\n",
"Deno.test(`${_nr}: Parameter`, () => {\n",
" assertEquals(binom.length, 2)\n",
"})\n",
"Deno.test(`${_nr}: binom(0,0)=1`, () => {\n",
" assertEquals(binom(0,0), 1)\n",
"})\n",
"Deno.test(`${_nr}: binom(2,1)=2`, () => {\n",
" assertEquals(binom(2,1), 2)\n",
"})\n",
"Deno.test(`${_nr}: binom(5,3)=10`, () => {\n",
" console.start()\n",
" assertEquals(binom(5,3), 10)\n",
" console.end()\n",
"})\n",
"Deno.test(`${_nr}: binom(9,4)=126`, () => {\n",
" console.start()\n",
" assertEquals(binom(9,4), 126)\n",
" console.end()\n",
"})\n",
"Deno.test(`${_nr}: binom(20,7)=77520`, () => {\n",
" console.start()\n",
" assertEquals(binom(20,7), 77520)\n",
" console.end()\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
}