'''
Created on 2014年8月23日
Largest prime factor
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
@author: wxp2971
'''
from numpy import *
# 直接求解,寻找最大的那个
N = 600851475143
factor = 2
lastFactor = 1
while N > 1:
if N % factor == 0:
lastFactor = factor
while N % factor == 0:
N = N / factor
factor += 1
print(lastFactor)
# 对特殊的质数进行筛选
N = 600851475143
if N % 2 == 0 :
N = N / 2
lastFactor = 2
while N % 2 == 0:
N = N / 2
else:
lastFactor = 1
factor = 3
while N > 1:
if N % factor == 0:
N = N / factor
lastFactor = factor
while N % factor == 0:
N = N / factor
factor += 2
print(lastFactor)
# 质因子的最大不会超过sqrt(N)
N = 600851475143
if N % 2 == 0 :
N = N / 2
lastFactor = 2
while N % 2 == 0:
N = N / 2
else:
lastFactor = 1
factor = 3
maxFactor = sqrt(N)
while N > 1 and factor <= maxFactor:
if N % factor == 0:
N = N / factor
lastFactor = factor
while N % factor == 0:
N = N / factor
maxFactor = sqrt(N)
factor += 2
if N == 1:
print(lastFactor)
else:
print(int(N))
Project Euler 3
最新推荐文章于 2020-02-16 23:38:29 发布