문제설명
Description:
The description is rather long but it tries to explain what a financing plan is.
The fixed monthly payment for a fixed rate mortgage is the amount paid by the borrower every month that ensures that the loan is paid off in full with interest at the end of its term.
The monthly payment formula is based on the annuity formula. The monthly payment c
depends upon:
rate
- the monthly interest rate is expressed as a decimal, not a percentage. The monthly rate is simply the given yearly percentage rate divided by 100 and then by 12.term
- the number of monthly payments, called the loan'sterm
.principal
- the amount borrowed, known as the loan's principal (orbalance
).
First we have to determine c
.
We have: c = n /d
with n = r * balance
and d = 1 - (1 + r)**(-term)
where **
is the power
function (you can look at the reference below).
The payment c
is composed of two parts. The first part pays the interest (let us call it int
) 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 get a new balance = old balance - princ
with c = int + princ
.
Loans are structured so that the amount of principal returned to the borrower starts out small and increases with each mortgage payment. While the mortgage payments in the first years consist primarily of interest payments, the payments in the final years consist primarily of principal repayment.
A mortgage's amortization schedule provides a detailed look at precisely what portion of each mortgage payment is dedicated to each component.
In an example of a $100,000, 30-year mortgage with a rate of 6 percents the amortization schedule consists of 360 monthly payments. The partial amortization schedule below shows with 2 decimal floats the balance between principal and interest payments.
-- | num_payment | c | princ | int | Balance | |
---|---|---|---|---|---|---|
-- | 1 | 599.55 | 99.55 | 500.00 | 99900.45 | |
-- | ... | 599.55 | ... | ... | ... | |
-- | 12 | 599.55 | 105.16 | 494.39 | 98,771.99 | |
-- | ... | 599.55 | ... | ... | ... | |
-- | 360 | 599.55 | 596.57 | 2.98 | 0.00 |
Task:
Given parameters
rate: annual rate as percent (don't forgent to divide by 100*12)
bal: original balance (borrowed amount)
term: number of monthly payments
num_payment: rank of considered month (from 1 to term)
the function amort
will return a formatted string:
"num_payment %d c %.0f princ %.0f int %.0f balance %.0f" (with arguments num_payment, c, princ, int, balance
)
Examples:
amort(6, 100000, 360, 1) ->
"num_payment 1 c 600 princ 100 int 500 balance 99900"
amort(6, 100000, 360, 12) ->
"num_payment 12 c 600 princ 105 int 494 balance 98772"
Ref
나의풀이
다른사람의 풀이
느낀점
문제 설명 이해하는데에만 시간을 얼마나 썼는지 모르겠다. 내가 약한 경제쪽 수학이어서 더 오래 걸린듯하다. 문제자체가 어렵진않은데, 문제 설명을 이해못하고 정답을 내는게 싫어서 계속 보아서 이해했다. 월별 상환후 잔액만을 계속 반복문으로 구하여 나머지 변수의 값들을 구했다. 그리고 전에 공부후 포스팅한 ES6 템플릿 표현식을 마지막에 써보았는데, 꽤나 편한것 같다. 다른사람의 풀이 중에 반복문을 쓰지 않고 푼 풀이가 있어 가져왔는데, 봐도 모르겠다. 6, 7번째 줄이 반복문 없이도 월별 이자를 구할 수 있는 공식 같은걸로 보이는 데, 이해가 되질않는다.. comp는 무얼 나타내는지 모르겠고... 반복문을 안쓰고 푼다면 훨씬 효율이 좋지 않을까 싶다. 혹시 아시는 분은 help 바랍니다... ㅠㅠ
'algorithm > codewars' 카테고리의 다른 글
[6kyu] Compare powers (0) | 2018.06.15 |
---|---|
[6kyu] Highest Scoring (0) | 2018.02.13 |
[6kyu] Tribonacci sequence (0) | 2018.02.08 |
[6kyu] Simple string expansion (0) | 2018.02.07 |
[7kyu] Homogenous array (0) | 2018.02.04 |