|
| 1 | +# [Financing a purchase](https://www.codewars.com/kata/financing-a-purchase "https://www.codewars.com/kata/59c68ea2aeb2843e18000109") |
| 2 | + |
| 3 | +The description is rather long, but it tries to explain what a financing plan is. |
| 4 | + |
| 5 | +The fixed monthly payment for a fixed rate mortgage is the amount paid by the borrower every month that ensures |
| 6 | +that the loan is paid off in full, with interest at the end of its term. |
| 7 | + |
| 8 | +The monthly payment formula is based on the annuity formula. |
| 9 | +The monthly payment `c` depends upon: |
| 10 | + |
| 11 | +- `rate` - the monthly interest rate is expressed as a decimal, not a percentage. |
| 12 | + The monthly rate is simply the **given** yearly percentage rate divided by 100 and then by 12. |
| 13 | + |
| 14 | +- `term` - the number of monthly payments, called the loan's `term`. |
| 15 | +- `principal` - the amount borrowed, known as the loan's principal (or `balance`). |
| 16 | + |
| 17 | +First we have to determine `c`. |
| 18 | + |
| 19 | +We have: `c = n /d` with `n = r LICENSE build.gradle.kts docs gradle gradle.properties gradlew gradlew.bat kata settings.gradle.kts balance` |
| 20 | +and `d = 1 - (1 + r)**(-term)` where `**` is the `power` function (you can look at the reference below). |
| 21 | + |
| 22 | +The payment `c` is composed of two parts. The first part pays the interest (let us call it `int`) |
| 23 | +due for the balance of the given month, the second part repays the balance (let us call this part `princ`) hence for the following month we |
| 24 | +get a `new balance = old balance - princ` with `c = int + princ`. |
| 25 | + |
| 26 | +Loans are structured so that the amount of principal returned to the borrower starts out small and increases with each mortgage payment. |
| 27 | +While the mortgage payments in the first years consist primarily of interest payments, the payments in the final years consist primarily of |
| 28 | +principal repayment. |
| 29 | + |
| 30 | +A mortgage's amortization schedule provides a detailed look at precisely what portion of each mortgage payment is dedicated to each |
| 31 | +component. |
| 32 | + |
| 33 | +In an example of a $100,000, 30-year mortgage with a rate of 6 percents the amortization schedule consists of 360 monthly payments. |
| 34 | +The partial amortization schedule below shows with 2 decimal floats |
| 35 | +the balance between principal and interest payments. |
| 36 | + |
| 37 | +| -- | num_payment | c | princ | int | Balance | |
| 38 | +|----|-------------|--------|--------|--------|-----------| |
| 39 | +| -- | 1 | 599.55 | 99.55 | 500.00 | 99900.45 | |
| 40 | +| -- | ... | 599.55 | ... | ... | ... | |
| 41 | +| -- | 12 | 599.55 | 105.16 | 494.39 | 98,771.99 | |
| 42 | +| -- | ... | 599.55 | ... | ... | ... | |
| 43 | +| -- | 360 | 599.55 | 596.57 | 2.98 | 0.00 | |
| 44 | + |
| 45 | +#### Task: |
| 46 | + |
| 47 | +Given parameters |
| 48 | + |
| 49 | +``` |
| 50 | +rate: annual rate as percent (don't forgent to divide by 100*12) |
| 51 | +bal: original balance (borrowed amount) |
| 52 | +term: number of monthly payments |
| 53 | +num_payment: rank of considered month (from 1 to term) |
| 54 | +``` |
| 55 | + |
| 56 | +the function `amort` will return a formatted string (for example): |
| 57 | + |
| 58 | +`"num_payment %d c %.0f princ %.0f int %.0f balance %.0f" (with arguments num_payment, c, princ, int, balance`) |
| 59 | + |
| 60 | +***In Common Lisp:*** |
| 61 | +return a list with num-payment, c, princ, int, balance each rounded. |
| 62 | + |
| 63 | +#### Examples: |
| 64 | + |
| 65 | +``` |
| 66 | +amort(6, 100000, 360, 1) -> |
| 67 | +"num_payment 1 c 600 princ 100 int 500 balance 99900" |
| 68 | +
|
| 69 | +amort(6, 100000, 360, 12) -> |
| 70 | +"num_payment 12 c 600 princ 105 int 494 balance 98772" |
| 71 | +
|
| 72 | +``` |
| 73 | + |
| 74 | +#### Ref |
| 75 | + |
| 76 | +<https://en.wikipedia.org/wiki/Mortgage_calculator> |
0 commit comments