Loan Amortization Schedule
Calculate monthly loan payments and generate amortization schedule. Shows principal and interest breakdown for each payment. Essential for mortgages, car loans, personal loans.
Visualization
Interactive visualization for Loan Amortization Schedule
Loan Amortization:
- • Fixed monthly payment
- • M = P[r(1+r)^n]/[(1+r)^n-1]
Interactive visualization with step-by-step execution
Implementation
1interface Payment {
2 month: number;
3 payment: number;
4 principal: number;
5 interest: number;
6 balance: number;
7}
8
9class LoanAmortization {
10 calculateMonthlyPayment(
11 principal: number,
12 annualRate: number,
13 years: number
14 ): number {
15 const monthlyRate = annualRate / 12;
16 const payments = years * 12;
17
18 if (monthlyRate === 0) return principal / payments;
19
20 return principal *
21 (monthlyRate * Math.pow(1 + monthlyRate, payments)) /
22 (Math.pow(1 + monthlyRate, payments) - 1);
23 }
24
25 generateSchedule(
26 principal: number,
27 annualRate: number,
28 years: number
29 ): Payment[] {
30 const monthlyPayment = this.calculateMonthlyPayment(principal, annualRate, years);
31 const monthlyRate = annualRate / 12;
32 const payments = years * 12;
33
34 const schedule: Payment[] = [];
35 let balance = principal;
36
37 for (let month = 1; month <= payments; month++) {
38 const interest = balance * monthlyRate;
39 const principalPayment = monthlyPayment - interest;
40 balance -= principalPayment;
41
42 schedule.push({
43 month,
44 payment: monthlyPayment,
45 principal: principalPayment,
46 interest,
47 balance: Math.max(0, balance)
48 });
49 }
50
51 return schedule;
52 }
53
54 totalInterest(principal: number, annualRate: number, years: number): number {
55 const monthlyPayment = this.calculateMonthlyPayment(principal, annualRate, years);
56 return monthlyPayment * years * 12 - principal;
57 }
58
59 payoffTime(
60 principal: number,
61 annualRate: number,
62 monthlyPayment: number
63 ): number {
64 const monthlyRate = annualRate / 12;
65
66 if (monthlyPayment <= principal * monthlyRate) {
67 return Infinity; // Payment too small
68 }
69
70 return Math.log(monthlyPayment / (monthlyPayment - principal * monthlyRate)) /
71 Math.log(1 + monthlyRate);
72 }
73
74 extraPaymentSavings(
75 principal: number,
76 annualRate: number,
77 years: number,
78 extraMonthly: number
79 ): { monthsSaved: number; interestSaved: number } {
80 const regularPayment = this.calculateMonthlyPayment(principal, annualRate, years);
81 const regularInterest = this.totalInterest(principal, annualRate, years);
82
83 const newMonths = this.payoffTime(principal, annualRate, regularPayment + extraMonthly);
84 const newInterest = (regularPayment + extraMonthly) * newMonths - principal;
85
86 return {
87 monthsSaved: years * 12 - newMonths,
88 interestSaved: regularInterest - newInterest
89 };
90 }
91}Deep Dive
Theoretical Foundation
Fixed payment formula: M = P[r(1+r)^n]/[(1+r)^n - 1] where P=principal, r=monthly rate, n=payments. Early payments mostly interest, later mostly principal. Total interest = M×n - P. Extra payments significantly reduce total interest.
Complexity
Time
O(n) for n payments
O(n)
O(n)
Space
O(n) for schedule
Applications
Industry Use
Home mortgage calculations
Auto loan payment schedules
Student loan repayment planning
Personal loan analysis
Business loan structuring
Refinancing decision analysis
Extra payment impact calculations
Loan comparison shopping
Use Cases
Related Algorithms
GCD (Euclidean Algorithm)
Compute the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm. Dating back to around 300 BC and appearing in Euclid's Elements, it's one of the oldest algorithms still in common use. The algorithm is based on the principle that GCD(a,b) = GCD(b, a mod b) and is remarkably efficient with O(log min(a,b)) time complexity. The GCD is the largest positive integer that divides both numbers without remainder.
LCM (Least Common Multiple)
Calculate the Least Common Multiple (LCM) of two integers - the smallest positive integer that is divisible by both numbers. The LCM is intimately related to the GCD through the formula: LCM(a,b) = |a×b| / GCD(a,b). This relationship allows us to compute LCM efficiently using the Euclidean algorithm for GCD, achieving O(log min(a,b)) time complexity instead of naive factorization methods.
Sieve of Eratosthenes
Ancient and highly efficient algorithm to find all prime numbers up to a given limit n. Invented by Greek mathematician Eratosthenes of Cyrene (276-194 BC), this sieve method systematically eliminates multiples of primes, leaving only primes in the array. With O(n log log n) time complexity, it remains one of the most practical algorithms for generating large lists of primes, vastly superior to trial division which runs in O(n² / log n) time.
Prime Factorization
Decompose a positive integer into its unique prime factor representation. Every integer greater than 1 can be expressed as a product of prime numbers in exactly one way (Fundamental Theorem of Arithmetic). This algorithm uses trial division optimized to check only up to √n, as any composite number must have a prime factor ≤ √n. Returns a map of prime factors to their powers, e.g., 360 = 2³ × 3² × 5¹.