Admin DueLLoYöneticisi
Mesaj Sayısı : 928 Doğum tarihi : 06/07/90 Yaşı : 34 Nerden : Antalya İş/Hobiler : Webtasarım - Öğrenci Lakap : Site sahibi Rep Puanı : 735008
| Konu: improved fibonacci sequence (ing) Cuma Ara. 04, 2009 8:08 pm | |
| - Kod:
-
<script type="text/javascript"> /* This script and many more are available free online at The JavaScript Source!! http://javascript.internet.com Created by: Ben Clarke | http://benclarke.ca/ */
var i=0; var fibs = []; // lookup table so it doesn't take forever function fib(a) { var x, y, z; switch(a) { case 0: case 1: x = a; break; default: if(!fibs[a - 1]) { fibs[a - 1] = fib(a - 1); } if(!fibs[a - 2]) { fibs[a - 2] = fib(a - 2); } y = fibs[a - 1]; z = fibs[a - 2]; x = y + z; } return x; }
function doFib() { var n = parseInt(document.getElementById("n").value); if(isNaN(n) || n < 1) { alert("Please enter a positive number for computations"); return; } var a; var r = document.getElementById("r"); r.value = ""; for(var i=0; i<n; i++) { a = fib(i); r.value += " " + a; } } </script>
<p> Computations: <input type="text" id="n"/></p> <p> <input type="text" id="r" style="width: 500px;"/></p> <p> <input type="button" onclick="doFib();" value="Calculate"/></p>
| |
|