recursion count

💾 replit: recursion count

function recursion(n, level = 0) {
    
    recursion.count += 1;
    const tab = '\t'.repeat(level);
    console.log(`${tab}n=${n}, count=${recursion.count}`);
    
    if (n < 1) return;
    recursion(n - 1, level + 1);
    recursion(n - 2, level + 1);
}

// a(n): recursion count
// a(-1) = 1, a(0) = 1,
// a(n) = a(n-1) + a(n-2) + 1 , n>=1
//    n: -1 0 1 2 3  4  5
// -----------------------
// a(n):  1 1 3 5 9 15 25
recursion.count = 0;
recursion(5);

;[
    recursion.count,        // 25

].forEach(x => console.log(x));

Last updated