Problem Statement
Auto dealerships frequently advertise tempting loan offers in order to make it easier for people to afford the "car of their dreams". A typical sales tactic is to show you various cars, and then talk in terms of what your monthly payment would be, to say nothing of how much you are actually paying for the car, how much interest you pay, or how long you have to make payments.
A typical auto loan is calculated using a fixed interest rate, and is set up so that you make the same monthly payment for a set period of time in order to fully pay off the balance. The balance of your loan starts out as the sticker price of the car. Each month, the monthly interest is added to your balance, and the amount of your payment is subtracted from your balance. (The payment is subtracted after the interest is added.) The monthly interest rate is 1/12 of the yearly interest rate. Thus, if your annual percentage rate is 12%, then 1% of the remaining balance would be charged as interest each month.
You have been checking out some of the cars at your local dealership, TopAuto. An excited salesman has just approached you, shouting about how you can have the car you are looking at for a payment of only monthlyPayment for only loanTerm months! You are to return a double indicating the annual percentage rate of the loan, assuming that the initial balance of the loan is price.
Definition
- Class:
- AutoLoan
- Method:
- interestRate
- Parameters:
- double, double, int
- Returns:
- double
- Method signature:
- double interestRate(double price, double monthlyPayment, int loanTerm)
- (be sure your method is public)
Notes
- Because of the way interest is compounded monthly, the actual interest accrued over the course of a year is not necessarily the same as (balance * yearly interest rate). In fact, it's usually more.
- In a real situation, information like this would typically need to be disclosed, but since you aren't at a point of signing any paperwork, the salesman has no legal obligation to tell you anything.
- The return value must be within 1e-9 absolute or relative error of the actual result.
Constraints
- price will be between 1 and 1000000, inclusive.
- monthlyPayment will be between 0 and price / 2, inclusive.
- loanTerm will be between 1 and 600, inclusive.
- The resulting interest rate will be between 0 and 100, inclusive.
Examples
6800
100
68
Returns: 1.3322616182218813E-13
Noting that 68 payments of 100 equals the total price of 6800, so there is no interest.
2000
510
4
Returns: 9.56205462458368
Here, we do pay a little interest. At 9.562% annual interest, that means each month we pay 0.7968% of the balance in interest. Our payment schedule looks like this: Month | + Interest | - Payment | = Balance ------------------------------------------ | | | 2000.00 1 | 15.94 | 510.00 | 1505.94 2 | 12.00 | 510.00 | 1007.94 3 | 8.03 | 510.00 | 505.97 4 | 4.03 | 510.00 | 0.00
15000
364
48
Returns: 7.687856394581649
This is similar to what purchasing a new car with no money down might look like, if you make payments for 4 years.
10000
385
30
Returns: 11.473054204549669
200
15
600
Returns: 89.99999999999982
1000000
1667
600
Returns: 7.986158248041164E-4
724556
50505
575
Returns: 83.64570854426711
332696
10951
136
Returns: 38.98851536883803
23002
4709
6
Returns: 74.54805516148613
492214
2049
471
Returns: 3.9204806010895594
740192
7185
588
Returns: 11.607783057395558
570542
24374
65
Returns: 47.05796362542851
407050
35078
17
Returns: 55.397079816319696
140192
1491
454
Returns: 12.653412761497272
219252
8934
337
Returns: 48.897089628821746
506973
11900
50
Returns: 7.7628540286105885
281335
16982
54
Returns: 68.87780115037442
286417
9239
145
Returns: 38.30192971448798
140642
1293
561
Returns: 10.965203755132924
389951
12413
163
Returns: 37.9601927752288
973110
337873
3
Returns: 24.80783488352443
230977
67123
4
Returns: 75.65099005216278
61703
16197
4
Returns: 23.76581938256987
44755
8959
5
Returns: 0.3574309895975514
297007
56775
6
Returns: 48.76330124289328
584755
1613
475
Returns: 1.4303822503235695
5628
2197
3
Returns: 100.00000000000003
6800.0
100.0
68
Returns: 1.3322616182218813E-13
15000.0
364.0
48
Returns: 7.687856394581649
2000.0
510.0
4
Returns: 9.56205462458368
60000.0
4999.0
600
Returns: 99.9799999999999
100001.0
989.0
599
Returns: 11.834664337677792
1000.0
500.0
2
Returns: 1.3322616182218813E-13
12.0
1.0
500
Returns: 99.99999999999977
1000000.0
2500.0
600
Returns: 1.7463073077668143
1.0
0.142857142857143
7
Returns: 3.9968010105369827E-13
1000000.0
79865.0
599
Returns: 95.83799999999995
999999.0
3000.0
599
Returns: 2.6299652255705737
15000.0
364.0
600
Returns: 29.11998354222205
9.999999991
2.5
4
Returns: 4.320001689699897E-7
200.0
100.0
2
Returns: 1.3322616182218813E-13
1000.0
40.0
34
Returns: 22.418392731637795