Largest prime factor in Python

This is the problem number four in the Project Euler which reads:

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

My own solution to resolve this problem is the following:

def primeFactors(n):
	factors=[]
	d=2
	while(d*d<=n):
		while(n>1):            
			while n%d==0:
				factors.append(d)
				n=n/d
			d+=1
	return factors[-1]
	
print primeFactors(600851475143)

And the correct answer to this problem is: 6857

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.