How many recursive calls does it take to compute binomial probability weights? I was given an algorithm and I was asked to estimate how often it would be called if I was trying to calculate ${100 \choose 50}0,25^{50}0.75^{50}$, the Binomial distribution of $50$ elements chosen within $100$ elements with $0.25$ their probability to appear.

This is the method:

binomial1(int N, int k, double p) {
    if (N == 0 &amp;&amp; k == 0) return 1.0;
    if (N &lt; 0 || k &lt; 0) return 0.0;

    return (1.0 - p) * binomial1(N-1, k, p) 
         + p * binomial1(N-1, k-1, p);  
}


The question is, how many calls to binomial1 would be required to evaluate binomial1(100, 50, 0.25)?

I tried first the fool strategy to implements a system.out.println within the code with an indice increasing each time the algorithm was called. But I realised that it was something fool when I noticed I reached:

passed 2485266 times


Thus I was wondering that as far that the algorithm is called twice every time it is called, I was wondering if it was simply called $2^{100}$ times? Yet, p*binomial1(N-1, k-1, p) seemed to be called only $50$ times with a parameter $k=50$...

I'm only asking for a hint.
If we let $T(n,k)$ represent the number of times that binomial1 is called when computing binomial1(n, k, p) we'll have $T(n,k) = T(n-1, k) + T(n-1, k-1)+1$ and $T(-1, k) = T(k, -1) = T(0,0)=1$. The closed form expression for $T(n,k)$ is a bit messy, but fortunately we don't need it.

Note that the binomial coefficients $\binom{n}{k}$ are defined by the nearly similar recurrence $\binom{n-1}{k} + \binom{n-1}{k-1}$ so we'll have $T(n,k)\ge \binom{n}{k}$ and in particular $T(100, 50)\ge \binom{100}{50}$ and by Stirling's approximation we'll have $T(100,50)\approx 2^{100}/\sqrt{50\pi}$, which is pretty much what you guessed.

[In fact, it turns out that $T(n,k)$ is quite a bit larger than $\binom{n}{k}$.]