Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
| Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
| gf_informatik:algorithmen_ii [2026-04-17 06:49] – [Aufgabe B1: Iterative Algorithmen] hof | gf_informatik:algorithmen_ii [2026-04-17 11:24] (aktuell) – [Auftrag zu Primzahlen] hof | ||
|---|---|---|---|
| Zeile 16: | Zeile 16: | ||
| * Teil 3: Schreibe eine Funktion `max3(a, b, c)`, die das Maximum von **drei** Zahlen zurückgibt. _Verwende dazu die `max2` Funktion!_ | * Teil 3: Schreibe eine Funktion `max3(a, b, c)`, die das Maximum von **drei** Zahlen zurückgibt. _Verwende dazu die `max2` Funktion!_ | ||
| - | <nodisp | + | <html>< |
| - | ++++Lösung| | + | <script type=" |
| - | <html><bottom-editor> | + | def max2(a, b): |
| + | """ | ||
| + | |||
| + | def max3(a, b, c): | ||
| + | """ | ||
| + | |||
| + | print(max2(5, | ||
| + | print(max3(1, 2, 3)) | ||
| + | </script> | ||
| + | <script type=" | ||
| + | assert max2(5, 3) == 5 | ||
| + | assert max2(5, 5) == 5 | ||
| + | assert max2(3, 5) == 5 | ||
| + | |||
| + | assert max3(1, 2, 3) == 3 | ||
| + | assert max3(3, 2, 1) == 3 | ||
| + | assert max3(1, 3, 2) == 3 | ||
| + | </script> | ||
| + | <script type=" | ||
| def max2(a, b): | def max2(a, b): | ||
| + | """ | ||
| if a > b: | if a > b: | ||
| return a | return a | ||
| Zeile 26: | Zeile 45: | ||
| def max3(a, b, c): | def max3(a, b, c): | ||
| + | """ | ||
| return max2(max2(a, | return max2(max2(a, | ||
| print(max3(int(input(' | print(max3(int(input(' | ||
| - | </ | + | </ |
| - | ++++ | + | </ |
| - | </ | + | |
| === Aufgabe A2: Sortieren === | === Aufgabe A2: Sortieren === | ||
| Zeile 220: | Zeile 240: | ||
| ++++ | ++++ | ||
| - | <nodisp 1> | + | <html>< |
| - | ++++Lösung:| | + | <script type=" |
| - | <html><bottom-editor> | + | def quersumme(x): |
| + | """ | ||
| + | </script> | ||
| + | <script type=" | ||
| + | assert quersumme(413) == 8 | ||
| + | assert quersumme(1398) == 21 | ||
| + | </ | ||
| + | <script type=" | ||
| def quersumme(x): | def quersumme(x): | ||
| summe = 0 | summe = 0 | ||
| Zeile 232: | Zeile 259: | ||
| print(quersumme(413)) | print(quersumme(413)) | ||
| - | </ | + | </ |
| - | ++++ | + | </ |
| - | </nodisp> | + | |
| ==== Primzahlen ==== | ==== Primzahlen ==== | ||
| Zeile 274: | Zeile 300: | ||
| - | <nodisp 1> | + | <html>< |
| - | ++++Lösung:| | + | <script type=" |
| - | <html><bottom-editor> | + | def is_divisor(dividend, |
| + | """ | ||
| + | |||
| + | def is_prime(n): | ||
| + | """ | ||
| + | </script> | ||
| + | <script type=" | ||
| + | assert is_divisor(9, | ||
| + | assert not is_divisor(10, | ||
| + | |||
| + | assert is_prime(2) | ||
| + | assert is_prime(3) | ||
| + | assert is_prime(5) | ||
| + | assert is_prime(97) | ||
| + | |||
| + | assert not is_prime(1) | ||
| + | assert not is_prime(4) | ||
| + | assert not is_prime(16) | ||
| + | assert not is_prime(221) | ||
| + | </ | ||
| + | <script type=" | ||
| import math | import math | ||
| Zeile 297: | Zeile 343: | ||
| # Keinen Teiler gefunden -> wir haben eine Primzahl! | # Keinen Teiler gefunden -> wir haben eine Primzahl! | ||
| return True | return True | ||
| - | </ | + | </ |
| - | ++++ | + | </ |
| - | </nodisp> | + | |
| Zeile 309: | Zeile 354: | ||
| * Schreibe eine Funktion `prime_factors(x)`, | * Schreibe eine Funktion `prime_factors(x)`, | ||
| - | <nodisp 1> | + | <html>< |
| - | ++++Lösung: | + | <script type=" |
| - | Mit den Funktionen `is_prime` und `is_divisor` von oben ([[https://wtp.ethz.ch/#?code=NobwRAdghgtgpmAXGGUCWEB0AHAnmAGjABMoAXKJMNGbAewCcyACVMgCwB0Jnvi4AZszQBnAPrE0AN1GMAFJJn8IxA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_vRL03Tw3BOcJR0MBJGAXRg2B2BdWjU2AAC-AC6QA& | + | def prime_factors(n): |
| - | <code python> | + | """ |
| + | </script> | ||
| + | <script type=" | ||
| + | import math | ||
| + | |||
| + | def is_prime(n): | ||
| + | """ | ||
| + | if n < 2: | ||
| + | return False | ||
| + | t = 2 | ||
| + | while t <= math.sqrt(n): | ||
| + | if n % t == 0: | ||
| + | return False | ||
| + | t = t + 1 | ||
| + | return True | ||
| def next_prime(n): | def next_prime(n): | ||
| """ | """ | ||
| Zeile 328: | Zeile 388: | ||
| # For each prime, divide the remainder as long as the prime is | # For each prime, divide the remainder as long as the prime is | ||
| # a divisor. | # a divisor. | ||
| - | while is_divisor(remainder, factor): | + | while remainder |
| remainder = remainder / factor | remainder = remainder / factor | ||
| print(factor) | print(factor) | ||
| Zeile 337: | Zeile 397: | ||
| prime_factors(int(input())) | prime_factors(int(input())) | ||
| - | </code> | + | </script> |
| - | ++++ | + | </bottom-exercise></ |
| - | </nodisp> | + | |
| ==== Weitere anspruchsvolle Algorithmen ==== | ==== Weitere anspruchsvolle Algorithmen ==== | ||
| Zeile 348: | Zeile 407: | ||
| * Schreibe eine Funktion `ggT(x,y)`, die zwei Zahlen `x` und `y` entgegennimmt und den ggT der beiden zurückgibt. | * Schreibe eine Funktion `ggT(x,y)`, die zwei Zahlen `x` und `y` entgegennimmt und den ggT der beiden zurückgibt. | ||
| - | <nodisp 1> | + | <html>< |
| - | ++++Lösung:| | + | <script type=" |
| - | <html><bottom-editor> | + | def ggt(a, b): |
| + | """ | ||
| + | </script> | ||
| + | <script type=" | ||
| + | assert ggt(15, 10) == 5 | ||
| + | assert ggt(544, 391) == 17 | ||
| + | </ | ||
| + | <script type=" | ||
| def ggt(a, b): | def ggt(a, b): | ||
| """ | """ | ||
| Zeile 360: | Zeile 426: | ||
| print(ggt(544, | print(ggt(544, | ||
| - | </ | + | </ |
| - | ++++ | + | </ |
| - | </nodisp> | + | |
| Zeile 370: | Zeile 435: | ||
| * Schreibe eine Funktion `wurzel(x)`, | * Schreibe eine Funktion `wurzel(x)`, | ||
| - | <nodisp 1> | + | <html>< |
| - | ++++Lösung:| | + | <script type=" |
| - | <html><bottom-editor> | + | def wurzel(n, precision=0.0001): |
| + | """ | ||
| + | </script> | ||
| + | <script type=" | ||
| + | assert 14.99 < wurzel(225) < 15.01 | ||
| + | assert 1.41 < wurzel(2) < 1.42 | ||
| + | </ | ||
| + | <script type=" | ||
| def wurzel(n, precision=0.0001): | def wurzel(n, precision=0.0001): | ||
| """ | """ | ||
| Zeile 387: | Zeile 459: | ||
| print(wurzel(225)) | print(wurzel(225)) | ||
| - | </ | + | </ |
| - | ++++ | + | </ |
| - | </nodisp> | + | |