Skip to content

Commit 7b35f76

Browse files
* docs: kata description * feat: kata/financing-a-purchase * refactor: cast one of the operands of this subtraction operation to a "double". --------- Co-authored-by: ParanoidUser <[email protected]>
1 parent 15c0e66 commit 7b35f76

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
interface Finance {
2+
static String amort(double rate, double bal, int term, int numPayment) {
3+
rate /= 1200;
4+
double c = rate * bal / (1 - Math.pow(1 + rate, -term));
5+
double principal = (c - bal * rate) * Math.pow(1 + rate, numPayment - 1.);
6+
return String.format("num_payment %d c %.0f princ %.0f int %.0f balance %.0f",
7+
numPayment,
8+
c,
9+
principal,
10+
c - principal,
11+
bal * (1 - (Math.pow(1 + rate, numPayment) - 1) / (Math.pow(1 + rate, term) - 1)));
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
5+
6+
class FinanceTest {
7+
@ParameterizedTest
8+
@CsvSource(textBlock = """
9+
7.4, 10215, 24, 20, num_payment 20 c 459 princ 445 int 14 balance 1809
10+
7.9, 107090, 48, 41, num_payment 41 c 2609 princ 2476 int 133 balance 17794
11+
6.8, 105097, 36, 4, num_payment 4 c 3235 princ 2685 int 550 balance 94447
12+
3.8, 48603, 24, 10, num_payment 10 c 2106 princ 2009 int 98 balance 28799
13+
""")
14+
void sample(double rate, int bal, int term, int numPayments, String expected) {
15+
assertEquals(expected, Finance.amort(rate, bal, term, numPayments));
16+
}
17+
}

kata/6-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
- [Fibonacci Rabbits](fibonacci-rabbits "5559e4e4bbb3925164000125")
140140
- [Fibonacci, Tribonacci and friends](fibonacci-tribonacci-and-friends "556e0fccc392c527f20000c5")
141141
- [Figurate Numbers #1 - Pentagonal Number](figurate-numbers-number-1-pentagonal-number "55ab9eee6badbdaf72000075")
142+
- [Financing a purchase](financing-a-purchase "59c68ea2aeb2843e18000109")
142143
- [Financing Plan on Planet XY140Z-n](financing-plan-on-planet-xy140z-n "559ce00b70041bc7b600013d")
143144
- [Find last Fibonacci digit [hardcore version]](find-last-fibonacci-digit-hardcore-version "56b7771481290cc283000f28")
144145
- [Find Numbers with Same Amount of Divisors](find-numbers-with-same-amount-of-divisors "55f1614853ddee8bd4000014")

0 commit comments

Comments
 (0)