-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPE006.py
More file actions
45 lines (32 loc) · 962 Bytes
/
PE006.py
File metadata and controls
45 lines (32 loc) · 962 Bytes
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
#!/usr/bin/env python
# -*coding:UTF-8-*-
"""
@Project hello_world
@File PE006.py
@Author Haosen Luo
@Date 2024/12/29 15:14
平方和与和平方之差
前十个自然数的平方的和是
1^2 + 2^2 + ... + 10^2 = 385
前十个自然数的和的平方是
(1+2+ ..+10)^2 = 55^2 = 3025
因此,前十个自然数的平方和与和平方之差是 3025 − 385 = 2640。
求前一百个自然数的平方的与和平方之差。
译注:注意此处出题人使用的自然数定义不包含0
"""
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
@timer
def main(n):
square_of_sum = sum(i ** 2 for i in range(1, n + 1))
sum_of_square = sum(i for i in range(1, n + 1)) ** 2
res = abs(square_of_sum - sum_of_square)
print(res)
if __name__ == '__main__':
main(10)
main(100)