To calculate the $n$-period payment $A$ on a loan of size $P$ at an interest rate of $r$, the formula is:
$A=\dfrac{Pr(1+r)^n}{(1+r)^n-1}$
Source: https://en.wikipedia.org/wiki/Amortization_calculator#The_formula
And so the total amount paid over those n-periods is simply:
$n*A=\dfrac{nPr(1+r)^n}{(1+r)^n-1}$
For example, to full amortize a 10-year loan of $10,000 with 5.00% annual interest would require annual payments (principal + interest) of:
$A=\dfrac{10000*0.05(1.05)^{10}}{(1.05)^{10}-1}\approx1295$ per year
And over those 10 years then, the person would have paid a total of: $n*A=10*1295=12950$.
This is the underlying formula for most "amortizing" loans with $n$ equal installment payments (e.g. car loans, mortgages, student loans). As principal balance is being paid off over time, the interest payments that are based on that decreasing principal balance are decreasing too -- allowing more of the fixed $n$-period payment $A$ to go toward paying off principal. In the end it all balances out (i.e. the increasing portion of $A$ going toward paying principal offsets the a decreasing portion of $A$ going toward paying interest payments).
Investing on the other hand works differently with the idea of "compound interest" being earned. The total amount $B$ you will have after investing $P$ at rate $r$ over $n$ periods is simply:
$B=P(1+i)^n$
For instance, if one invests $10,000 at 5.00%/year for 10 years, the compound interest results in:
$B=P(1+i)^n=10000*1.05^{10}=16289$.
Comparing investing rate ($i$) to borrowing rate ($r$), the break-even analysis for $B=nA$ should result in $0<i<r$.
Computing this explicitly, assume $B=nA$:
$B=nA$
$P(1+i)^n=\dfrac{nPr(1+r)^n}{(1+r)^n-1}$
$(1+i)^n=\dfrac{nr(1+r)^n}{(1+r)^n-1}$
$i=\bigg(\dfrac{nr(1+r)^n}{(1+r)^n-1}\bigg)^{(\frac{1}{n})}-1$
Thus $0<i<r$ (I couldn't come up with a more simplified formula above, sorry, but the graph plot checks out).
Using the example above borrowing at $r=5\%$, if we invest at $i\approx2.619\%$ then $nA=B$. Notice how much smaller $i$ is than $r$ to simply break even... amazing!
In fact, for typical $r$ like what we would see for common long-term loans, say $2\%<r<8\%$, the formula is approximately:
$i\approx\dfrac{r}{2}+0.1\%$ (where $2\%<i<r<8\%$) (based on regression approximation)
Question: Is this true or not? So many people have told me "Only say yes to an X% loan if you think you can beat that same X% investing in the market!" This math makes it seem like actually, you should say "Yes" to loans at X% rates if you can simply beat at least half of that rate investing in the market over the same period.