# recursion count

{% tabs %}
{% tab title="💾 程式" %}
:floppy\_disk: replit: [recursion count](https://replit.com/@pegasusroe/recursion-count#index.js)

```javascript
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));
```

![](https://2527454625-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfvEFZnSBhKT6fJmus0%2Fuploads%2FdMHiuiftdakLDWazgUqO%2FE88FA4CD-F24C-46A0-BA3C-1E30A13DF77F.jpeg?alt=media\&token=330c63cd-a152-4add-9272-de992760cdb8)
{% endtab %}

{% tab title="📗 參考" %}
* wolframalpha: [a(n)=a(n-1)+a(n-2)+1, a(-1)=1, a(0)=1](https://www.wolframalpha.com/input?i=a%28n%29%3Da%28n-1%29%2Ba%28n-2%29%2B1%2C+a%28-1%29%3D1%2C+a%280%29%3D1)
{% endtab %}
{% endtabs %}
