Primfaktorenzerlegung und Berechnung des GGT zweier Zahlen
Verfasst: Freitag 12. Februar 2010, 22:37
Hallo zusammen!
Anbei eines meiner "Erstlingswerke"! Ich würde mich über eure Komentare, welcher Art auch immer, freuen.
LG RonnyO.
Anbei eines meiner "Erstlingswerke"! Ich würde mich über eure Komentare, welcher Art auch immer, freuen.
Code: Alles auswählen
# -*- coding: utf-8 -*-
def calc_primenumbers(x):
primes = []
for i in range(2, x + 1):
isPrime = True
for ii in range(2, i):
if i % ii == 0:
isPrime = False
if isPrime:
primes.append(i)
return primes
def calc_primefactors(x):
primeNumbers = calc_primeNumbers(x)
primeFactors = []
for i in primeNumbers:
while x % i == 0:
x = x / i
primeFactors.append(i)
return primeFactors
def calc_greatescommondivider(x,y):
greatestCommonDivider = 1
commonPrimeFactors = []
xPrimeFactors = calc_primefactors(x)
yPrimeFactors = calc_primefactors(y)
for xPrimeFactor in xPrimeFactors:
if xPrimeFactor in yPrimeFactors:
commonPrimeFactors.append(xPrimeFactor)
yPrimeFactors.pop(0)
for commonPrimeFactor in commonPrimeFactors:
greatestCommonDivider *= commonPrimeFactor
return greatestCommonDivider
if __name__ == '__main__':
while 1:
x = input("Eingabe 1. Zahl: ")
y = input("Eingabe 2. Zahl: ")
xPrimeFactors = calc_primefactors(x)
yPrimeFactors = calc_primefactors(y)
print "Die Primfaktoren der Zahl", y, "sind",yPrimeFactors
print "Die Primfaktoren der Zahl", x, "sind", xPrimeFactors
print "Der GGT der Zahlen %s und %s ist %s" %(x, y, calc_greatesCommonDivider(x, y))
print ""