Formula for APR with Odd Days I need to programmatically calculate APR based on the following inputs:


*

*Principal Amount

*Number of payments (for example, 3 months loan is 3 if paying monthly, or 6 bi-weekly payments)

*Payment each period


From this, I need the formula for APR. 
Second, the customer may have an odd number of days before their first payment. I need to be able to plug this in as a variable in to calculation and get an adjusted APR. 
Can someone help me with the formula for this?
 A: Let $r$ denote the APR we seek to establish, and the payments are made each $\frac{1}{n}$ of the year (i.e. $n=12$ for monthly payments, and $n=26$ for biweekly). Let $a_k$ be the principal amount of the loan at the beginning of the $k$-th period. Then $a_0 = a$, and
$$
   a_{k+1} = a_k (1+r)^{1/n}-p
$$
Such a recurrence equation is not hard to solve using generating function technique, i.e. multiplying both sides of the equation with $x^k$ (for some indeterminate $x$) and forming $g(x) = \sum_{k=0}^\infty a_k x^k$:
$$\begin{eqnarray}
    \sum_{k=0}^\infty a_{k+1} x^k &=& (1+r)^{1/n} \sum_{k=0}^\infty a_k x^k - p \sum_{k=0}^\infty x^k \\
      \frac{f(x)-a_0}{x} &=& (1+r)^{1/n} f(x) - p \frac{1}{1-x} \\
     f(x) &=& \frac{1}{(1+r)^{1/n}-1} \left( \frac{p}{1-x} - \frac{a + p - a(1+r)^{1/n}}{1 - (1+r)^{1/n} x} \right) \\
      a_{k} &=& \frac{1}{(1+r)^{1/n}-1} \left( p - \left( a + p - a (1+r)^{1/n} \right)  (1+r)^{k/n}  \right)
\end{eqnarray}
$$
Requirement that after $m$ equal payments the loan is paid off gives the equation for effective annual percentage rate $r$:
$$
   p = \left( a + p - a (1+r)^{1/n} \right)  (1+r)^{m/n}
$$
As this is a non-linear equation, solving for $r$ will need to be done numerically. Here is a code in Mathematica:
In[63]:= FindAPR[a0_, p_, m_, n_] := 
 Block[{sol}, 
  sol = FindRoot[
    p == (a0 + p - a0 (1 + r)^(1/n)) ((1 + r)^(m/n)), {r, 1/2}];
  If[sol === {}, Indeterminate, r /. First[sol]]]

In[64]:= FindAPR[1000, 256, 4 (* payments *), 12 (* paid monthly *)]*100

Out[64]= 12.0876

If the time to the first payment is different, $a_0$ needs to be replaced with different effective principal amount, $a_0^\ast = a_0 \left(1 + r \right)^{d/n}$, where $d$ is the length of the no-payment period given as a fraction of payment intervals. For example, if payments are made monthly $n=12$, there are 4 payments to be made $m=4$ in the amount of $p=\$260.00$ on the loan of $\$1000.00$ with 1 month no payment period, the effective APR is
In[3]:= FindAPR2[a0_, p_, m_, n_, d_] := 
 Block[{sol}, 
  sol = FindRoot[
    p == (p - a0 (1 + r)^(d/n) ( (1 + r)^(1/n) - 1)) ((1 + r)^(m/
       n)), {r, 1/2}];
  If[sol === {}, Indeterminate, r /. First[sol]]]

In[5]:= FindAPR2[1000, 260, 4, 12, 1]*100

Out[5]= 14.4241

