whazzup,
Having the following problem, I can't get it fixed. Handling with numbers having a length around 5 - 52, I wan't to get their prime factors. Using Python, I found the following two algorithms:
I.
def faktorisiere(n):
l = [] # Lösungsmenge
# Auf Teilbarkeit durch 2, und alle ungeraden Zahlen von 3..n/2 testen
for i in chain([2], range(3, n//2 + 1, 2)):
# Ein Teiler kann mehrfach vorkommen (z.B. 4 = 2 * 2), deswegen:
while n % i == 0:
l.append(i)
print(i)
n = n // i # "//" ist ganzzahlige Division und entspricht int(n/i)
if i > n: # Alle Teiler gefunden? Dann Abbruch.
break
print(l)
II.
x = input("Number: ")
mode = x[-1:]
x = int(x[:-1])
if int(x) < 1000000000000:
faktorisiere(x)
else:
if mode == 'f':
faktorisiere(x)
rx = int(sqrt(x)) + 1
for i in range(1, rx+1):
if mode == 'a':
if x % i == 0:
y = int(x/i)
print(str(x), "=", i, "*", str(y))
if mode == 'u':
if i % 2 != 0:
if x % i == 0:
y = int(x/i)
print(str(x), "=", i, "*", str(y))
But the first code is very slow computing numbers like these: 1917141215419419171412154194191714
The second is working a little faster, but I get wrong outputs and I can't find the mistake in code. As an example we take the given number. These are the first lines of pythons output:
1917141215419419171412154194191714 = 1 * 1917141215419419143900621852114944
1917141215419419171412154194191714 = 2 * 958570607709709571950310926057472
- 1917141215419419171412154194191714 = 3 * 639047071806473023947675938062336
But as you can see, these multiplications don't equal the result. Is there any mistake in the code? Or is it just because of the length of numbers? Hope you can help me.
Best wishes, TimeMen

y = x//iinstead ofint(x/y)as you did in algo I.