Compound interest formula with regular deposits, solve for time I cant seem solve the following problem due to my bad memory of logarithms
if someone opens an account with 
initial balance of $B$
interest rate of $R$
and does a constant monthly deposit of $D$
how long before the account balance reaches $X$ ?
 A: Update 2- Warning: I checked this solution numerically and it seems right to me, if my understanding of the question is correct. However, it differs from Robert Israel's answer. 
Update: edited the text several times, solved for $n$ and included figure.
I changed the notation, instead of $D$  I denote each constant deposit by $A$ (from Annuity, the uniform amounts deposited/paid in a period, i.e. a month in the present case). Let $i$ be the effective interest rate per month, and $n$ the number of months. 
I am going to consider separately the uniform sequence of deposits $A$ and the initial balance $B$.

Concerning the sequence of $n$ monthly constant deposits  $A$, at the interest rate of $i$, its future accumulated amount  $F$ after $n$ months, can be computed as follows. The first deposit earns interest during $n-1$ months. The second, $n-2$, and, in general, the $k$  deposit, $n-k$ months. The deposit in the $k$ month values after $n$ months 
$$F_k=A(1+i)^{n-k}.$$
Adding all future values $F_k$, for $k=1,2,\dots,n$, and using the sum formula of a geometric progression with $n$ terms, ratio $c$ and first term $u_1$, 
$$u_1\dfrac{c^n-1}{c-1},$$
where, in the present case, $u_1=A$ and $c=1+i$, yields
$$F=\displaystyle\sum _{k=1}^{n}F_k=\displaystyle\sum _{k=1}^{n}A(1+i)^{n-k}=\displaystyle\sum _{m=1}^{n}A(1+i)^{m-1}=A\dfrac{(1+i)^n-1}{i}.$$
There are three implicit conditions when applying this formula: 


*

*the deposits $A$ are uniform and equally spaced in time, 

*they are made always at the end of each month, and

*the interest rate $i$ remains unchanged.


The total amount $X$ at the end of $n$ months is the sum of the future value of the initial balance $B$, which is $B\times (1+i)^n$, with the future value $F$ of all monthly deposits calculated above. And we know that this sum equals $X$, the balance at the end of $n$ months:
$$X=B(1+i)^n+A\dfrac{(1+i)^n-1}{i}.$$
Hence
$$(1+i)^{n}=\frac{A+X\times i}{A+B\times i}.$$
Taking logarithms (remember that $\ln a^n=n\ln a, \ln\frac{a}{b}=\ln{a}-\ln{b}$) and solving for $n$, as Robert Israel wrote in his answer, we obtain
$$n=\frac{1}{\ln (1+i)}\ln \left( \frac{A+X\times i}{A+B\times i}\right)=\frac{1}{\ln (1+i)}(\ln (A+X\times i)-\ln (A+B\times i)).$$

Note 1. As Ross Milikan commented, "if $i\ll 1$ (as it usually is these days) you can use $(1+i)^n\approx 1+in$ and avoid the logs entirely." I got 
$$(1+i)^{n}\approx 1+in=\frac{A+X\times i}{A+B\times i}\Leftrightarrow n\approx \frac{1}{i}\left(\frac{A+X\times i}{A+B\times i}-1\right).$$
Note 2. For a given $n$ if one had to solve for $i$, one could use the secant method. For an example see this post of mine, in Portuguese.
A: I'll use Americo's notation.  Here's a simple way to do this.  Suppose at the same time as you make the inital deposit of B, your friend takes out a loan at the same interest rate, in the amount $C$.  So the two of you have a net balance of $B - C$,
which would generate a monthly interest of $(B - C)i$.  We choose $C =  B + A/i$, 
so this interest would eat up your monthly deposit; with your friend making no payments, your combined net balance would stay constant at $-A/i$.  But of course
your friend's balance owing will increase as yours decreases: after $n$ months your friend owes $C (1 + i)^n$.  Your account balance will reach $X$ when your friend's debt reaches $B - C + X$.  That is, $C (1 + i)^n = B - C + X$, or $(1 + i)^n = \frac{B - C + X}{C} = \frac{X - A/i}{B + A/i}$.  Now take logarithms and solve for $n$.
A: 
There are three implicit conditions when applying this formula:

*

*the deposits $A$ are uniform and equally spaced in time,

*they are made always at the end of each month, and

*the interest rate $i$ remains unchanged.
$$X=B(1+i)^n+A\dfrac{(1+i)^n-1}{i}.$$

I'd like to note that when the second implicit condition for applying the formula derived by Américo Tavares changes to:
They are made always at the beginning of each month
The formula becomes:
$$X=B(1+i)^n+A\dfrac{(1+i)^{n+1}-(1+i)}{i}.$$
This formula is  also used in Microsoft Excel to calculate the Future Value (FV). The user can choose whether deposits are made at the beginning or at the end of the period.
A: I just spent a few days looking into this question and while this question is quite old it was the top result on google so it seems still relevant. I wanted to share what I found to save someone in the future time. 
What the question asker is looking for is the Annuity Formula solved for n.
Source for this here. Laid out bellow in case the source site is no longer available.
Here's the Annuity Formaula. We will derive the solution from here:
$$
FV of Annuity = P \Bigg[\frac{(1+r)^n-1}{r}\Bigg]
$$
P = Periodic payment
R = Rate per period (this is an effective rate)
n = Number of periods
To solve for n we will begin by multiplying both sides by r/p and add 1 to each side:
$$
1+\frac{FV \times r}{P}=(1+r)^n
$$
Now we take the natural log of both sides to pull out the exponent n:
$$
ln\bigg(1+\frac{(FV \times r)}{P}\bigg)=n\times ln(1+r)
$$
Which results in:
$$
n = ln\bigg(1+\frac{(FV \times r)}{P}\bigg) \div ln(1+r)
$$
