본문 바로가기

algorithm/codewars

[6kyu] Financing a purchase

문제설명

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's term.

  • principal - the amount borrowed, known as the loan's principal (or balance).

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_paymentcprincintBalance
--1599.5599.55500.0099900.45
--...599.55.........
--12599.55105.16494.3998,771.99
--...599.55.........
--360599.55596.572.980.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





나의풀이

function amort(rate, bal, term, num_payments) { var r = rate/1200; var c = ((r * bal) / (1 - (1 + r)**(-term)));//string var old_bal = bal, new_bal = 0, princ = 0, int = 0; for(var i = 1; i <= num_payments; i+=1){ i !== num_payments ? (old_bal = ((1+r)*old_bal - c)) : (new_bal = ((1+r)*old_bal - c)); } princ = old_bal - new_bal; int = c - princ; return `num_payment ${num_payments} c ${c.toFixed()} princ ${princ.toFixed()} int ${int.toFixed()}
balance ${new_bal.toFixed()}`; }





다른사람의 풀이

function amort(perc_rate, bal, term, num_payments) { let rate = perc_rate / 1200 , payment = rate * bal / (1 - Math.pow(1 + rate, -term)) , comp = Math.pow(1 + rate, num_payments - 1) , int = rate * bal * comp - payment * (comp - 1) , princ = payment - int , balance = int / rate - princ return `num_payment ${num_payments} ` + `c ${payment.toFixed(0)} ` + `princ ${princ.toFixed(0)} ` + `int ${int.toFixed(0)} ` + `balance ${balance.toFixed(0)}`;  
}




느낀점

문제 설명 이해하는데에만 시간을 얼마나 썼는지 모르겠다. 내가 약한 경제쪽 수학이어서 더 오래 걸린듯하다. 문제자체가 어렵진않은데, 문제 설명을 이해못하고 정답을 내는게 싫어서 계속 보아서 이해했다. 월별 상환후 잔액만을 계속 반복문으로 구하여 나머지 변수의 값들을 구했다. 그리고 전에 공부후 포스팅한 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