Module 2 · Financial Arithmetic, Time Value, and Returns Lesson 12 of 120
Compound Interest
When a savings balance earns on previous interest.
Transcript
21 sentences · select one to jump thereCheck your understanding
Why is year-two interest 52.50 rather than 50?
Code lab
Run it yourself
The lesson source in 7 languages. Edit it, run TypeScript and Python right here, and compare with the expected output.
/**
* Fintech Math Bootcamp · Lesson 012 of 120
* Compound Interest
* Module 02: Financial Arithmetic, Time Value, and Returns
*
* Scenario: When a savings balance earns on previous interest
* Rule: FV = P(1+r)ⁿ
*
* Try it: Why is year-two interest 52.50 rather than 50?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/compound-interest/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*/
export function lesson012() {
const principal = 1000, rate = 0.05;
const balances = Array.from({length: 4}, (_, t) =>
principal * (1 + rate) ** t);
const result = balances;
return result;
}
export const checkedResult = [1000,1050,1102.5,1157.6250000000002];
// Run this file directly: npx tsx lessons/02-financial-arithmetic-time-value-and-returns/012-compound-interest.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
console.log(JSON.stringify(lesson012(), null, 2));
}
Your output
Press Run to execute the code in your browser.
Expected output
[
1000,
1050,
1102.5,
1157.6250000000002
]"""
Fintech Math Bootcamp · Lesson 012 of 120
Compound Interest
Module 02: Financial Arithmetic, Time Value, and Returns
Scenario: When a savings balance earns on previous interest
Rule: FV = P(1+r)ⁿ
Try it: Why is year-two interest 52.50 rather than 50?
Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/compound-interest/
Free course: https://courses.thefintechbuilder.com
Synthetic teaching example, not financial advice or a production library.
Run it: python main.py
"""
import json
def lesson012() -> list:
principal, rate = 1000, 0.05
balances = [principal * (1 + rate) ** t for t in range(4)]
result = balances
return result
if __name__ == "__main__":
print(json.dumps(lesson012(), indent=2))
Your output
Press Run to execute the code in your browser.
Expected output
[
1000,
1050,
1102.5,
1157.6250000000002
]/**
* Fintech Math Bootcamp · Lesson 012 of 120
* Compound Interest
* Module 02: Financial Arithmetic, Time Value, and Returns
*
* Scenario: When a savings balance earns on previous interest
* Rule: FV = P(1+r)^n
*
* Try it: Why is year-two interest 52.50 rather than 50?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/compound-interest/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: javac Main.java && java Main
*/
public class Main {
static double[] lesson012() {
double principal = 1000, rate = 0.05;
double[] balances = new double[4];
for (int t = 0; t < balances.length; t++) {
balances[t] = principal * Math.pow(1 + rate, t);
}
double[] result = balances;
return result;
}
public static void main(String[] args) {
System.out.println(numbers(lesson012()));
}
// Formats a double the way JSON.stringify does: whole numbers without ".0", null for NaN or infinity.
static String num(double x) {
if (Double.isNaN(x) || Double.isInfinite(x)) return "null";
if (x == Math.rint(x) && Math.abs(x) < 1e15) return String.valueOf((long) x);
return String.valueOf(x);
}
// Indents an already formatted JSON value by one level.
static String nested(String json) {
return json.replace("\n", "\n ");
}
static String array(java.util.List<String> items) {
if (items.isEmpty()) return "[]";
StringBuilder out = new StringBuilder("[");
for (int i = 0; i < items.size(); i++) {
out.append(i == 0 ? "\n " : ",\n ").append(nested(items.get(i)));
}
return out.append("\n]").toString();
}
static String numbers(double... values) {
java.util.List<String> items = new java.util.ArrayList<String>();
for (double value : values) items.add(num(value));
return array(items);
}
}
No browser runner for Java yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
[
1000,
1050,
1102.5,
1157.6250000000002
]// Fintech Math Bootcamp · Lesson 012 of 120
// Compound Interest
// Module 02: Financial Arithmetic, Time Value, and Returns
//
// Scenario: When a savings balance earns on previous interest
// Rule: FV = P(1+r)ⁿ
//
// Try it: Why is year-two interest 52.50 rather than 50?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/compound-interest/
// Free course: https://courses.thefintechbuilder.com
// Synthetic teaching example, not financial advice or a production library.
//
// Run it: go run main.go
package main
import (
"encoding/json"
"fmt"
"math"
)
func lesson012() []float64 {
principal, rate := 1000.0, 0.05
balances := make([]float64, 4)
for t := range balances {
balances[t] = principal * math.Pow(1+rate, float64(t))
}
result := balances
return result
}
func main() {
printJSON(lesson012())
}
// printJSON prints a value as JSON indented with two spaces, like JSON.stringify(value, null, 2).
func printJSON(value any) {
out, err := json.MarshalIndent(value, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(out))
}
No browser runner for Go yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
[
1000,
1050,
1102.5,
1157.6250000000002
]/**
* Fintech Math Bootcamp · Lesson 012 of 120
* Compound Interest
* Module 02: Financial Arithmetic, Time Value, and Returns
*
* Scenario: When a savings balance earns on previous interest
* Rule: FV = P(1+r)ⁿ
*
* Try it: Why is year-two interest 52.50 rather than 50?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/compound-interest/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: g++ -std=c++17 -o main main.cpp && ./main
*/
#include <charconv>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
// Formats a double the way JSON.stringify does: shortest round-trip form, null for NaN or infinity.
std::string num(double x) {
if (!std::isfinite(x)) return "null";
char buffer[32];
auto [end, error] = std::to_chars(buffer, buffer + sizeof buffer, x);
(void)error;
return std::string(buffer, end);
}
// Indents an already formatted JSON value by one level.
std::string nested(const std::string& json) {
std::string out;
for (char c : json) {
out += c;
if (c == '\n') out += " ";
}
return out;
}
std::string array(const std::vector<std::string>& items) {
if (items.empty()) return "[]";
std::string out = "[";
for (std::size_t i = 0; i < items.size(); ++i) {
out += i == 0 ? "\n " : ",\n ";
out += nested(items[i]);
}
return out + "\n]";
}
std::string numbers(const std::vector<double>& values) {
std::vector<std::string> items;
for (double value : values) items.push_back(num(value));
return array(items);
}
std::vector<double> lesson012() {
const double principal = 1000, rate = 0.05;
std::vector<double> balances(4);
for (std::size_t t = 0; t < balances.size(); ++t) {
balances[t] = principal * std::pow(1 + rate, static_cast<double>(t));
}
const std::vector<double> result = balances;
return result;
}
int main() {
std::cout << numbers(lesson012()) << "\n";
}
No browser runner for C++ yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
[
1000,
1050,
1102.5,
1157.6250000000002
]//! Fintech Math Bootcamp · Lesson 012 of 120
//! Compound Interest
//! Module 02: Financial Arithmetic, Time Value, and Returns
//!
//! Scenario: When a savings balance earns on previous interest
//! Rule: FV = P(1+r)ⁿ
//!
//! Try it: Why is year-two interest 52.50 rather than 50?
//!
//! Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/compound-interest/
//! Free course: https://courses.thefintechbuilder.com
//! Synthetic teaching example, not financial advice or a production library.
//!
//! Run it: rustc main.rs && ./main
fn lesson012() -> Vec<f64> {
let (principal, rate): (f64, f64) = (1000.0, 0.05);
let balances: Vec<f64> = (0..4)
.map(|t| principal * (1.0 + rate).powf(t as f64))
.collect();
let result = balances;
result
}
fn main() {
println!("{}", numbers(&lesson012()));
}
/// Formats a number the way JSON.stringify does: shortest round-trip form, null for NaN or infinity.
fn num(x: f64) -> String {
if x.is_finite() {
format!("{}", x)
} else {
"null".to_string()
}
}
/// Indents an already formatted JSON value by one level.
fn nested(json: &str) -> String {
json.replace('\n', "\n ")
}
fn array(items: &[String]) -> String {
if items.is_empty() {
return "[]".to_string();
}
let lines: Vec<String> = items.iter().map(|item| format!(" {}", nested(item))).collect();
format!("[\n{}\n]", lines.join(",\n"))
}
fn numbers(values: &[f64]) -> String {
let items: Vec<String> = values.iter().map(|&value| num(value)).collect();
array(&items)
}
No browser runner for Rust yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
[
1000,
1050,
1102.5,
1157.6250000000002
]/**
* Fintech Math Bootcamp · Lesson 012 of 120
* Compound Interest
* Module 02: Financial Arithmetic, Time Value, and Returns
*
* Scenario: When a savings balance earns on previous interest
* Rule: FV = P(1+r)ⁿ
*
* Try it: Why is year-two interest 52.50 rather than 50?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-arithmetic-time-value-and-returns/compound-interest/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: dotnet run (inside a console project that holds this Program.cs)
*/
using System.Text.Json;
var jsonOptions = new JsonSerializerOptions { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
Console.WriteLine(JsonSerializer.Serialize(Lesson012(), jsonOptions));
static double[] Lesson012()
{
double principal = 1000, rate = 0.05;
var balances = Enumerable.Range(0, 4)
.Select(t => principal * Math.Pow(1 + rate, t))
.ToArray();
var result = balances;
return result;
}
No browser runner for C# yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
[
1000,
1050,
1102.5,
1157.6250000000002
]Prefer your own machine? Every file is in the course repository · open it in Codespaces.
Lesson notes
The rule
FV = P(1+r)ⁿ