return page history
α-wwwiki
::
history/fibonacci/20130909-104607.txt
editor : alpha [83.158.157.173] 2013/09/09 10:46:07 _h1 fibonacci _p This page shows how to compute and to display the first N numbers of [[Fibonacci|http://en.wikipedia.org/wiki/Fibonacci_number]]. This can be done directly in the page's code, via a mini-console integrated in the page or in an external Lisp console. _h3 1) using the define operator _p The lambdatalk {b define} operator allows to define user functions directly in the wiki page's code. _h5 a) code {pre °° {define fibonacci (m) function fibo(n) { return (function fib(a,b,i) { return (i==0)? b : fib(a+b,a,i-1) })(1,0,n) } return m + ': ' + fibo(m) + '\n'; } {pre fibonacci numbers from 0 to 12 : {map fibonacci {serie 0 12}} } °°} _p The display is done via lambdatalk built-in operators. The alternate {b defun} operator using lambdatalk syntax can't be used here, because this operator can't define recursive functions. So we must work with the {b define} operator using a JS syntax. A loss in elegance but a gain in speed ! _h5 b) result {div {@ style="display:none;"} {define fibonacci (m) function fibo(n) { return (function fib(a,b,i) { return (i==0)? b : fib(a+b,a,i-1) })(1,0,n) } return m + ': ' + fibo(m) + '\n'; } } {pre fibonacci numbers from 0 to 12 : {map fibonacci {serie 0 12}} } _p Note that the page's code must be edited to modify the value of N. _h3 2) using the input operator _p Sometimes it can be better to build a mini-console in order to test different values without editing the pages's code : the {b input} operator is adapted for this purpose. _h5 a) code _p The interface written in the wiki page is made of three parts, the input, the output and a button. _ul 1) The number N will be entered in an input field made with an {b input} operator : {pre °° {input {@ id="input" value="" placeHolder="enter a number"}} °°} _ul 2) the result will be displayed in a HTML {b pre}(format) container : {pre °° {pre {@ id="output"}} °°} _ul 3) the JS code will be included in an {b input} operator via an {b onclick} attribute : {pre °°{input {@ type="submit" value="fibonacci" onclick="•• function fibs(n) { function fibonacci(n) { return (function fib(a,b,i) { return (i==0)? b : fib(a+b,a,i-1) })(1,0,n) } var t0 = new Date().getTime(); for (var r ='', i=0; i< n; i++) { r += '\n' + i + ': ' + fibonacci(i); } var t1 = new Date().getTime(); return '\ntime = ' + (t1-t0) + 'ms\n' + r; } getId('output').innerHTML = fibs(getId('input').value) ••"}} °°} _h5 b) result _p These are the three elements ready for test any values of N : {center {input {@ id="input" value="" placeHolder="enter a number"}} {input {@ type="submit" value="fibonacci" onclick="°° function fibs(n) { function fibonacci(n) { return (function fib(a,b,i) { return (i==0)? b : fib(a+b,a,i-1) })(1,0,n) } var t0 = new Date().getTime(); for (var r ='', i=0; i< n; i++) { r += '\n' + i + ': ' + fibonacci(i); } var t1 = new Date().getTime(); return '\ntime = ' + (t1-t0) + 'ms\n' + r; } getId('output').innerHTML = fibs(getId('input').value) °°"}} } {pre {@ id="output"}} _h3 3) using an external console _p The Fibonacci sequence can be computed using an outside Lisp console, see for instance [[monodicolisp|data/monodicolisp/]] or [[alphalisp|data/alphalisp/]] and test this : {pre (define fast_fib (lambda (n) (begin (define ifib (lambda (n a b) (if (= n 0) b (if (= n 1) a (ifib (- n 1) (+ a b) a) ) ) ) ) (ifib n 1 0) ) ) ) > [fast_fib] (fast_fib 12) > 144 // time : 1ms (fast_fib 123) > 2.269837405200687e+25 // time : 17ms (fast_fib 1234) > 3.4774673918037014e+257 // time : 127ms }