User Tools

Site Tools


λ Fibonacci rounding

======= Algorithm ======= <syntax js> function fib(n) { var sqrt5 = Math.sqrt(5); var phi = (sqrt5+1)/2; return Math.round(Math.pow(phi, n)/sqrt5); } </syntax> ======= Support ======= <syntax js> function run(args) { delete result; result = fib(args.n); } </syntax> ======= Tests ======= <syntax js> function testTen() { assert (fib(10) === 55, "fib(10)"); } function testEighteen() { assert (fib(18) === 2584 , "fib(18)"); }</syntax> ======= Options ======= <syntax js> { "title":"Fibonacci rounding", "height":"150px" } </syntax> ======= Visualisation ======= <syntax html> <html> <head> <style type="text/css"> input {width:50px;} </style> <script type="text/javascript"> function update(n, x) { var element = document.getElementById("result"); var result = x.lookupInScope("result"); var n = x.lookupInScope("n"); if (typeof result != "undefined") { element.innerHTML = "= "+result; } else if (n) { element.innerHTML = "Calculating fib("+n+")"; } else { element.innerHTML = ""; } } function args() { var n = document.getElementById('n').value; return {n:n}; } </script> </head> <body> fib( <input id="n" value=12> ) <div id="result"></div> </body> </html> </syntax>

algorithm/fibonacci-rounding.txt · Last modified: 2015/02/02 08:28 (external edit)