1---
2app_version: 0.1.0
3precision: 2
4form_title: Savings Goal Calculator
5form_description: Calculate how much you need to save to reach your goal. Enter target amount, interest rate and timeframe to see required monthly savings.
6form_inputs: Target amount|target{€}, Current savings|current{€}, Interest rate|rate{%}, Years|years
7form_outputs: Monthly savings needed|monthly_savings, Total to save|total_savings, Final amount|final_amount, Interest earned|interest_earned
8---
9# Savings Goal Calculator
10// How much do you need to save each month to reach your goal?
11
12# Goal parameters
13target = 50000 € // savings target
14current = 5000 € // current savings
15rate = 3 % // annual interest rate
16years = 10 // time horizon (years)
17
18# Calculations
19// Monthly interest rate
20monthly_rate = rate / 12
21months = years * 12
22
23// Future value of current savings
24fv_current = current * (1 + monthly_rate)^months
25
26// We need: fv_current + FV(monthly savings annuity) = target
27// FV(annuity) = P * (((1 + r)^n - 1) / r)
28// So: P * (((1 + r)^n - 1) / r) = target - fv_current
29
30annuity_factor = ((1 + monthly_rate)^months - 1) / monthly_rate
31monthly_savings = (target - fv_current) / annuity_factor
32
33// Total amount saved (monthly * months)
34total_savings = monthly_savings * months
35
36// Final amount (current + savings + interest)
37final_amount = current * (1 + monthly_rate)^months + monthly_savings * annuity_factor
38
39// Interest earned
40interest_earned = final_amount - current - total_savings
41
42// Alternative: save at end of each month (annuity due vs ordinary annuity)
43// For ordinary annuity (payments at end of period), the formula above is correct
44
45// What if you start with 0?
46monthly_from_zero = target / annuity_factor
47
48# Results
49"Savings goal": target
50"Current savings": current
51"Interest rate (%)": rate * 100
52"Time horizon": years
53
54"Monthly savings needed": monthly_savings
55"Total contributions": total_savings
56"Final amount": final_amount
57"Interest earned": interest_earned
58
59"If starting from 0, monthly": monthly_from_zero
60
61// Progress visualization
62balance(month) = current * (1 + monthly_rate)^month + monthly_savings * ((1 + monthly_rate)^month - 1) / monthly_rate
63plot(balance(x), 0..months, "Savings growth", "Months", "€", "Account balance over time")
64