"""
1~500求和(一个一个加起来)
"""
sum = 0
for i in range(1,501):
sum += i
print(sum)
"""
1-500求和(使用高斯求和的方法)
Sn=(a1+an)*n/2.
"""
sum = 0
a1 = 1
a500 = 500
"""
因为1-500是公差为1的等差数列
"""
sum = (a1 + a500) * 500/2
print(sum)
"""
1~500求和(用函数实现高斯方法的求和)
"""
def sum(a1,an,n):
sum = 0
sum = (a1+an)*n/2
print(sum)
if __name__ == '__main__':
sum(1,500,500)
"""
1~500求和(使用python内置函数numpy中的sum进行求和)
"""
import numpy as np
list = []
sum = 0
for i in range(1,501):
list.append(i)
sum = np.sum(list)
print(sum)
def decorator(func):
def infunc():
print("我要完成计算1~500求和的功能")
func()
return infunc
@decorator
#把sum作为decorator的参数传入
def sum():
sum = 0
for i in range(1, 501):
sum += i
print(sum)
sum()