-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPE005.py
More file actions
67 lines (48 loc) · 1.17 KB
/
PE005.py
File metadata and controls
67 lines (48 loc) · 1.17 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
#!/usr/bin/env python
# -*coding:UTF-8-*-
"""
@Project hello_world
@File PE005.py
@Author Haosen Luo
@Date 2024/12/22 22:08
最小公倍数
2520是最小的能够被1到10整除的正数。
最小的能够被1到20整除的正数是多少?
"""
import time
def timer(func):
def inner_func(*args, **kwargs):
begin_time = time.time()
func(*args, **kwargs)
print(f"Running {time.time() - begin_time} s")
return inner_func
def gcd(a, b):
"""计算最大公约数"""
while b:
a, b = b, a % b
return a
def lcm(a, b):
"""计算最小公倍数"""
return a * b // gcd(a, b)
@timer
def lcm_of_range(n):
"""计算1到n的最小公倍数"""
result = 1
for i in range(1, n + 1):
result = lcm(result, i)
print(f"The least common multiple of numbers from 1 to 20 is: {result}")
@timer
def main():
base_num = 2520
switch = True
while switch:
for j in range(1, 21, 1):
if base_num % j != 0:
base_num += 1
break
if j == 20:
switch = False
print(base_num)
if __name__ == '__main__':
lcm_of_range(20)
main()