编程计算前30项的和:s=1+(1+2)+(1+2+3)+(1+2+3+4)+…+(1+2+3+4+…+n),用函数调用的方法python
时间: 2023-06-08 11:05:03 浏览: 401
python 求1+2!+3!+...+20!的和 (示例)
5星 · 资源好评率100%
以下是Python代码,可以计算前30项的和:
def sum_n(n):
sum = 0
for i in range(1, n+1):
sublist = [j for j in range(1, i+1)]
product = 1
for num in sublist:
product *= num
sum += product
return sum
print(sum_n(30)) # 输出答案
阅读全文