forked from liyang1234567890/lyproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.3.3.html
44 lines (40 loc) · 1.12 KB
/
4.3.3.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!-- 3.编写程序,计算10!(即 1*2*3*·…10)的结果(10的阶乘)。 -->
<script>
//递归
//10! = 10 * 9!;
//9! = 9 * 8!;
//n! = n * (n-1)! ;
function fac(num){
if( num == 1){
return 1;
}
// return num * fac(num - 1);
//.calee 是对象。方法的意思
return num * arguments.callee(num - 1);
}
console.log(fac(10));
//非递归
function fac2(num){
var sum = 1;
for(var i = num; i > 0; i--){
sum *= i;
}
return sum;
}
console.log(fac2(10));
// 4.编写程序,计算 1!+2!+3!+…..+10!的结果。
var sum = 0;
for(var i = 1; i<=10; i++){
sum += fac(i);
}
console.log(sum);
</script>
</body>
</html>