Python :: Aufgabe #287 :: Lösung #1
3 Lösungen
#287
Teilersumme natürlicher Zahlen
Anfänger - Python
von JKooP
- 04.10.2020 um 11:49 Uhr
Schreibe eine Methode/Funktion, mit der es möglich ist zu prüfen, ob es sich bei einer natürlichen Zahl um eine defiziente,
vollkommende oder abundante Zahl handelt.
Betrachtet wird die Summe aller Teiler ohne die Zahl selbst.
Beispiel Zahl 80:
1 + 2 + 4 + 5 + 8 + 10 + 20 + 40 = 106 --> abundant, da
Summe kleiner als Zahl --> defizient
Summe gleich Zahl --> vollkommen
Summe größer als Zahl --> abundant
Viel Spaß
vollkommende oder abundante Zahl handelt.
Betrachtet wird die Summe aller Teiler ohne die Zahl selbst.
Beispiel Zahl 80:
1 + 2 + 4 + 5 + 8 + 10 + 20 + 40 = 106 --> abundant, da
Summe kleiner als Zahl --> defizient
Summe gleich Zahl --> vollkommen
Summe größer als Zahl --> abundant
Viel Spaß
#1
von Marty3000 (680 Punkte)
- 04.11.2020 um 20:46 Uhr
def get_teiler(n: int):
all_teiler = []
for i in range(2, int(n / 2) + 1):
if n / i == int(n / i):
all_teiler.append(i)
return all_teiler
def def_vol_abu(n: int):
at = get_teiler(n)
txt: str = "Zahl %d:\n 1" % n
summ: int = 1
for i in at:
summ += i
txt += " + %d" % i
txt += " = %d --> " % summ
if summ < n:
txt += "defizient\n"
if summ == n:
txt += "vollkommen\n"
if summ > n:
txt += "abundent\n"
print(txt)
def ipt():
try:
v = int(input("Bitte geben Sie eine Zahl ein: "))
except ValueError:
print('argument must be an integer')
exit(-1)
else:
return v
if __name__ == '__main__':
while True:
n = ipt()
def_vol_abu(n)
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1
