matlab求阶乘的和1~20
时间: 2023-05-29 13:06:02 浏览: 1414
求阶乘的和
可以使用循环和递归两种方式来实现求阶乘的和:
1. 循环方式:
```
sum = 0;
for i = 1:20
factorial = 1;
for j = 1:i
factorial = factorial * j;
end
sum = sum + factorial;
end
disp(sum);
```
2. 递归方式:
```
function f = factorial(n)
if n == 0
f = 1;
else
f = n * factorial(n-1);
end
sum = 0;
for i = 1:20
sum = sum + factorial(i);
end
disp(sum);
```
阅读全文