Skip to content

Easy Math

py_simple.easy_math

Beginner-friendly helpers for common math operations.

calculate_simple_interest(principal, rate, time)

Returns the simple interest earned on a principal amount.

Parameters:

Name Type Description Default
principal float

The initial amount of money.

required
rate float

The annual interest rate (as a percentage, e.g., 5 for 5%).

required
time float

The time the money is invested or borrowed for, in years.

required

Returns:

Name Type Description
float float

The calculated simple interest amount.

Raises:

Type Description
ValueError

If any argument is not a number (int or float).

Example
from py_simple import calculate_simple_interest

result = calculate_simple_interest(1000, 5, 2)  # -> 100.0
principal, rate, time = 1000, 5, 2
result = (principal * rate * time) / 100

collatz_sequence(n)

Returns the Collatz conjecture sequence for a given positive integer.

The sequence starts with n. If n is even, the next number is n / 2. If n is odd, the next number is 3 * n + 1. This repeats until n reaches 1.

Parameters:

Name Type Description Default
n int

Positive integer to start the sequence.

required

Returns:

Type Description
list[int]

list[int]: The Collatz sequence ending in 1.

Raises:

Type Description
ValueError

If n is less than 1 or not an integer.

Example
from py_simple import collatz_sequence

result = collatz_sequence(6)  # -> [6, 3, 10, 5, 16, 8, 4, 2, 1]
n = 6
sequence = [n]
while n != 1:
    n = n // 2 if n % 2 == 0 else 3 * n + 1
    sequence.append(n)

digit_count(n)

Returns the total number of digits in an integer.

The negative sign is ignored when counting digits.

Parameters:

Name Type Description Default
n int

Integer to count digits for.

required

Returns:

Name Type Description
int int

The number of digits.

Raises:

Type Description
ValueError

If n is not an integer.

Example
from py_simple import digit_count

result = digit_count(-1234)  # -> 4
n = -1234
result = len(str(abs(n)))

distance_between_points(x1, y1, x2, y2)

Returns the Euclidean distance between two 2D points.

Parameters:

Name Type Description Default
x1 float

X coordinate of the first point.

required
y1 float

Y coordinate of the first point.

required
x2 float

X coordinate of the second point.

required
y2 float

Y coordinate of the second point.

required

Returns:

Name Type Description
float float

The distance between the two points.

Example
from py_simple import distance_between_points

result = distance_between_points(0, 0, 3, 4)  # -> 5.0
import math

result = math.sqrt((3 - 0)**2 + (4 - 0)**2)

divisors(n)

Returns every positive integer that divides n evenly.

Parameters:

Name Type Description Default
n int

Positive integer to divide.

required

Returns:

Name Type Description
list list

All divisors of n.

Raises:

Type Description
ValueError

If n is less than 1.

Example
from py_simple import divisors

result = divisors(12)  # -> [1, 2, 3, 4, 6, 12]
n = 12
result = [number for number in range(1, n + 1) if n % number == 0]

factorial(n)

Returns the factorial of a whole number.

Parameters:

Name Type Description Default
n int

Non-negative integer.

required

Returns:

Name Type Description
int int

The factorial of n.

Raises:

Type Description
ValueError

If n is negative or not a whole number.

Example
from py_simple import factorial

result = factorial(5)  # -> 120
n = 5
result = 1
for number in range(2, n + 1):
    result *= number

fibonacci(count)

Returns the first count Fibonacci numbers as a list.

Parameters:

Name Type Description Default
count int

How many Fibonacci numbers to return.

required

Returns:

Name Type Description
list list

The first count Fibonacci numbers.

Raises:

Type Description
ValueError

If count is less than 1.

Example
from py_simple import fibonacci

result = fibonacci(5)  # -> [0, 1, 1, 2, 3]
count = 5
sequence = [0, 1]
while len(sequence) < count:
    sequence.append(sequence[-1] + sequence[-2])
result = sequence[:count]

get_least_common_multiple(a, b)

Returns the least common multiple of two integers.

The result is always positive, and 0 is returned if either number is 0.

Parameters:

Name Type Description Default
a int

First number.

required
b int

Second number.

required

Returns:

Name Type Description
int int

The least common multiple.

Example
from py_simple import lcm

result = lcm(4, 6)  # -> 12
import math
from math import gcd

a, b = 4, 6
result = abs(a * b) // gcd(a, b)

is_abundant_number(n)

Returns whether a positive integer is an abundant number.

An abundant number is one where the sum of its proper divisors (excluding the number itself) is greater than the number. For example, 12 is abundant because 1 + 2 + 3 + 4 + 6 = 16 > 12.

Parameters:

Name Type Description Default
n int

Positive integer to check.

required

Returns:

Name Type Description
bool bool

True if n is an abundant number, otherwise False.

Raises:

Type Description
ValueError

If n is less than 1 or not an integer.

Example
from py_simple import is_abundant_number

result = is_abundant_number(12)  # -> True
n = 12
proper_divisors = [i for i in range(1, n) if n % i == 0]
result = sum(proper_divisors) > n

is_armstrong_number(n)

Returns whether a non-negative integer is an Armstrong number.

An Armstrong number (or narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

Parameters:

Name Type Description Default
n int

Non-negative integer to check.

required

Returns:

Name Type Description
bool bool

True if n is an Armstrong number, otherwise False.

Raises:

Type Description
ValueError

If n is negative or not an integer.

Example
from py_simple import is_armstrong_number

result = is_armstrong_number(153)  # -> True
n = 153
num_str = str(n)
num_digits = len(num_str)
result = sum(int(digit) ** num_digits for digit in num_str) == n

is_harshad_number(n)

Returns whether a positive integer is a Harshad (or Niven) number.

A Harshad number is an integer that is divisible by the sum of its digits. For example, 18 is a Harshad number because 1 + 8 = 9, and 18 % 9 == 0.

Parameters:

Name Type Description Default
n int

Positive integer to check.

required

Returns:

Name Type Description
bool bool

True if n is a Harshad number, otherwise False.

Raises:

Type Description
ValueError

If n is less than 1 or not an integer.

Example
from py_simple import is_harshad_number

result = is_harshad_number(18)  # -> True
n = 18
result = n % sum(int(d) for d in str(n)) == 0

is_perfect_square(n)

Returns whether a non-negative integer is a perfect square.

A perfect square is an integer that can be written as another integer multiplied by itself, such as 0, 1, 4, 9, or 16.

Parameters:

Name Type Description Default
n int

Non-negative integer to check.

required

Returns:

Name Type Description
bool bool

True if n is a perfect square, otherwise False.

Raises:

Type Description
ValueError

If n is negative or not an integer.

Example
from py_simple import is_perfect_square

result = is_perfect_square(49)  # -> True
import math

n = 49
root = math.isqrt(n)
result = root * root == n

is_triangular_number(n)

Returns whether a non-negative integer is a triangular number.

A triangular number counts objects arranged in an equilateral triangle. For example, 6 is triangular because it can be arranged as 1 + 2 + 3.

Parameters:

Name Type Description Default
n int

Non-negative integer to check.

required

Returns:

Name Type Description
bool bool

True if n is a triangular number, otherwise False.

Raises:

Type Description
ValueError

If n is negative or not an integer.

Example
from py_simple import is_triangular_number

result = is_triangular_number(10)  # -> True
n = 10
k = int((2 * n) ** 0.5)
result = k * (k + 1) // 2 == n

midpoint(x1, y1, x2, y2)

Returns the exact midpoint between two 2D coordinates.

Parameters:

Name Type Description Default
x1 float

X coordinate of the first point.

required
y1 float

Y coordinate of the first point.

required
x2 float

X coordinate of the second point.

required
y2 float

Y coordinate of the second point.

required

Returns:

Type Description
tuple[float, float]

tuple[float, float]: The (x, y) coordinates of the midpoint.

Example
from py_simple import midpoint

result = midpoint(0, 0, 4, 6)  # -> (2.0, 3.0)
result = ((0 + 4) / 2, (0 + 6) / 2)

prime_factorization(n)

Returns the prime factors of a positive integer, including repeats.

Parameters:

Name Type Description Default
n int

Positive integer to factor.

required

Returns:

Name Type Description
list list

Prime factors of n.

Raises:

Type Description
ValueError

If n is less than 1.

Example
from py_simple import prime_factorization

result = prime_factorization(12)  # -> [2, 2, 3]
n = 12
factors = []
divisor = 2
while divisor * divisor <= n:
    while n % divisor == 0:
        factors.append(divisor)
        n //= divisor
    divisor += 1
if n > 1:
    factors.append(n)

reverse_digits(n)

Returns an integer with its digits reversed, preserving the sign.

Parameters:

Name Type Description Default
n int

Integer to reverse.

required

Returns:

Name Type Description
int int

The reversed integer.

Raises:

Type Description
ValueError

If n is not an integer.

Example
from py_simple import reverse_digits

result = reverse_digits(-123)  # -> -321
n = -123
result = int(str(n)[::-1]) * (-1 if n < 0 else 1)

sum_of_digits(n)

Returns the sum of the digits of an integer.

Negative numbers are handled by ignoring the minus sign.

Parameters:

Name Type Description Default
n int

Integer to add up.

required

Returns:

Name Type Description
int int

The sum of the digits.

Raises:

Type Description
ValueError

If n is not an integer.

Example
from py_simple import sum_of_digits

result = sum_of_digits(1234)  # -> 10
n = 1234
result = sum(int(digit) for digit in str(n))

sum_of_squares(numbers)

Returns the sum of the squared values in a list of numbers.

Parameters:

Name Type Description Default
numbers list[float]

A list of numeric values.

required

Returns:

Name Type Description
float float

The sum of the squares.

Raises:

Type Description
ValueError

If the input is not a list.

Example
from py_simple import sum_of_squares

result = sum_of_squares([1, 2, 3])  # -> 14.0
numbers = [1, 2, 3]
result = sum(x**2 for x in numbers)