2010-05-04 18:21:07 +00:00
|
|
|
"""Find the largest prime factor of a composite number.
|
|
|
|
|
|
|
|
The prime factors of 13195 are 5, 7, 13 and 29.
|
|
|
|
What is the largest prime factor of the number 600851475143 ?
|
|
|
|
"""
|
|
|
|
|
2010-04-12 15:53:13 +00:00
|
|
|
def pfactor(n):
|
2010-04-12 15:53:00 +00:00
|
|
|
i = 2
|
|
|
|
while (n % i != 0 and i < n):
|
|
|
|
i = i + 1
|
|
|
|
if i == n:
|
|
|
|
return [n]
|
2010-04-12 15:53:13 +00:00
|
|
|
p = pfactor(n // i)
|
2010-04-12 15:53:00 +00:00
|
|
|
p.append(i)
|
|
|
|
return sorted(p, reverse=True)
|
|
|
|
|
2010-05-04 18:21:07 +00:00
|
|
|
def main():
|
2010-04-12 15:53:14 +00:00
|
|
|
print 'Prime factors of 600851475143:'
|
|
|
|
print pfactor(600851475143)
|
2010-05-04 18:21:07 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|