Q:

How would I derive the closed form for this?

In a simple martingale betting strategy you double your bet each time you lose until you win. Let's say you use a martingale strategy in a series of coin flip bets. Let $X$ be a random variable that denotes the number of flips to win. Let $p$ be the probability of a heads. Let $M$ be the payout of the strategy assuming an infinite bankroll and possible trials. What is the $var(M)$ as a function of $p$ with an initial bet of 1?
$P$(heads)$ = p$
$P$(tails)$ = 1-p$
$E[X] = $ expected flips to win
$M = $ fpayout($X$)
$E[M] =  E$[fpayout($X$)]
Since $X$ is a geometric variable I know $E[X] = 1/p$. Rather than calculating $E[M]$ directly I've reasoned fpayout($X) = 2^{X-1}$, since you can expect $E[X]$ flips and you can't lose (infinite bankroll) the expected payout should be $E[M] = $ fpayout$(E[X]) = 2^{1/p - 1}$ or is that a bad assumption?
Calculating $E[M]$ directly would look something like this:
$$E[M] = \sum_{x=0}^\infty (2^{x})  \cdot p \cdot (1-p)^{x}$$
But I couldn't get the above closed form from this... so moving on.
The variance is defined as 
$$var(M) = E[M^2] - E[M]^2 $$
or 
$$var(M) = \sum_{x=0}^\infty (M_x - \mu)^2 \cdot p \cdot (1-p)^{x}  $$
I tried both approaches but only had success with the second one (how would you do it?):
$$var(M) = \sum_{x=0}^\infty (2^x - 2^{1/p}) ^2 \cdot p \cdot (1-p)^x$$
And derived this messy closed form by expanding the quadratic:
$$var(M) = p \cdot \left(\frac{1}{4p - 3} + \frac{2^{\frac{1}{p}}}{2p -1} + \frac{2^{\frac{2}{p} - 2}}{p}\right)$$
But I get some divide by zeros here and some wrong values e.g. $p = 1$ (which should $= 0$) so I must have made some errors.
And I'm not sure why the simpler approach of summing the expected payout and probabilities doesn't work: 
$$var(M) = E[M^2] - E[M] ^ 2$$
$$E[M] = 2^{\frac{1}{p} - 1}$$
$$E[M^2] = \sum_{x=0}^\infty (2^{x})^2  \cdot p \cdot (1-p)^{x}$$  
I don't get anything sensical when I work out $E[M^2]$.
Am I missing something? Any elucidation would be much appreciated. I didn't study maths in university but I'm teaching myself probability theory now.

A:

It is not legal to write $E[2^{X}] = 2^{E[X]}$. However, the sum
$$
E[M] = \sum_{x=0}^\infty (2^{x})  \cdot p \cdot (1-p)^{x}
$$
is a geometric series, so it has a closed form:
$$
\sum_{x=0}^\infty ar^x = \frac a{1-r}\tag1
$$
whenever $|r|<1$. For the calculation of $E[M]$, you have $a=p$ and $r=2(1-p)$, so 
$$E[M]=\frac p{1-2(1-p)},$$
provided $2(1-p)<1$ (i.e. $p>\frac12$). The formula for $E[M^2]$ can be handled using (1) as well.

