Skip to content
Kittycat.tech logo

Algebra Formulas in LaTeX, Python and Rust

Contributors:
Erica Madebeykin, MBA
Erica Madebeykin, MBA
🇬🇧MBA Merit
🇬🇧MSc Comp Sci student
Start Creation Date: October 20, 2024 🔄 Continuously updated

✅ 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

Just click the copy button in the top right corner of each code block!

Summary Overview ​

#FormulaExample
1Exponent Laws23⋅22=32
2Logarithm Ruleslog2⁡32=log2⁡8+log2⁡4=5
3Change of Base Formulalog2⁡8=3
4Quadratic Formulax2−5x+6=0⇒x=3,2
5Slope and Equation of a Linethrough (1,2) and (3,8): m=3
6Distance and Midpointfrom (0,0) to (3,4): d=5
7Arithmetic Sequencea1=2,d=3: a10=29, S10=155
8Geometric Sequencea1=3,r=2: a5=48, S5=93
9Compound Interest1000 at 5% monthly for 10 years: 1647.01

1. Exponent Laws ​

Exponent Laws

Formula:

am⋅an=am+n(am)n=amna−n=1an

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.

markdown
$$
a^{m} \cdot a^{n} = a^{m+n} \qquad (a^{m})^{n} = a^{mn} \qquad a^{-n} = \frac{1}{a^{n}}
$$
python
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.25
rust
fn 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:

logb⁡(xy)=logb⁡x+logb⁡ylogb⁡(xy)=logb⁡x−logb⁡ylogb⁡(xk)=klogb⁡x

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.

markdown
$$
\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
$$
python
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.0
rust
fn 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:

logb⁡a=logk⁡alogk⁡b

Any base k works, so the code uses the natural log. Rust also has a built-in shortcut: a.log(b).

Where it's used: calculating logarithms in any base, such as log2 in computer science.

markdown
$$
\log_b a = \frac{\log_k a}{\log_k b}
$$
python
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.0
rust
fn 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:

ax2+bx+c=0⇒x=−b±b2−4ac2a

The discriminant b2−4ac tells you how many real roots there are. If it is negative, there are none.

Where it's used: solving quadratic equations, such as x2−5x+6=0, which has roots 3 and 2.

markdown
$$
ax^2 + bx + c = 0 \quad \Rightarrow \quad x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
$$
python
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))   # None
rust
fn 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:

m=y2−y1x2−x1y=mx+c

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.

markdown
$$
m = \frac{y_2 - y_1}{x_2 - x_1} \qquad y = mx + c
$$
python
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.0
rust
fn 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:

d=(x2−x1)2+(y2−y1)2M=(x1+x22,y1+y22)

Where it's used: measuring how far apart two points are, which is the idea behind k-nearest neighbours.

markdown
$$
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)
$$
python
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)
rust
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:

an=a1+(n−1)dSn=n2(2a1+(n−1)d)

d is the common difference added each time.

Where it's used: savings that grow by the same amount each month, and counting steps in loops.

markdown
$$
a_n = a_1 + (n - 1)d \qquad S_n = \frac{n}{2} \big( 2a_1 + (n - 1)d \big)
$$
python
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.0
rust
fn 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:

an=a1rn−1Sn=a11−rn1−r,r≠1

r is the common ratio multiplied each time.

Where it's used: exponential growth and decay, and discounted rewards in reinforcement learning.

markdown
$$
a_n = a_1 r^{n-1} \qquad S_n = a_1 \frac{1 - r^{n}}{1 - r}, \quad r \neq 1
$$
python
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.0
rust
fn 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:

A=P(1+rk)kt

P is the starting amount, r the yearly rate, k the number of times interest is added per year, and t the number of years.

Where it's used: savings and loans, a real-life use of exponents.

markdown
$$
A = P \left( 1 + \frac{r}{k} \right)^{kt}
$$
python
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.01
rust
fn 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
}

✅Study notes based on Kittycat's insights from the best online courses at top universities