* @param num 一共获取几个值
**/
let fibonacci = (num, arr = [1, 1]) => {
if (num < 1 || num % 1 !== 0) {
throw Error("num输入有误")
}
if (num === 1) {
return [1]
}
if (num === 2) {
return [1, 1]
}
let newNum = arr[arr.length - 1] + arr[arr.length - 2]
if (newNum > 2 ** 53 - 1) {
throw Error("已超出计算范围")
}
arr.push(newNum)
if (arr.length === num) {
return arr
}
return fibonacci(num, arr)
}
console.log(fibonacci(30))
请采纳
<script type="text/javascript"><!--
getSDF(100)
function getSDF(toNum){
var f0 = 0
var f1 = 1
var temp = 0
var huanHang = " <br> "
for(var i=1i<=toNumi++){
if(i==1){
document.write(f0 + " ")
}else if(i == 2){
document.write(f1 + " ")
}else{
temp = f1
f1 = f0 + f1
f0 = temp
document.write((f0 + f1) + " ")
}
if(i%4==0){
document.write(huanHang)
}
}
}
//-->
</script>