Learn how to perform basic mathematical operations in programming, including square roots, prime numbers, factors, greatest common divisors, least common multiples, and modular arithmetic.
Written by: Brianna Laird Last updated: December 2024
Mathematics is a fundamental part of programming, providing essential tools and methods that enable developers to solve complex problems efficiently. This guide explores a range of mathematical operations and demonstrates how to implement them effectively. In later tutorials, you’ll learn how to integrate these operations into your SplashKit projects to create programs, games and encrypted messaging applications.
Basic Maths Operations
This section introduces fundamental mathematical operations that are essential for programming. We’ll explore various mathematical functions and algorithms, starting with basic calculations like square roots and progressing to more complex operations like finding prime numbers and calculating greatest common divisors, least common multiples, and finding factors.
Square Root
The square root of a number is a value that, when multiplied by itself, gives the original number. For example, the square root of is since .
To calculate the square root efficiently, we use the Newton-Raphson method, an iterative numerical technique for finding roots of real-valued functions. This method starts with an initial estimate and repeatedly refines it using the formula:
After running the above code, the expected output should be:
If we swapped the value to be instead of , the output would then be:
Prime Number
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. To check if a number is prime, you will need to also calculate the square root of the number. You can use the following functions to check if a number is prime:
To check if a number is prime, we can use a simple algorithm that checks if the number is divisible by any number from 2 to its square root. The logic behind this approach is based on the following mathematical principles:
Any non-prime number can be written as a product of two factors:
If is not prime, at least one of these factors must be less than or equal to
If we can’t find any factors up to , then the number must be prime
After running the above code, the expected output should be:
If we swapped the value to be instead of , the output would then be:
Finding Prime Numbers up to a Given Number
Finding prime numbers up to a given number is a common task in number theory and cryptography. The following function implements the Sieve of Eratosthenes algorithm, which efficiently finds all prime numbers up to a specified maximum . The algorithm works by:
Creating a list of numbers from 2 to
Starting with (the first prime), marking all multiples of up to as composite
Finding the next unmarked number and repeating step 2
The remaining unmarked numbers are prime
This can be expressed mathematically as finding all numbers where:
For example, to find primes up to 20, we would mark off:
After running the above code, the expected output should be:
Finding the Number of Divisors
The number of divisors of a positive integer is the count of positive integers that divide without leaving a remainder. We can find the number of divisors efficiently by only checking up to because divisors come in pairs.
For any divisor of , there exists a corresponding divisor such that . For example, for :
When , we get
When , we get
When , we get
Therefore, except for perfect squares where , divisors occur in pairs. The total count can be calculated by:
After running the above code, the expected output should be:
If we swapped the value to be instead of , the output would then be:
Finding the Sum of Divisors
The sum of divisors of a positive integer is the sum of all positive integers that divide the number without leaving a remainder. This function calculates the sum of divisors of a given number by iterating through all numbers up to the square root of the input number and summing the divisors.
For a number , finding the sum of its divisors involves:
After running the above code, the expected output should be:
If we swapped the value to be instead of , the output would then be:
Greatest Common Divisor
The greatest common divisor (GCD) of two numbers is the largest positive integer that divides both numbers without leaving a remainder. The algorithm works by repeatedly dividing the larger number by the smaller number and replacing the larger number with the remainder until the remainder is zero. The last non-zero remainder is the GCD of the two numbers. This algorithm is known as the Euclidean algorithm which can be expressed mathematically as:
where represents the remainder when is divided by .
You can use the following functions to calculate the GCD of two numbers:
After running the above code, the expected output should be:
If we swapped the value to be instead of , the output would then be:
Least Common Multiple
The least common multiple (LCM) of two numbers is the smallest positive integer that is divisible by both of those numbers. An efficient way to calculate the LCM is by using the relationship between the LCM and the greatest common divisor (GCD) of those numbers. The formula for finding the LCM using the GCD is:
After running the above code, the expected output should be:
If we swapped the value to be instead of , the output would then be:
Checking for Coprime Numbers
Two numbers are coprime (or relatively prime) if their greatest common divisor (GCD) is . For example, and are coprime because their GCD is , even though neither number is prime.
After running the above code, the expected output should be:
If we swapped the value to be instead of , the output would then be:
Finding All Coprime Numbers
Finding all numbers that are coprime to a given value is useful in many mathematical applications, particularly in cryptography and number theory. The following function identifies all numbers that are coprime to an input value up to a specified maximum. Two numbers are coprime if their greatest common divisor (GCD) is .
For example, if we want to find all numbers coprime to up to , we would get:
You can use the find_coprimes_up_to function to find all numbers coprime to a given value up to a specified maximum. For example, to find all numbers coprime to up to :
After running the above code, the expected output should be:
If we swapped the value to be instead of , the output would then be:
Eulers Totient Function
Euler’s totient function, denoted as , is a mathematical function that counts the number of positive integers less than or equal to that are coprime to . For example, because the numbers and are coprime to .
The totient function can be calculated using the formula:
After running the above code, the expected output should be:
If we swapped the value to be instead of , the output would then be:
Finding Factors
A factor is a number that divides evenly into another number with no remainder. For example, the factors of are and . Finding factors is useful in various mathematical applications, including:
Simplifying fractions
Finding common divisors
Breaking down numbers into their prime factorisation
The following function finds all factors of a given number by checking all potential divisors up to the input number:
After running the above code, the expected output should be:
If we swapped the value to be instead of , the output would then be:
Finding Prime Factors
The prime factorisation of a number is its decomposition into a product of prime numbers. For example, can be written as . The following function identifies all prime factors of a given number by:
First extracting all factors of (the smallest prime number)
Then checking odd numbers up to the square root for remaining factors
If any number remains after this process, it must be a prime factor itself
After running the above code, the expected output should be:
If we swapped the value to be instead of , the output would then be:
Modular Arithmetic Operations
Modular arithmetic is a system of arithmetic for integers, where numbers “wrap around” upon reaching a certain value—the modulus. This section includes functions for modular addition, subtraction, multiplication, and exponentiation. The function modular_exponentiation performs modular exponentiation, which raises a base to an exponent under a modulus efficiently. This operation is commonly used in cryptography and number theory.
Modular Addition
Modular addition is the operation of adding two numbers and taking the remainder when divided by a modulus. This operation is useful in many applications, including cryptography and computer science.
Mathematically, modular addition can be expressed as:
where:
and are the numbers to be added
is the modulus
denotes the modulo operation, which finds the remainder after division of one number by another
For example, if we want to add and with a modulus of , we calculate:
This means that and add up to , and when we take the remainder after dividing by , we get .
After running the above code, the expected output should be:
If we swapped the value to be instead of , the output would then be:
Modular Subtraction
Modular subtraction is the operation of subtracting two numbers and taking the remainder when divided by a modulus. This operation is useful in many applications, including cryptography and computer science.
Mathematically, modular subtraction can be expressed as:
where:
and are the numbers to be subtracted
is the modulus
denotes the modulo operation, which finds the remainder after division of one number by another
For example, if we want to subtract from with a modulus of , we calculate:
This means that minus is , and when we take the remainder after dividing by , we get .
After running the above code, the expected output should be:
If we swapped the value to be instead of , the output would then be:
Modular Multiplication
Modular multiplication is the operation of multiplying two numbers and taking the remainder when divided by a modulus. This operation is useful in many applications, including cryptography and computer science.
Mathematically, modular multiplication can be expressed as:
where:
and are the numbers to be multiplied
is the modulus
denotes the modulo operation, which finds the remainder after division of one number by another
For example, if we want to multiply and with a modulus of , we calculate:
This means that multiplied by is , and when we take the remainder after dividing by , we get .
After running the above code, the expected output should be:
If we swapped the value to be instead of , the output would then be:
Modular Exponentiation
Modular exponentiation is the operation of raising a base to an exponent and taking the remainder when divided by a modulus. This operation is useful in many applications, including cryptography and computer science.
Mathematically, modular exponentiation can be expressed as:
where:
is the number to be raised to the power
is the power to which the base is raised
is the modulus
denotes the modulo operation, which finds the remainder after division of one number by another
For example, if we want to calculate with a modulus of , we calculate:
This means that raised to the power of is , and when we take the remainder after dividing by , we get .
You can use the modular_exponentiation function to perform modular exponentiation of a base to an exponent. For example, to calculate with a modulus of :
After running the above code, the expected output should be:
If we swapped the value to be instead of , the output would then be:
Summary
This guide covered various mathematical utilities and functions that are commonly used in programming and discrete mathematics. You learned how to implement functions for finding the greatest common divisor (GCD), least common multiple (LCM), coprime numbers, factors, prime factors, and modular arithmetic operations. These functions are essential for solving mathematical problems, cryptography, and number theory. By understanding and implementing these functions, you can enhance your programming skills and solve a wide range of mathematical problems efficiently. You can use this knowledge to build more complex algorithms and applications that require mathematical computations.