primality-test
algorithm
computational-mathematics
number-theory
computer-science

Fastest algorithm for primality test

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In the realm of computer science and number theory, primality testing is a critical concept. It involves determining whether a given number is prime, meaning it is only divisible by 1 and itself. Primality tests are crucial for cryptographic applications and other computational tasks. Over the years, several algorithms have been developed to efficiently determine primality. This article discusses some of the fastest algorithms for primality testing, exploring their mechanisms and implementations with technical depth.

Karatsuba Multiplication and Basic Algorithms

Before delving into advanced primality tests, it's essential to understand some basic multiplication algorithms, as they play a foundational role in more complex algorithms. Karatsuba multiplication is a notable method that reduces the multiplication of two numbers from the traditional O(n2)O(n^2) complexity to approximately O(nlog23)O(n1.585)O(n^{\log_2 3}) \approx O(n^{1.585}). This faster multiplication can be leveraged to speed up arithmetic within primality tests.

Basic Primality Tests

  1. Trial Division: • The simplest primality test involves checking divisibility from 2 up to the square root of the number (nn). • Time Complexity: O(n)O(\sqrt{n}). • Not feasible for large numbers due to poor time efficiency.
  2. Fermat's Little Theorem: • Utilizes the theorem which states that if pp is a prime number, then for any integer aa such that 1<a<p11 < a < p-1, ap11 (mod p)a^{p-1} \equiv 1 \ (\text{mod} \ p). • This offers a probabilistic primality test. • Behaves poorly for Carmichael numbers, which are composite numbers passing Fermat’s test for many aa values.

Advanced Primality Testing Algorithms

The field of primality testing saw a significant breakthrough with the advent of advanced algorithms that offer both deterministic results and improved time complexity compared to simpler methods.

Miller-Rabin Primality Test

The Miller-Rabin test is a probabilistic test for primality, widely used due to its balance between efficiency and accuracy.

• It uses a property of prime numbers concerning their representation of the form n=2sd+1n = 2^s \cdot d + 1. • An integer aa is randomly chosen, and the number ad (mod n)a^d \ (\text{mod} \ n) is computed. • The test involves squaring and repeatedly checking if 1-1 or 11 modulo nn is obtained.

Pseudocode:

Complexity: O(klog3n)O(k \cdot \log^3 n) where kk is the number of accuracy iterations. • The main idea is rooted in a property of binomial coefficients. If xx is a variable, then for a prime pp, (x1)pxp1 (mod p)(x - 1)^p \equiv x^p - 1 \ (\text{mod} \ p). • AKS modifies this to verify with congruence for all numbers up to nn that ana (mod n)a^n \equiv a \ (\text{mod} \ n) holds under specific constraints. • Complexity: Originally O((logn)6)O((\log n)^6), with subsequent improvements reducing it to nearly O(log5.5n)O(\log^{5.5} n).


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.