-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp_003_largest_prime_factor.py
81 lines (66 loc) · 1.5 KB
/
p_003_largest_prime_factor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
__author__ = 'Xidai'
"""
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
"""
from math import sqrt
def is_prime(num):
if num <= 3:
if num <= 1:
return False
return True
if not num % 2 or not num % 3:
return False
for i in range(5, int(num**0.5) + 1, 6):
if not num % i or not num % (i + 2):
return False
return True
def largest_prime_factor(n):
divisor = 2
while n > 1:
if n % divisor == 0 and is_prime(divisor):
n /= divisor
divisor -= 1
divisor += 1
return divisor
def cheat(n):
i = 2
test = 0
while n > i:
while n % i == 0:
n /= i
i += 1
if i * i > n > 1:
print n
test = 1
break
if test == 0:
print i - 1
def cheat2():
n = 600851475143
divisor = 2
count = 0
while n > 1:
count += 1
print "n=", n, "divisor=", divisor
if n % divisor == 0:
n /= divisor
divisor -= 1
divisor += 1
print "count=", count
return divisor
def cheat3():
roots = []
product = 1
x = 2
number = input("number?: ")
y = number
while product != number:
while y % x == 0:
roots.append(x)
y /= x
product *= roots[-1]
x += 1
print roots
if __name__ == "__main__":
print largest_prime_factor(600851475143)