-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPE003.py
More file actions
82 lines (63 loc) · 1.4 KB
/
PE003.py
File metadata and controls
82 lines (63 loc) · 1.4 KB
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
82
#!/usr/bin/env python
# -*coding:UTF-8-*-
"""
@Project ProjectEuler
@File PE003.py
@Authand Haosen Luo
@Date 2024/12/8 23:34
"""
"""
最大质因数
13195的质因数包括5、7、13和29。
600851475143的最大质因数是多少?
"""
import math
import numpy as np
def get_primer_list(n):
"""
一个数的所有质数列表
:param n:
:return:
"""
# 一个数的最大因数(因子)是平方根
max_factor = int(math.sqrt(n))
# 最小的质数
base_primer = [2, 3, 5]
flag = True
for i in range(7, max_factor + 1, 2):
for j in base_primer:
if i % j == 0:
flag = False
break
if flag:
base_primer.append(i)
flag = True
return base_primer
def main(number):
"""
求解一个数的所有质因数
:param number:
:return:
"""
primers = get_primer_list(number)
primers = np.array(primers)
res = number % primers
res = primers[res == 0]
return res[-1]
def main2(number):
max_factor = int(math.sqrt(number))
x = np.array(range(2, max_factor + 1, 1))
print(x)
x = x[number % x == 0]
print(x)
while True:
if np.any(x[-1] % x[:-1] == 0):
x = x[:-1]
else:
break
return x[-1]
if __name__ == '__main__':
n1 = 13195
print(main(n1))
n2 = 600851475143
print(main2(n2))