累乘专题

torch.cumprod实现累乘计算

cumprod取自“cumulative product”的缩写,即“累计乘法”。 数学公式为: y i = x 1 × x 2 × x 3 × . . . × x i y_i=x_1\times{x_2}\times{x_3}\times{...}\times{x_i} yi​=x1​×x2​×x3​×...×xi​ 官方链接:torch.cumprod 用法: import torch

python累乘相加求1 + 2! + 3! + 4! + ......20!

s = 1total = 0for i in range(1, 21):s*= itotal += sprint(total) 方法二: def f(n):s = 1for i in range(1, n + 1):s *= ireturn stotal = sum(map(f, range(1, 21)))print(total)