Created
December 30, 2022 13:53
-
-
Save Friedjof/a252c532bcf027f7d95ef55562ef6364 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Zahl: | |
| def __init__(self, n) -> None: | |
| self.n = n | |
| def teiler(self) -> iter: | |
| for i in range(1, self.n): | |
| if self.n % i == 0: | |
| yield i | |
| def ist_prim(self) -> bool: | |
| return len(list(self.teiler())) == 1 | |
| def primfaktoren(self) -> iter: | |
| teiler = self.teiler() | |
| for i in teiler: | |
| if Zahl(i).ist_prim(): | |
| yield i | |
| def ggT(self, zahl: "Zahl") -> int: | |
| if zahl == 0: | |
| return self.n | |
| else: | |
| return Zahl(zahl).ggT(self % zahl) | |
| def teilerfremd(self, zahl: "Zahl") -> bool: | |
| return self.ggT(zahl) == 1 | |
| def phi(self) -> "Zahl": | |
| return Zahl(len(list(filter(lambda x: self.teilerfremd(x), range(1, self.n))))) | |
| def mul_inv(self, zahl: "Zahl") -> "Zahl": | |
| for i in range(1, self.n): | |
| if (i * zahl.n) % self.n == 1: | |
| return Zahl(i) | |
| def euler(self, n: "Zahl", k: "Zahl") -> "Zahl": | |
| return self ** (k * n.phi() + 1) | |
| def __eq__(self, zahl: "Zahl" or int) -> bool: | |
| if type(zahl) == Zahl: | |
| return self.n == zahl.n | |
| else: | |
| return self.n == zahl | |
| def __int__(self) -> int: | |
| return self.n | |
| def __mod__(self, other: "Zahl" or int) -> "Zahl": | |
| if type(other) == Zahl: | |
| return Zahl(self.n % other.n) | |
| else: | |
| return Zahl(self.n % other) | |
| def __pow__(self, other: "Zahl" or int) -> "Zahl": | |
| if type(other) == Zahl: | |
| return Zahl(self.n ** other.n) | |
| else: | |
| return Zahl(self.n ** other) | |
| def __mul__(self, other: "Zahl" or int) -> "Zahl": | |
| if type(other) == Zahl: | |
| return Zahl(self.n * other.n) | |
| else: | |
| return Zahl(self.n * other) | |
| def __add__(self, other: "Zahl" or int) -> "Zahl": | |
| if type(other) == Zahl: | |
| return Zahl(self.n + other.n) | |
| else: | |
| return Zahl(self.n + other) | |
| def __str__(self) -> str: | |
| return str(self.n) | |
| if __name__ == '__main__': | |
| # z = Zahl(99942371278312816472746526754385475176345175643478264726) | |
| z = Zahl(7) | |
| print(f"Ist {z} eine Primzahl? {z.ist_prim()}") | |
| print(f"Primfaktoren von {z}: {tuple(z.primfaktoren())}") | |
| print(f"Phi von {z}: {z.phi()}") | |
| print(f"ggT von {z} und 5: {z.ggT(Zahl(5))}") | |
| print(f"Sind {z} und 5 teilerfremd? {z.teilerfremd(Zahl(5))}") | |
| print(f"{Zahl(5 * 4).phi()} == {Zahl(5).phi() * Zahl(4).phi()}") | |
| print(f"Mul_inv von 5 und 6: {Zahl(5).mul_inv(Zahl(6))}") | |
| print(f"Mul_inv von 2 und 6: {Zahl(2).mul_inv(Zahl(6))}") | |
| print(f"Euler von 3, 35 und 2: {Zahl(3).euler(Zahl(35), Zahl(2))}") |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Zahl
Die Klasse
Zahlist eine Hülle für einen Integern.Von dieser Zahl können so verschiedene Eigenschaften abgefragt werden.
Methoden
teiler(): Gibt alle Teiler der Zahl zurückist_prim(): Gibt zurück, ob die Zahl eine Primzahl istprimfaktoren(): Gibt alle Primfaktoren der Zahl zurückggT(): Gibt den größten gemeinsamen Teiler der Zahl und einer anderen Zahl zurückteilerfremd(): Gibt zurück, ob die Zahl und eine andere Zahl teilerfremd sindphi(): Gibt die Euler-Funktion der Zahl zurückmul_inv(): Gibt das Multiplikative Inverse der Zahl und einer anderen Zahl zurückeuler(): Gibt das Ergebnis der Euler-Funktion der Zahl, einer anderen Zahl und einer dritten Zahl zurückOperatoren
==: Gibt zurück, ob die Zahl gleich einer anderen Zahl ist%: Gibt den Rest der Division der Zahl und einer anderen Zahl zurück**: Gibt das Ergebnis der Potenzierung der Zahl und einer anderen Zahl zurück*: Gibt das Ergebnis der Multiplikation der Zahl und einer anderen Zahl zurück+: Gibt das Ergebnis der Addition der Zahl und einer anderen Zahl zurückstr(): Gibt die Zahl als String zurückQuelle