Monday 15 April 2013

Fibonacci



Fibonacci just blows my mind

Grey Wolf is amazing and helped me greatly with the following HTML + Javascript to test fibonacci principals based on a couple khanacademy video’s

Caution entering a value of 45 and above may cause serious lag or even crash issues

<body>
<h1> Fibonacci </h1>
<br><br>
<form>
<table>
<tr>
<th style=”text-align:center”>Enter Number</th>
</tr>
<tr>
<td><input type=”text” id=”fn” autocomplete=”off”/></td>
</tr>
<tr>
<td align=”center”><input type=”button” value=”Fibonacciate” onclick=”runFib();”/></td>
</tr>
</table>
</form>
<br><br>
<form>
<table>
<tr>
<th style=”text-align:center”>Fibonacci Jumps</th>
</tr>
<tr>
<td><input id=”fibonacci” readonly=”readonly”/></td>
</tr>
</table>
</form>
</body>

<script>
//Fibonacci function
fibonacci = function(fib) {
if (fib<=1) return fib;
else return fibonacci(fib-1) + fibonacci(fib-2);
}
//Run fibonacci function
runFib = function() {
var fib = document.getElementById(“fn”).value;
var v = fibonacci(fib);
document.getElementById(“fibonacci”).value=v;
}
</script>

Original Post Aug 15th 2012

No comments:

Post a Comment