在html中 分别用递推法、一维数组和递归函数求斐波那契数列的前二十项的值
时间: 2023-12-03 12:46:47 浏览: 69
递推法:
```html
<!DOCTYPE html>
<html>
<head>
<title>Fibonacci Sequence using Recursion</title>
</head>
<body>
<h1>Fibonacci Sequence using Recursion</h1>
<script>
let n = 20;
let fib = [];
fib[0] = 0;
fib[1] = 1;
for(let i = 2; i <= n; i++){
fib[i] = fib[i-1] + fib[i-2];
}
console.log(fib);
</script>
</body>
</html>
```
一维数组:
```html
<!DOCTYPE html>
<html>
<head>
<title>Fibonacci Sequence using Recursion</title>
</head>
<body>
<h1>Fibonacci Sequence using Recursion</h1>
<script>
let n = 20;
let fib = new Array(n);
fib[0] = 0;
fib[1] = 1;
for(let i = 2; i < n; i++){
fib[i] = fib[i-1] + fib[i-2];
}
console.log(fib);
</script>
</body>
</html>
```
递归函数:
```html
<!DOCTYPE html>
<html>
<head>
<title>Fibonacci Sequence using Recursion</title>
</head>
<body>
<h1>Fibonacci Sequence using Recursion</h1>
<script>
function fibonacci(n){
if(n <= 1){
return n;
}
return fibonacci(n-1) + fibonacci(n-2);
}
let n = 20;
let fib = [];
for(let i = 0; i < n; i++){
fib.push(fibonacci(i));
}
console.log(fib);
</script>
</body>
</html>
```
阅读全文