Basic Theory
The way to solve this problem is to calculate how much each payment reduces your debt after you have been repaying your loan for $n$ years. Let $r=1+R/100$, ie. this converts the interest rate from a percentage to a value you can multiply your debt by to calculate how much you owe after adding one time period's interest.
If I make a payment of $P$ at the end of the $k$th year, then we avoid paying interest on this money $n-k$ times and so we reduce our debt by $Pr^{n-k}$. We sum up the future values of all our payments:
$\sum\limits_{k=1}^n Pr^{n-k}$
If we reverse this, it is equivalent to:
$\sum\limits_{k=0}^{n-1} Pr^k$
This is a geometric series, which can be solved using the formula $\frac{ar^{n-1}}{r-1}$ where $a$ is the first term, $r$ is the factor and $n$ is the number of terms being summed. We then attempt to equate this with the debt owed after $n$ years, which is $Mr^n$.
We now compare the two equations:
$\frac{Pr^{n-1}}{r-1} = Mr^n$
Calculating $n$
We group the $r^n$ terms:
$\frac{P}{r-1} = r^n\frac{M-P}{r-1}$
$r^n = \frac{P}{M(r-1)-P}$
So we just take the $n$th log of the right hand side.
Calculating repayments
Given the principal ($M$) and the interest rate ($r$), what will my payment-per-term ($P$) be over $n$ accruation terms?
$P=\frac{Mr^n(r-1)}{r^{n-1}}$
Payments made at the start of the year
In this case, the future values of our interest payment simply become:
$\sum\limits_{k=1}^n Pr^k$
We proceed as we did before.
Notes
We could also solve this problem using present value instead of future value.