斐波那契数列<递归和排序>
作者:互联网
算法(斐波那契数列两种方法分析)
// 递归方法 let data1 = new Date(); function fib(n) { if (n === 1 || n === 2) return n - 1 return fib(n - 1) + fib(n - 2) } let arr = [] for (let index = 1; index < 10 ; index++) { arr.push(fib(index)) } let data2 = new Date(); console.log(arr, data2 - data1); //非递归方法 let data3 = new Date(); function fib1(n) { if (n == 1) { return 0 } let a = 0 let b = 1 let c = a + b for (let i = 3; i < n; i++) { a = b b = c c = a + b } return c } let arr1 = [] for (let index = 1; index < 10; index++) { arr1.push(fib1(index)) } let data4 = new Date(); console.log(arr1, data4 - data3);
标签:index,return,数列,斐波,let,fib,Date,new,那契 来源: https://www.cnblogs.com/sjfeng/p/16164882.html