Finance: Breakeven Rate of Borrowing vs. Investing? 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.


 A: Let's be more explicit about what's happening. If you borrow $\$10000$ at $5\%$ over a $10$-year term, you must pay
$$
A=\frac{\$10000(0.05)(1+0.05)^{10}}{(1+0.05)^{10}-1}\approx\$1295.045
$$
per year, which means that the total sum you pay is $\$12950.45$.
If you invest at your interest rate of $i \approx 2.619\%$, then you'll have $\$10000(1+i)^{10} \approx \$12950.24$ after $10$ years, which is basically equal to the above sum up to rounding error.
The problem is, you won't have the money when you need it. When you're borrowing the money, you have to start paying it back after a year; with the investment you've set up, you're assuming you won't make any withdrawals until the end of the full $10$-year period. If you work out what happens if you withdraw enough money every year to make the payment you owe in that particular year, you'll discover that you'd need your investment to return the full $5\%$ in order to cover the payments on your loan.
A: Your approximation is not bad at all.
We can make it a bit better considering (as you wrote)
$$i=\bigg(\dfrac{nr(1+r)^n}{(1+r)^n-1}\bigg)^{\frac{1}{n}}-1$$  Expand the rhs as a Taylor series around $r=0$. This would give
$$i=\frac{(n+1) r}{2 n}\left(1-\frac{(n-1) (n+3) r}{12 n}+O\left(r^2\right) \right)$$
For the example $n=10$, $r=\frac 5 {100}$, this would give $i=\frac{8371}{320000}\approx 0.0261594$ while the exact value should be $0.0261917$. 
If you want more accurate use
$$i=\frac{(n+1) r}{2 n}\left(1-\frac{(n-1) (n+3) }{12 n}r+\frac{(n-1)(n^2+2n-1)}{24n^2}r^2+O\left(r^3\right) \right)$$
For the example, this would give $\frac{3352327}{128000000}\approx 0.0261901$
Edit
Computing exactly $i$ for given $r$ and $n$ does not present any problem using a pocket calculator. Computing $r$ for given $i$ and $n$ is a very different story and it would require numerical methods.
However, we can get good approximation using the above Taylor series and series reversion. This process would give
$$r=x+\frac{(n-1) (n+3) x^2}{12 n}+\frac{(n-1) (n+2) \left(n^2-3\right) x^3}{72
   n^2}+O\left(x^4\right)$$ where $x=\frac{2  n}{n+1}i$.
Using $n=10$ and $i=\frac{25}{1000}$ would lead to $r=\frac{101381}{2129600}\approx 0.0476057$ while the exact calculation would give $r=0.0476143$
