cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              `strict mode`
function fib(n) {
    if (n <= 2) {
        return 1;
    }
    return fib(n - 1) + fib(n - 2);
}

function fib2(n) {
    var n_1 = 1;
    var n_2 = 2;
    var tmp;
    while (n > 2) {
        tmp = n_2;
        n_2 = n_2 + n_1;
        n_1 = tmp;
        n = n - 1;

    }
    return n_1;
}

function fib3(n){
  function rec(n, n_1, n_2){
    return n < 2 ? n_1 : rec(n-1, n_2, n_1+n_2)
  }
  return rec(n, 1, 1); 
}



function bench(n) {
    var start;
    var stop;
    var result;
    
    
    start = (new Date()).getTime();
    result = fib(n);
    stop = (new Date()).getTime();
    
    document.writeln("Recurcive version (fib("+n+")="+result+"): " + (stop - start) + ' milliseconds<br>');
    
    
    
    start = (new Date()).getTime();
    result = fib2(n);
    stop = (new Date()).getTime();
    document.writeln("Iterative version (fib("+n+")="+result+"): " + (stop - start) + " milliseconds<br>");

    start = (new Date()).getTime();
    result = fib3(n);
    stop = (new Date()).getTime();
    document.writeln("Proper tail  recursion (fib("+n+")="+result+"): " + (stop - start) + " milliseconds");

}
bench(30);

            
          
!
999px
Loading ..................

Console