Problem Statement
- At the beginning of each month, the purchasers pay a fixed amount towards settling the loan, which decreases the amount they owe.
- At the end of the month, the amount the purchasers owe increases due to interest. Each month, 1/12 of the annual interest rate is added to the amount owed. Hence, if the annual interest rate is 12%, then the debt increases by 1% each month. You may assume that the amount owed after adding interest is always rounded up to the nearest dollar greater than or equal to the actual value.
For example, if loan = 1000, interest = 50, and term = 1, then the loan is for $1000, to be paid back in one year, at an annual interest rate of 5%, or (5/12)% per month. If the purchasers pay back $86 every month, then the total amount owed will be as follows after each month:
month | after making payment | after interest accrues ------+----------------------+------------------------------------ 1 | 1000 - 86 = 914 | ceiling(914 * (1 + 5/12/100)) = 918 2 | 918 - 86 = 832 | ceiling(832 * (1 + 5/12/100)) = 836 3 | 836 - 86 = 750 | 754 4 | 754 - 86 = 668 | 671 5 | 671 - 86 = 585 | 588 6 | 588 - 86 = 502 | 505 7 | 505 - 86 = 419 | 421 8 | 421 - 86 = 335 | 337 9 | 337 - 86 = 251 | 253 10 | 253 - 86 = 167 | 168 11 | 168 - 86 = 82 | 83 12 | 86 is more than enough to pay off the restClearly, 85 a month wouldn't be enough, since we just barely paid off the loan at 86.
Definition
- Class:
- Mortgage
- Method:
- monthlyPayment
- Parameters:
- int, int, int
- Returns:
- int
- Method signature:
- int monthlyPayment(int loan, int interest, int term)
- (be sure your method is public)
Constraints
- loan will be between 100 and 2,000,000,000, inclusive.
- interest will be between 1 and 1,000,000, inclusive.
- term will be between 1 and 1000, inclusive.
Examples
1000
50
1
Returns: 86
From the problem statement.
2000000000
6000
1
Returns: 671844808
interest = 6000 means that the monthly interest is a whopping 50%!
1000000
1000000
1000
Returns: 988143
The interest is so high that even if we had 1000 years to pay back the loan, we'd still have to pay almost a million dollars a month.
1000000
129
30
Returns: 10868
100
1
1000
Returns: 2
618083873
932377
462
Returns: 610230012
37007334
497647
389
Returns: 36135971
910806859
106489
245
Returns: 818564691
620972289
447604
815
Returns: 604759055
1964106774
57176
469
Returns: 1623392057
396495408
965433
303
Returns: 391627612
1303970803
751105
470
Returns: 1283465566
560290889
94158
606
Returns: 496956137
1193263809
406909
59
Returns: 1159081766
790990757
569741
255
Returns: 774674409
1878848491
765597
345
Returns: 1849853804
1767005838
159220
890
Returns: 1643164757
160606687
352973
961
Returns: 155326077
468903894
557830
713
Returns: 459029289
622058302
712197
633
Returns: 611750749
804652054
4523
437
Returns: 220265161
317486105
626173
530
Returns: 311516199
1266328720
680421
78
Returns: 1244382615
133678780
660664
497
Returns: 131294016
180769391
462677
325
Returns: 176199478
645338021
625478
616
Returns: 633190063
534515614
800188
584
Returns: 526618197
705069909
347802
229
Returns: 681554646
614051902
656351
585
Returns: 603026823
721647343
325968
970
Returns: 696024302
1214770213
843509
161
Returns: 1197730951
925650314
407094
289
Returns: 899145989
1992772528
53725
966
Returns: 1628934258
327822429
896518
882
Returns: 323492445
1280563618
750707
121
Returns: 1260415956
1114588932
332313
22
Returns: 1075743268
61085060
497791
148
Returns: 59647176
239835955
918655
492
Returns: 236743476
252618379
388703
450
Returns: 245053124
1157290063
733456
947
Returns: 1138660553
259665595
69925
161
Returns: 221630965
929938417
686154
508
Returns: 913954464
175138409
695122
926
Returns: 172166276
1495989673
426673
394
Returns: 1455066535
1041972280
681297
337
Returns: 1023937200
1382590995
11132
41
Returns: 665355481
265538953
562037
137
Returns: 259987974
1981832936
758911
487
Returns: 1950983727
512042040
323687
861
Returns: 493737774
163543995
973173
738
Returns: 161551931
1511988723
580548
752
Returns: 1481368648
1490110002
305117
39
Returns: 1433722865
1463035902
147540
281
Returns: 1352991833
1378407427
121376
802
Returns: 1254390445
1939524178
2486
309
Returns: 332849450
182518
88
197
Returns: 1330
87128
96
279
Returns: 693
184597
40
497
Returns: 615
72135
69
268
Returns: 414
179135
74
12
Returns: 1870
48826
32
424
Returns: 131
41875
84
677
Returns: 293
177616
57
625
Returns: 841
77336
83
661
Returns: 533
13115
25
82
Returns: 32
187890
49
103
Returns: 770
164227
44
706
Returns: 601
138709
23
280
Returns: 267
9303
82
1000
Returns: 65
14512
15
310
Returns: 20
151273
73
895
Returns: 916
127121
6
846
Returns: 65
143038
65
879
Returns: 772
126061
53
955
Returns: 556
113753
76
387
Returns: 717
1999999999
1000000
1
Returns: 1976284585
2000000000
6000
1
Returns: 671844808
20000000
46
999
Returns: 76375
1000000
1000000
1000
Returns: 988143
1999999999
1000000
1
Returns: 1976284585
1999999997
100000
1
Returns: 1785714284
1000
50
1
Returns: 86
3948234
984
73
Returns: 299221
2000000000
1
1000
Returns: 263648