Fibonacci Sequences in JavaScript with/without recursive

```        //with Recursion
function fibonacci1 (argument) {
// body...
return (argument <= 1 ? argument : fibonacci1(argument - 1) + fibonacci1(argument - 2));
}
window.console.log(fibonacci1(10));

function fibonacci2 (argument) {
return (argument <= 1 ? argument : arguments.callee(argument - 1) + arguments.callee(argument - 2));
}
window.console.log(fibonacci2(10));```

```var factorial = function (x) {
if (x == 1) {return 1;}
return x * arguments.callee(x-1);
};```

```        //without Recursion
function fibo3 (argument) {
if(argument <= 1){
return argument;
}
var fibo = 1;
var fiboPre = 1;
for (var i = 2; i < argument; ++i) {
var temp = fibo;
fibo = fibo + fiboPre;
fiboPre = temp;
}
return fibo;
}
window.console.log(fibo3(10));```

```        //with gold ratio
function fibo4 (n) {
var sqrt5 = Math.sqrt(5);
var alpha = (1+sqrt5)/2; // 黄金比率：1.618...
return Math.round(Math.pow(alpha,n) / sqrt5); // Please note that this method holds good till n = 69 only.http://www.mathsisfun.com/numbers/fibonacci-sequence.html
}
window.console.log(fibo4(3));```

103 篇文章96 人订阅

0 条评论

相关文章

D3DXCreateTextureFromFile

HRESULT D3DXCreateTextureFromFile( __in LPDIRECT3DDEVICE9 pDevice, _...

2725

780

2625

1276

593

1926

《Drools7.0.0.Final规则引擎教程》第4章 global全局变量

global 全局变量 global用来定义全局变量，它可以让应用程序的对象在规则文件中能够被访问。通常，可以用来为规则文件提供数据或服务。特别是用来操作规则执...

2426

1002

822

1986