![Page 1: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/1.jpg)
Diskrete Mathe11 2 3 4 5 6 7
Diskrete Mathematik I
Rekursion
Vorlesung 3
![Page 2: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/2.jpg)
Diskrete Mathe11 2 3 4 5 6 71
• Das Prinzip der Rekursion• Beispiel: Fakultätsfunktion
Übersicht
![Page 3: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/3.jpg)
Diskrete Mathe11 2 3 4 5 6 72
Rekursion im Bild
![Page 4: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/4.jpg)
Diskrete Mathe11 2 3 4 5 6 7
• Eine rekursive Definition reduziert ein Problem auf ein kleineres Problem derselben Art.
• Eine rekursive Funktion setzt eine rekursive Definition in ein Programm um.
Das Prinzip der Rekursion
3
![Page 5: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/5.jpg)
Diskrete Mathe11 2 3 4 5 6 7
Beispiel: Fakultätsfunktion
Definition: Für n 0 gilt
A 1x
0,)!1(*
0,1!
nnn
nn
4
![Page 6: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/6.jpg)
Diskrete Mathe11 2 3 4 5 6 7
Programmkonstrukte von Java
• if (Bedingung) Anweisung1;else Anweisung2;
Wenn die Bedingung erfüllt ist, führe Anweisung1 aus,sonst Anweisung 2
• int my_function(...){
......return Integer_Ausdruck; .....
}
Aufruf: int i = my_function(...);
Die Funktion my_function liefert den integer-Ausdruck zurück, der hinter return steht.
5
![Page 7: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/7.jpg)
Diskrete Mathe11 2 3 4 5 6 7
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-1); else return -1;}
A 9x
Fakultät
0,)!1(*
0,1!
nnn
nn
6
![Page 8: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/8.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}
fak(4)n = 4
7
![Page 9: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/9.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}
4 != 0
fak(4)
7
![Page 10: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/10.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}
4 > 0
fak(4)
7
![Page 11: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/11.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}
4 * fak(3)
fak(4)
7
![Page 12: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/12.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}
4 * fak(3)
fak(4)
fak(4)=4*fak(3)
fak(3)
n = 3
7
![Page 13: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/13.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}
4 * fak(3)
fak(4)
fak(4)=4*fak(3)
3 != 0
fak(3)
7
![Page 14: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/14.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}
4 * fak(3)
fak(4)
fak(4)=4*fak(3)
3 > 0
fak(3)
7
![Page 15: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/15.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}
4 * fak(3)
fak(4)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)
7
![Page 16: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/16.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)3 * fak(2)
fak(3)=3*fak(2)
fak(2)
7
![Page 17: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/17.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)3 * fak(2)
fak(3)=3*fak(2)
2 * fak(1)
fak(2)
7
![Page 18: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/18.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}
4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)=3*fak(2)
3 * fak(2)
fak(3)=3*fak(2)
2 * fak(1)
fak(2)2 * fak(1)
fak(2)=2*fak(1)
fak(1)
7
![Page 19: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/19.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}
4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)=3*fak(2)
3 * fak(2)
fak(3)=3*fak(2)
2 * fak(1)
fak(2)=2*fak(1)
1 * fak(0)
fak(1)
7
![Page 20: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/20.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)=3*fak(2)
3 * fak(2)
fak(3)=3*fak(2)
4 * fak(3)
fak(3)=3*fak(2)
2 * fak(1)
fak(2)=2*fak(1)
3 * fak(2)
fak(2)=2*fak(1)
1 * fak(0)
fak(1)2 * fak(1)
fak(1)=1*fak(0)
fak(0)
7
![Page 21: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/21.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)=3*fak(2)
3 * fak(2)
fak(3)=3*fak(2)
4 * fak(3)
fak(3)=3*fak(2)
2 * fak(1)
fak(2)=2*fak(1)
3 * fak(2)
fak(2)=2*fak(1)
1 * fak(0)
fak(1)2 * fak(1)
fak(1)=1*fak(0)
n = 0
fak(0)
7
![Page 22: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/22.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)=3*fak(2)
3 * fak(2)
fak(3)=3*fak(2)
4 * fak(3)
fak(3)=3*fak(2)
2 * fak(1)
fak(2)=2*fak(1)
3 * fak(2)
fak(2)=2*fak(1)
1 * fak(0)
fak(1)2 * fak(1)
fak(1)=1*fak(0)
1
fak(0)
7
![Page 23: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/23.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)=3*fak(2)
3 * fak(2)
fak(3)=3*fak(2)
4 * fak(3)
fak(3)=3*fak(2)
2 * fak(1)
fak(2)=2*fak(1)
3 * fak(2)
fak(2)=2*fak(1)
1 * fak(0)
fak(1)2 * fak(1)
fak(1)=1*fak(0)
fak(0) = 1
7
![Page 24: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/24.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)=3*fak(2)
3 * fak(2)
fak(3)=3*fak(2)
4 * fak(3)
fak(3)=3*fak(2)
2 * fak(1)
fak(2)=2*fak(1)
3 * fak(2)
fak(2)=2*fak(1)
1 * fak(0)
fak(1)fak(0) = 1
fak(1)=1*fak(0)
7
![Page 25: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/25.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)=3*fak(2)
3 * fak(2)
fak(3)=3*fak(2)
4 * fak(3)
fak(3)=3*fak(2)
2 * fak(1)
fak(2)=2*fak(1)
3 * fak(2)
fak(2)=2*fak(1)
fak(1) = 1*1
7
![Page 26: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/26.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)=3*fak(2)
3 * fak(2)
fak(3)=3*fak(2)
4 * fak(3)
fak(3)=3*fak(2)
2 * fak(1)
fak(2)=2*fak(1)
3 * fak(2)
fak(2)=2*fak(1)
fak(1) = 1
7
![Page 27: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/27.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)=3*fak(2)
3 * fak(2)
fak(3)=3*fak(2)
4 * fak(3)
fak(3)=3*fak(2)
2 * fak(1)
fak(2)=2*fak(1)
fak(1) = 1
fak(2)=2*fak(1)
7
![Page 28: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/28.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)=3*fak(2)
3 * fak(2)
fak(3)=3*fak(2)
4 * fak(3)
fak(3)=3*fak(2)
fak(2) = 2*1
7
![Page 29: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/29.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)=3*fak(2)
3 * fak(2)
fak(3)=3*fak(2)
4 * fak(3)
fak(3)=3*fak(2)
fak(2) = 2
7
![Page 30: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/30.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)=3*fak(2)
3 * fak(2)
fak(3)=3*fak(2)
fak(2) = 2
fak(3)=3*fak(2)
7
![Page 31: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/31.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)=3*fak(2)
fak(3) = 3*2
7
![Page 32: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/32.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
3 * fak(2)
fak(3)=3*fak(2)
fak(3) = 6
7
![Page 33: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/33.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
fak(3) = 6
fak(4)=4*fak(3)
7
![Page 34: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/34.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
4 * fak(3)
fak(4)=4*fak(3)
fak(4) = 4*6
fak(4)=4*fak(3)
7
![Page 35: Diskrete Mathe1 1234567 Diskrete Mathematik I Rekursion Vorlesung 3](https://reader033.vdocuments.pub/reader033/viewer/2022061305/5514454d550346494e8b4b58/html5/thumbnails/35.jpg)
Diskrete Mathe11 2 3 4 5 6 7
A 29x
int fak(int n){ if(n==0) return 1; else if (n>0) return n * fak(n-
1); else return -1;}4 * fak(3)
fak(4)=4*fak(3)
fak(4) = 24
7