inf-abi2027/01 Einführung/07 Rekursion/04 Pascal.ipynb

222 lines
5.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.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
}