Algebra Formulas in LaTeX, Python and Rust


✅ Copy-ready code for algebra formulas in LaTeX, Python and Rust. For each formula, you'll see how to write it in LaTeX and how to calculate it in code. 😻
Every snippet runs on its own with no extra libraries, and the expected output is written in the comments. Use the tabs to switch between LaTeX, Python and Rust. To run a Rust snippet, save it as main.rs and run rustc main.rs, or paste it into the Rust Playground.
📚 Math Formulas in Code series
- Part 1: Algebra Formulas (you are here): exponents, logarithms, quadratics, lines and sequences
- Part 2: Linear Algebra Formulas: vectors, matrices and a neural network layer
- Part 3: Descriptive Statistics Formulas: mean, median, spread and correlation
- Part 4: Inferential Statistics Formulas: tests, confidence intervals, probability and regression
- Part 5: Calculus Formulas: derivatives, integrals and numerical methods
Just click the copy button in the top right corner of each code block!
Summary Overview
| # | Formula | Example |
|---|---|---|
| 1 | Exponent Laws | |
| 2 | Logarithm Rules | |
| 3 | Change of Base Formula | |
| 4 | Quadratic Formula | |
| 5 | Slope and Equation of a Line | through |
| 6 | Distance and Midpoint | from |
| 7 | Arithmetic Sequence | |
| 8 | Geometric Sequence | |
| 9 | Compound Interest | 1000 at 5% monthly for 10 years: 1647.01 |
1. Exponent Laws
Exponent Laws
Formula:
In Python, ** raises to a power. In Rust, integers use .pow() and decimals use .powi().
Where it's used: simplifying expressions, scientific notation, and growth models.
$$
a^{m} \cdot a^{n} = a^{m+n} \qquad (a^{m})^{n} = a^{mn} \qquad a^{-n} = \frac{1}{a^{n}}
$$a, m, n = 2, 3, 2
print(a**m * a**n, a**(m + n)) # 32 32
print((a**m)**n, a**(m * n)) # 64 64
print(a**-n, 1 / a**n) # 0.25 0.25fn main() {
let (a, m, n): (i64, u32, u32) = (2, 3, 2);
println!("{} {}", a.pow(m) * a.pow(n), a.pow(m + n)); // 32 32
println!("{} {}", a.pow(m).pow(n), a.pow(m * n)); // 64 64
let af = a as f64;
println!("{} {}", af.powi(-(n as i32)), 1.0 / af.powi(n as i32)); // 0.25 0.25
}2. Logarithm Rules
Logarithm Rules
Formula:
Both languages have log2, log10 and the natural log built in.
Where it's used: simplifying logarithms, and turning products into sums, as in log-likelihood.
$$
\log_b(xy) = \log_b x + \log_b y \qquad \log_b \left( \frac{x}{y} \right) = \log_b x - \log_b y \qquad \log_b (x^{k}) = k \log_b x
$$import math
x, y, k = 8, 4, 3
print(math.log2(x * y), math.log2(x) + math.log2(y)) # 5.0 5.0
print(math.log2(x / y), math.log2(x) - math.log2(y)) # 1.0 1.0
print(math.log2(x**k), k * math.log2(x)) # 9.0 9.0fn main() {
let (x, y, k): (f64, f64, i32) = (8.0, 4.0, 3);
println!("{} {}", (x * y).log2(), x.log2() + y.log2()); // 5 5
println!("{} {}", (x / y).log2(), x.log2() - y.log2()); // 1 1
println!("{} {}", x.powi(k).log2(), k as f64 * x.log2()); // 9 9
}3. Change of Base Formula
Change of Base Formula
Formula:
Any base a.log(b).
Where it's used: calculating logarithms in any base, such as
$$
\log_b a = \frac{\log_k a}{\log_k b}
$$import math
def log_base(a: float, b: float) -> float:
return math.log(a) / math.log(b)
print(round(log_base(8, 2), 4)) # 3.0
print(round(log_base(100, 10), 4)) # 2.0fn log_base(a: f64, b: f64) -> f64 {
a.ln() / b.ln()
}
fn main() {
println!("{:.4}", log_base(8.0, 2.0)); // 3.0000
println!("{:.4}", log_base(100.0, 10.0)); // 2.0000
}4. Quadratic Formula
Quadratic Formula
Formula:
The discriminant
Where it's used: solving quadratic equations, such as
$$
ax^2 + bx + c = 0 \quad \Rightarrow \quad x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
$$import math
def quadratic(a: float, b: float, c: float):
d = b**2 - 4*a*c # discriminant
if d < 0:
return None # no real roots
root = math.sqrt(d)
return (-b + root) / (2*a), (-b - root) / (2*a)
print(quadratic(1, -5, 6)) # (3.0, 2.0)
print(quadratic(1, 0, 1)) # Nonefn quadratic(a: f64, b: f64, c: f64) -> Option<(f64, f64)> {
let d = b * b - 4.0 * a * c; // discriminant
if d < 0.0 {
return None; // no real roots
}
let root = d.sqrt();
Some(((-b + root) / (2.0 * a), (-b - root) / (2.0 * a)))
}
fn main() {
println!("{:?}", quadratic(1.0, -5.0, 6.0)); // Some((3.0, 2.0))
println!("{:?}", quadratic(1.0, 0.0, 1.0)); // None
}5. Slope and Equation of a Line
Slope and Equation of a Line
Formula:
Rust prints whole-number decimals without the .0, so 3.0 shows as 3.
Where it's used: finding the gradient between two points, and the starting point of linear regression.
$$
m = \frac{y_2 - y_1}{x_2 - x_1} \qquad y = mx + c
$$def slope(x1: float, y1: float, x2: float, y2: float) -> float:
return (y2 - y1) / (x2 - x1)
m = slope(1, 2, 3, 8)
c = 2 - m * 1 # intercept, using the point (1, 2)
print(m, c) # 3.0 -1.0fn slope(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 {
(y2 - y1) / (x2 - x1)
}
fn main() {
let m = slope(1.0, 2.0, 3.0, 8.0);
let c = 2.0 - m * 1.0; // intercept, using the point (1, 2)
println!("{} {}", m, c); // 3 -1
}6. Distance and Midpoint
Distance and Midpoint
Formula:
Where it's used: measuring how far apart two points are, which is the idea behind k-nearest neighbours.
$$
d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} \qquad M = \left( \frac{x_1 + x_2}{2}, \; \frac{y_1 + y_2}{2} \right)
$$import math
def distance(x1, y1, x2, y2):
return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
def midpoint(x1, y1, x2, y2):
return (x1 + x2) / 2, (y1 + y2) / 2
print(distance(0, 0, 3, 4)) # 5.0
print(midpoint(0, 0, 3, 4)) # (1.5, 2.0)fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 {
((x2 - x1).powi(2) + (y2 - y1).powi(2)).sqrt()
}
fn midpoint(x1: f64, y1: f64, x2: f64, y2: f64) -> (f64, f64) {
((x1 + x2) / 2.0, (y1 + y2) / 2.0)
}
fn main() {
println!("{}", distance(0.0, 0.0, 3.0, 4.0)); // 5
println!("{:?}", midpoint(0.0, 0.0, 3.0, 4.0)); // (1.5, 2.0)
}7. Arithmetic Sequence
Arithmetic Sequence
Formula:
Where it's used: savings that grow by the same amount each month, and counting steps in loops.
$$
a_n = a_1 + (n - 1)d \qquad S_n = \frac{n}{2} \big( 2a_1 + (n - 1)d \big)
$$def nth_term(a1: int, d: int, n: int) -> int:
return a1 + (n - 1) * d
def arithmetic_sum(a1: int, d: int, n: int) -> float:
return n * (2 * a1 + (n - 1) * d) / 2
print(nth_term(2, 3, 10)) # 29
print(arithmetic_sum(2, 3, 10)) # 155.0fn nth_term(a1: i64, d: i64, n: i64) -> i64 {
a1 + (n - 1) * d
}
fn arithmetic_sum(a1: i64, d: i64, n: i64) -> i64 {
n * (2 * a1 + (n - 1) * d) / 2
}
fn main() {
println!("{}", nth_term(2, 3, 10)); // 29
println!("{}", arithmetic_sum(2, 3, 10)); // 155
}8. Geometric Sequence
Geometric Sequence
Formula:
Where it's used: exponential growth and decay, and discounted rewards in reinforcement learning.
$$
a_n = a_1 r^{n-1} \qquad S_n = a_1 \frac{1 - r^{n}}{1 - r}, \quad r \neq 1
$$def geometric_term(a1: float, r: float, n: int) -> float:
return a1 * r**(n - 1)
def geometric_sum(a1: float, r: float, n: int) -> float:
return a1 * (1 - r**n) / (1 - r)
print(geometric_term(3, 2, 5)) # 48
print(geometric_sum(3, 2, 5)) # 93.0fn geometric_term(a1: f64, r: f64, n: i32) -> f64 {
a1 * r.powi(n - 1)
}
fn geometric_sum(a1: f64, r: f64, n: i32) -> f64 {
a1 * (1.0 - r.powi(n)) / (1.0 - r)
}
fn main() {
println!("{}", geometric_term(3.0, 2.0, 5)); // 48
println!("{}", geometric_sum(3.0, 2.0, 5)); // 93
}9. Compound Interest
Compound Interest
Formula:
Where it's used: savings and loans, a real-life use of exponents.
$$
A = P \left( 1 + \frac{r}{k} \right)^{kt}
$$def compound(P: float, r: float, k: int, t: float) -> float:
return P * (1 + r / k) ** (k * t)
print(round(compound(1000, 0.05, 12, 10), 2)) # 1647.01fn compound(p: f64, r: f64, k: f64, t: f64) -> f64 {
p * (1.0 + r / k).powf(k * t)
}
fn main() {
println!("{:.2}", compound(1000.0, 0.05, 12.0, 10.0)); // 1647.01
}