These machines cost a lot! I dropped by http://www.edmunds.com/apps/calc/CalculatorController. But it's a black box to me: I had no real idea how it was actually doing its calculation. So I tried to cook up the calculations from scratch:
(module car-payment mzscheme
;; Small toy code for calculating car stuff.
(provide (all-defined))
;; debt: number number number number -> number
;; Returns the amount of money owed after months pass,
;; given a particular monthly payment and apr rate.
(define (debt months current-debt monthly-payment apr)
(let loop ([n months]
[current-debt current-debt])
(cond
[(= n 0) current-debt]
[else
(let ([monthly-interest (/ apr 12)])
(loop (sub1 n)
(* (+ 1 monthly-interest)
(- current-debt monthly-payment))))])))
;; months-till-debt-free: number number number -> number
;; Returns how many months have to pass before we're debt free.
(define (months-till-debt-free initial-cost monthly-payment apr)
(let loop ([n 0])
(let ([new-debt (debt n initial-cost monthly-payment apr)])
(cond
[(<= new-debt 0) n]
[else (loop (add1 n))])))))
Now I could verify for myself: how long would it be until I dig myself out of a big hole?
> (months-till-debt-free 16300 327.93 0.0767)
60
Oh. Ok, so at least it looks like the code is doing the same thing as the Edmunds calculator. That's good.
I should brush up on my recurrence relations, though, since I'm sure that there's a closed form for this calculation.
No comments:
Post a Comment