Project Report
Project Report
Report
1. Introduction
This project provides a MATLAB implementation for finding prime numbers using various
algorithms. The goal is to offer a flexible and efficient set of tools for both primality
testing of individual numbers and generating lists of primes within a specified range.
2. Project Structure
• primeFinder.m : The main entry point for the prime number finder. It acts as a
dispatcher, calling the appropriate algorithm based on user input.
• is_prime_trial_division.m : Implements the trial division algorithm for
primality testing.
• sieve_of_eratosthenes.m : Implements the Sieve of Eratosthenes algorithm
for generating primes up to a given limit.
• is_prime_miller_rabin.m : Implements the Miller-Rabin probabilistic primality
test.
• test_prime_finder.m : A unit test script to verify the correctness of the
implemented functions.
• example_usage.m : A script demonstrating how to use the primeFinder
function and its underlying algorithms.
3. Algorithms Implemented
Description: Trial division is the simplest and most intuitive method for primality
testing. To check if a number n is prime, it attempts to divide n by every integer from 2
up to the square root of n . If any of these divisions result in a zero remainder, n is
composite; otherwise, it is prime.
Algorithm Steps: 1. Handle base cases: Numbers less than or equal to 1 are not prime. 2
and 3 are prime. 2. Check divisibility by 2 and 3: If n is divisible by 2 or 3, it's not prime.
3. Iterate from 5: For numbers greater than 3, check divisors of the form 6k ± 1 up to
sqrt(n) . This optimization skips multiples of 2 and 3.
Description: The Sieve of Eratosthenes is an ancient algorithm for finding all prime
numbers up to a specified integer. It works by iteratively marking as composite (i.e., not
prime) the multiples of each prime, starting with the first prime number, 2.
Algorithm Steps: 1. Create a boolean list isPrime for numbers from 1 to limit ,
initially marking all as true. 2. Mark 1 as not prime. 3. Iterate from p = 2 up to
sqrt(limit) : a. If p is marked as prime, then all multiples of p (starting from p*p )
are marked as not prime. 4. The numbers remaining marked as true are prime.
Algorithm Steps: 1. Handle base cases: Numbers less than 2 are not prime. 2 and 3 are
prime. Even numbers greater than 2 are not prime. 2. Express n-1 as 2^s * d , where
d is an odd integer. 3. Repeat k times (where k is the number of iterations, higher k
means higher accuracy): a. Choose a random integer a in the range [2, n-2] . b.
Compute x = a^d mod n . c. If x = 1 or x = n-1 , then n is likely prime, continue
to the next iteration. d. For r from 1 to s-1 : i. Compute x = x^2 mod n . ii. If x =
n-1 , then n is likely prime, break this inner loop and continue to the next iteration. e. If
none of the above conditions are met, n is composite. 4. If all k iterations pass, n is
considered likely prime.
Advantages: Very efficient for testing the primality of large numbers. Disadvantages:
Probabilistic (may rarely return a composite number as prime, though the probability of
error decreases exponentially with k ).
4. Usage Examples
To use the functions, navigate to the matlab_prime_finder directory in MATLAB and
run the example_usage.m script.
5. Testing
Unit tests are provided in test_prime_finder.m . To run the tests, open MATLAB,
navigate to the project directory, and run the following command in the MATLAB
command window:
runtests("test_prime_finder.m")
This will execute all defined test cases and report any failures or errors.