I have a code to calculate highest prime factor of 600851475143.
def PRIME(a): #Check if no is prime
f = 0
i = 2
while(i < a/2): #No factor of a no can be greater than a/2
if (a % i == 0):
f = 1
break
i = i + 1
if(f == 1):
return 0
else:
return 1
def PFIND(a):
for i in range(1, 100000): #Iteratively check if the no is prime
if PRIME(a/2 - i): #No factor of a no can be greater than a/2.
if (a % (a/2 - i) == 0):
return (a/2 - i)
print PFIND(600851475143)
But the code runs on and on and on and does’nt give any output.
Python’s support for large integers is fine. I think your problem is that you’re doing very slow algorithms for both brute-force finding the factor and testing whether your factor is prime. If you just invert the order of those two tests (ie, test whether it’s a factor, then test whether it’s prime), it’ll go a lot faster.
But perhaps the problem was about using more sophisticated algorithms. Maybe you should use the Rabin-Miller test instead of brute-force.