I'm trying to learn Python by working my way through problems on the Project Euler website. I managed to solve problem #3, which is
What is the largest prime factor of the number 600851475143 ?
However, my code looks horrible. I'd really appreciate some advice from experienced Python programmers.
import math import unittest def largestPrime(n, factor): """ Find the largest prime factor of n """ def divide(m, factor): """ Divide an integer by a factor as many times as possible """ if m % factor is 0 : return divide(m / factor, factor) else : return m def isPrime(m): """ Determine whether an integer is prime """ if m is 2 : return True else : for i in range(2, int(math.floor(math.sqrt(m))) + 1): if m % i is 0 : return False return True # Because there is at most one prime factor greater than sqrt(n), # make this an upper limit for the search limit = math.floor(math.sqrt(n)) if factor <= int(limit): if isPrime(factor) and n % factor is 0: n = divide(n, factor) if n is 1: return factor else: return largestPrime(n, factor + 2) else: return largestPrime(n, factor + 2) return n