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$ ?

-
Do you have a formula for the amount in the account as a function of time? – Ross Millikan Apr 21 '11 at 22:33
Can you please elaborate: do the deposits begin immediately? Do you have knowledge of the formula for the accumulated/present value of an annuity? Is R the effective rate of interest per month? This seems like a rather difficult problem for those who can't recall log rules (en.wikipedia.org/wiki/Logarithm#Logarithmic_identities) – Tyler Apr 21 '11 at 23:19

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:

1. the deposits $A$ are uniform and equally spaced in time,
2. they are made always at the end of each month, and
3. 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.

-
 Also, if $in \ll 1$ (as it usually is these days) you can use $(1+i)^n\approx 1+in$ and avoid the logs entirely. – Ross Millikan Apr 22 '11 at 12:54 @Ross Millikan: thanks for the estimate. – Américo Tavares Apr 22 '11 at 12:56 Actually the requirement is $i\ll 1,$ not $in \ll 1$. Even easier to meet. – Ross Millikan Apr 22 '11 at 16:23 @Ross: thanks once more. – Américo Tavares Apr 22 '11 at 16:24

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$.

-
 I detailed my solution, which equals yours, except that instead of a positive and a negative term in the numerator, it has two positive terms: $X+A/i$, and not $X-A/i$. Would you mind checking the sign of $A/i$? – Américo Tavares Apr 22 '11 at 16:22

There are three implicit conditions when applying this formula:

1. the deposits $A$ are uniform and equally spaced in time,
2. they are made always at the end of each month, and
3. 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.

-