summaryrefslogtreecommitdiff
path: root/src/zahlenspiel.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/zahlenspiel.py')
-rwxr-xr-xsrc/zahlenspiel.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/zahlenspiel.py b/src/zahlenspiel.py
new file mode 100755
index 0000000..e8891f9
--- /dev/null
+++ b/src/zahlenspiel.py
@@ -0,0 +1,52 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+# getestet mit Python 2.7.8 und 3.4.2
+'''Berechnet einen Bruch zur Übung des Kürzens'''
+
+from random import randrange
+
+def gcd(vala, valb):
+ '''Berechnet den größten gemeinsamen Teiler nach Euklid'''
+ while valb:
+ vala, valb = valb, vala % valb
+ return vala
+
+def bruch(stufe):
+ '''Berechnet einen ungekürzten Bruch vala/valb
+mit gekürztem Bruch valp/valq nach Schwierigkeits`stufe`.
+`stufe` ist 0 (leicht), 1 (mittel) oder 2 (schwer)'''
+
+ length = 4 if stufe == 0 else 5
+
+ erfolg = False
+ while not erfolg:
+ suchen = True
+ while suchen:
+ valp = randrange(1, (stufe + 1) * 10)
+ valq = randrange(stufe * 10 - valp, (stufe + 1) * 10 - valp)
+ suchen = (gcd(valp, valq) != 1) # falls nicht kürzbar
+
+ valx = 2
+ suchen = True
+ while suchen:
+ vala, valb = valp * valx, valq * valx
+
+ if len(str(vala)) + len(str(valb)) == length:
+ suchen = False
+ erfolg = True
+ else:
+ valx += 1
+ if len(str(vala)) + len(str(valb)) > length:
+ suchen = erfolg = False # neue Zahlen generieren
+
+ return vala, valb, valp, valq
+
+def aufgaben(stufe, anzahl):
+ '''gibt `anzahl` Aufgaben mit `stufe` (0 - leicht, 2 - schwer) aus'''
+ for _ in range(anzahl):
+ vala, valb, valp, valq = bruch(stufe)
+ print(str(vala) + " / " + str(valb) + \
+ " = " + str(valp) + " / " + str(valq))
+
+if __name__ == "__main__":
+ aufgaben(randrange(4), randrange(1, 10))