Thứ Bảy, 16 tháng 4, 2016

[#3] Largest prime factor

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

def problem3(n):
        '''
        The prime factors of 13195 are 5, 7, 13 and 29.
        What is the largest prime factor of the number n ?
        '''
        result = 0 #store the largest prime
        #All primes are odd except 2
        if n % 2 == 0:
            result = 2
            n /= 2
        i = 3
        while n != 1:
            if n % i == 0:
                result = i
                n /= i
            i += 2
        return result

ChiefdaThief

Author & Editor

Keep It Simple Stupid.

0 nhận xét:

Đăng nhận xét

Manual Categories