Problem 3:Largest prime factor

原题地址:http://projecteuler.net/problem=3

Largest prime factor

Problem 3

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

What is the largest prime factor of the number 600851475143 ?


大意是:

13195的质数因子有5,7,13和29.

600851475143的最大质数因子是多少?


解法1:

1.进行因数分解

2.筛选出质因数

3.取最大质因数

python代码如下所示:

import math
maxnum = 600851475143L
factorlist = []
prifactorlist = []

def isPrime(num):
    num = int(num) 
    temp = math.floor(math.sqrt(num))+1
    
    while temp > 1:
         if num%temp == 0:
             print temp
             return False
         temp -= 1
    else:
         return True

def getFactors(num):
    temp = 2
    while temp < num:
        if num%temp == 0:
            factorlist.append(temp)
            num /= temp
            temp = 2
        temp += 1
    factorlist.append(num)
    
def maxPrime(num):
    for x in factorlist:
        prifactorlist.append(x)

    return max(prifactorlist)
        
if __name__ == '__main__':
   getFactors(maxnum)
   #print max(factorlist)
   print 'max prime factor of %d is:%d' % (maxnum,maxPrime(maxnum))     
  




注:题目的中文翻译源自http://pe.spiritzhang.com


你可能感兴趣的:(python,projecteuler)