Die Präsentation wird geladen. Bitte warten

Die Präsentation wird geladen. Bitte warten

Rekursive Funktionen (Fakultät) class Fakultaet { static long fak(long n) { if (n <= 0) return 1; else return n * fak(n-1); } public static void main(String[]

Ähnliche Präsentationen


Präsentation zum Thema: "Rekursive Funktionen (Fakultät) class Fakultaet { static long fak(long n) { if (n <= 0) return 1; else return n * fak(n-1); } public static void main(String[]"—  Präsentation transkript:

1 Rekursive Funktionen (Fakultät) class Fakultaet { static long fak(long n) { if (n <= 0) return 1; else return n * fak(n-1); } public static void main(String[] args) { System.out.println(20 + "! = " + fak(20)); } >>>def fakt (n): if n==0: return 1 else: return fakt(n-1)*n >>> print fak(20) % fakt fakt(0,1). fakt(N,F):- N1 is N-1, fakt(N1,L), F is N*L. % Nach Konsultation der obigen WB antwortet das System auf die Anfrage: ?- fakt(20,F). F =

2 Rekursive Funktionen (ggT) class Teiler { static int ggT(int m,n) { if (m==n) return m; else if (m>n) return ggT(m-n,n); else return ggT(m,n-m); } public static void main(String[] args) { System.out.println( ggT(24,3) = "+ggT(24,3); } >>>def ggT(m,n): if m==n: print m elif m > n: return ggT(m-n,n) else: return ggT (m,n-m) >>>print ggT(24,3) 8 % ggT ggT(M,M,M). ggT(M,N,G):-M>N, H is M-N,ggT(H,N,G). ggT(M,N,G):-H1 is N-M, ggT(M,H1,G). ?- ggT(24,3,G). G = 8

3 ggT Rekursionsbaum

4 Rekursive Funktionen (Fibonacci) class Fibonacci { static int fibo(int n) { if (n <2) return 1; else return fibo(n-2)+fibo(n-1); } public static void main(String[] args) { System.out.println(fibo(6) = " + fibo(6)); } >>>def fibo (n): if n < 2: return 1 else: return fibo(n-1) + fibo(n-2) >>>print fibo(6) 13 % fibo fibo(0,1). fibo(1,1). fibo(N,M):-N1 is N-1, N2 is N- 2,fibo(N1,M1),fibo(N2,M2),M is M1+M2. ? Fibo(6,M). M = 13

5 Fibonacci Rekursionsbaum

6 Rekursive Funktionen (Ulam) int n=17; void ulam() { if (n>1){ if (n%2==0) n=n/2; else n=3*n+1; ulam(); } ulam(X):- X==1. ulam(X):- X1 is (X mod 2), X1==0, Z is X/2,write(Z),write(' '),ulam(Z). ulam(X):- Z1 is 3*X+1,write(Z1), write(' '),ulam(Z1). def ulam(n): while n>1: if n%2 == 0: n=n/2 return n else: n=3*n+1 return n return ulam(n)

7 Rekursive Funktionen (McCarthy) class macCarthy { static int mcCarthy (int n) { if (n < 100) return n-10; else return mcCarthy(mcCarthy(n+11)); } public static void main (String[] args) { System.out.println (mcCarthy(7)= +mcCarthy(7)); } >>>def mcCarthy (n): if n>100: return n-10 else: return mcCarthy(mcCarthy (n+11)) >>>print mcCarthy(88) 91 mcCarthy(X,Y):- X>100, Y is X-10. mcCarthy(X,Y):- X1 is X+11, mcCarthy(X1,Y1), mcCarthy(Y1,Y).

8 McCarthy Rekursionsbaum

9 Rekursive Funktionen (Potenz) class potenz { static int pot (int x, int n) { if (n==0) return 1; else return x*(pot(x,n-1)); } public static void main(String[] args) { System.out.println(7³= + pot(7,3)); } >>> def pot (x,n): if n == 0: return 1 else: return x* (pot (x,n-1)) >>>print pot(7,3) 343 pot(X,0,1). Pot(X,N,P):- N>0, N1 is N-1, pot(X,N1,P1), P is X * P1.

10 Rekursive Funktionen (Summe) public class rekursion { int summe(int n){ if (n==1) return 1; else return summe(n -1) + n; } public static void main(String[] args) { System.out.println(sum(4)=summe(4)); } >>> summe(n, s=0): if n == 1: return 1 return summe(n-1,s+n) >>> print summe(4) 10 summe(1,1). summe(N,S):- N1 is N-1, summe(N1,S1), S is S1 + N. ?- summe(4,S) S = 10

11 Rekursive Funktionen (Quadratsumme) public class rekursion { int qsumme(int n){ if (n==1) return 1; else return (n*n) + qsumme(n-1); } public static void main(String[] args) { System.out.println(qsum(4)=qsumme(4)); } >>>def quadr_sum(n,qs=0): if n == 1: return 1 qs += n**2 + quadr_sum(n-1,qs) return qs >>> print quadr_sum(4) 30 qsumme(1,1). qsumme(N,Q):- N1 is N - 1, qsumme(N1,Q1), Q is Q1 + (N*N). ?- qsumme(4,Q). Q = 30

12 Rekursive Funktionen (Binomialkoeffizient) public class Binomi { static long binomi(int n, int k) { if (k == 0 || n == k) return 1; else return binomi(n-1, k- 1) + binomi(n-1,k);} public static void main(String[] args) { System.out.println("Binomialkoeffizient von 5 über 2 =" + binomi(5,2)); } } >>>def binomi(n,k): if k == 0 or n == k: return 1 else: return binomi(n-1,k-1)+ binomi(n-1,k) >>> print binomi(5,2) 10 % Binomialkoeffizient binomi(_,0,1). binomi(N,N,1). binomi(N,K,B):- N1 is N-1, K1 is K-1, binomi(N1,K1,B1), binomi(N1,K,B2), B is B1+B2. ? binomi(5,2,B). B = 10


Herunterladen ppt "Rekursive Funktionen (Fakultät) class Fakultaet { static long fak(long n) { if (n <= 0) return 1; else return n * fak(n-1); } public static void main(String[]"

Ähnliche Präsentationen


Google-Anzeigen