Solution (source code)

= Solution

Let $B$ mean that the selected coin is biased and $F$ that it is fair. Both have prior <probability> $1/2$. Conditional on either coin, tosses are independent. For the observed head count, the two <binomial distribution> <likelihoods> are $\binom nk p^k(1-p)^{n-k}$ and $\binom nk2^{-n}$. <Bayes' theorem> cancels the common coefficient and equal prior weights, giving
$$
\boxed{\mathbb P(B\mid k\text{ heads in }n\text{ tosses})
=\frac{p^k(1-p)^{n-k}}{p^k(1-p)^{n-k}+2^{-n}}}.
$$
This applies for $0\leq k\leq n$, including endpoint coin biases with zero-power factors interpreted by the corresponding <likelihood>. The fair-coin <likelihood> is positive, so the denominator cannot vanish.