-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPE001.py
More file actions
59 lines (45 loc) · 1.19 KB
/
PE001.py
File metadata and controls
59 lines (45 loc) · 1.19 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
#!/usr/bin/env python
# -*coding:UTF-8-*-
"""
@Project PE
@File PE001.py
@Author Haosen Luo
@Date 2024/12/8 22:09
"""
"""
3或5的倍数
在小于10的自然数中,3或5的倍数有3、5、6和9,这些数之和是23。
求小于1000的自然数中所有3或5的倍数之和。
译注:自然数是否包含0目前仍有争议,出题人应当是选择了不包含0的定义,但所幸本题的结采并
不受此定义影响。
"""
import time
def timer(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
print(f"{func.__name__} took {end_time - start_time:.12f} seconds to execute.")
return result
return wrapper
@timer
def method_1(n):
result = sum(i for i in range(1, n + 1) if i % 3 == 0 or i % 5 == 0)
print(result)
@timer
def method_2(n):
result = 0
while n:
if n % 3 == 0 or n % 5 == 0:
result += n
n -= 1
print(result)
if __name__ == '__main__':
method_1(1000)
method_2(1000)
"""
234168
method_1 took 0.000000000000 seconds to execute.
234168
method_2 took 0.000000000000 seconds to execute.
"""