IsPrime | Fermat's Little Theorem
Given a number n, check if it is prime or not. We have introduced and discussed the School method for primality testing.
In this page, Fermat’s method is discussed. This method is a probabilistic method and is based on Fermat’s Little Theorem.
Fermat's little theorem:
If a given number is prime, then this method always returns true. If the given number is composite (or non-prime), then it may return true or false, but the probability of producing incorrect results for composite is low and can be reduced by doing more iterations.
Below is algorithm:
Last updated