From 7906360a1550d0e568d18fbe9099cef43afe373b Mon Sep 17 00:00:00 2001 From: Martin Putzlocher Date: Mon, 25 Apr 2022 16:41:06 +0200 Subject: [PATCH] Iteration und Rekursion am Beispiel von "2 hoch n" --- algorithmen/iteration_rekursion_exp2.py | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 algorithmen/iteration_rekursion_exp2.py diff --git a/algorithmen/iteration_rekursion_exp2.py b/algorithmen/iteration_rekursion_exp2.py new file mode 100644 index 0000000..e28d833 --- /dev/null +++ b/algorithmen/iteration_rekursion_exp2.py @@ -0,0 +1,29 @@ + +# Iteration + +def exp2(n): + """ Berechne "2 hoch n" über eine Schleife + """ + if n == 0: + return 1 + else: + result = 1 + for i in range(n): # Die Berechnung erfolgt in einer Schleife + result = result * 2 + print(result) + return result + +# Rekursion +def exp2rek(n): + """ Berechne "2 hoch n" über Rekursiven Aufruf + """ + if n == 0: + return 1 + else: + result = exp2rek(n-1) * 2 # Die Funktion ruft sich selbst wieder auf + print(result) + return result + +exp2(20) +print("-"*10) +exp2rek(20)