Time to retire as a function of percentage of income saved. I've been interested in the math behind the teachings of Mr Money Moustache.
He talks about years to retire as a function of the percentage of income saved.
This is easy to calculate in a spreadsheet yet by just adding the sum of a compound interest function yet I'd love to see this function written more simply and elegantly.
We assume that the amount saved will last indefinitely without the taking from the principal. (Withdrawal = interest, and no inflation.)
The basic premise is that if t is the amount of time needed to retire, i is income, r is the interest rate, and p is the percentage saved of i, then if only saving for a single year the equation should be...
$i/r = ip(1+r)^t$
Numerically assuming a interest rate of 0.04, savings rate of 0.5 and income of  100,000 a year this would look like...
$100,000/0.04 = 100,000 * 0.5 *(1+0.04)^t $
or
$2,500,000 = 50,000*(1.04)^t$
You can see that to retire at the current income level of \$100,000 a year you need \$2,500,000 which will yeild interest of $100,000 per year.
However solving this for t only solves for saving in a single year, the reality would including include subsequent years subject to $t - years to retirement$ for each additional year of income.
This is the problem I'd like to solve without making $t$ the sum of multiple equations.
In my workings I stumbled upon the equation $ t = ln(p)/ln(1+r)$ which quite accurately matches the time to retire calculated in a spreadsheet. This function was created from calculating the time required to make your entire income back from a given saving rate of your income from a single year $ A = Ap(1+r)^t$.
In this sense the equation fails to account for multiple years yet the fact that it accounts for $pA$ compounded to get $A$ as opposed to $pA$ compounded to get $A/r$ means seems to offset the lack of subsequent years added.
It seems likely that this is a simplification of the equation I am trying to get to yet am unsure.
 A: First formulate the question mathematically.  Consider individual sums saved of $p \times I$ deposited at the start of each year for $n$ years with annual interest rate of $r$.  At the end of the $n$ years you will have accumulated a sum $C$,
\begin{align*}
C =  Ip(1+r)^{n} + Ip(1+r)^{n-1} + Ip(1+r)^{n-2} + \cdots + Ip(1+r).
\end{align*}
Notice there is interest on the last payment because each payment is made at the start of the year.  This will be sufficient to maintain your income if the interest on this capital is more than $I$.  This means you want,
\begin{align*}
Cr \geqslant I.
\end{align*}
So far, you have this in your post, except the analysis is now extended to multiple years of saving and introduced an inequality '$\geqslant$'.  The inequality simply articulates that when saving for whole years only, you may not hit the target exactly.  We assume you prefer to overshoot a little rather than undershoot.
The long sum can be evaluated because it is a geometric progression,
\begin{align*}
C &= Ip\times (1+r)\times \frac{(1+r)^n-1}{(1+r)-1} \\
&=Ip \times (1+r) \times \frac{(1+r)^n-1}{r} \\
\end{align*}
and you are looking for the value of $n$ which makes $Cr \geqslant I$, which is to say
\begin{align*}
Ip(1+r)((1+r)^n-1)\geqslant I.
\end{align*}
A little algebra shows that $I$ cancels and the condition is met if,
\begin{align*}
(1+r)^n \geqslant \Big( 1 + \frac{1}{p(1+r)} \Big) = \frac{1+p(1+r)}{p(1+r)} .
\end{align*}
You can take logarithms (to any base, but we may as well use $\log_e = \ln$) and using $\log(x\times y)=\log(x)+\log(y)$, we get,
\begin{align*}
n \geqslant \frac{\log((1+p(1+r))/p(1+r))}{\log(1+r)} = \frac{\log(1+p(1+r)) - \log p - \log(1+r)}{\log(1+r)}
\end{align*}
Now, $p$ and $r$ are small relative to $1$, which makes the first and last terms in the numerator small relative to the middle term, and so for practical purposes the numerator is close to $-\log p$.  We then have the formula you quoted (appropriately signed), which addresses the question for multiple years of saving,
\begin{align*}
n \geqslant \frac{-\log p}{\log(1+r)} \quad \text{(approximately)}.
\end{align*}
A refinement might be to consider shorter intervals of saving.  The mathematics is the same.  If you want to work with monthly savings cycle, then instead of an annual interest rate $r$, and $n$ years we use monthly interest and $n$ counts the months to retirement.  The savings rate $p$ remains the same because we will continue saving the same proportion of income, only now applied to monthly income instead of annual income.
