For a fixed-rate loan with monthly payments, the formula is:
A = P*r / (1 - (1+r)^(-n))
Here P is the principal, r is the monthly interest rate, A is the payment and n is the number of months.
Rearranging gives:
(1+r)^(-n) = 1 - P*r/A
Is the next step:
n = -ln(1 - P*r/A) / ln(1+r)?
For a concrete example, take P = 27,800, r = 0.0599/12 and A = 450. How should a non-integer result be interpreted when payments happen monthly and the final payment can be smaller?
Also, am I right that A must be greater than P*r for a finite payoff time? I'm interested in how that condition appears both in the algebra and in what happens to the balance each month.