Is the following a correct statement for the Fermat primality test?
For all $b$ if
- $n$ is prime and
- $(b,n)=1$
then $b^{n-1} \equiv 1 \bmod{n}$.
The contrapositive is if $b^{n-1} \not \equiv 1 \bmod{n}$ then condition (1) and/or (2) is false (they are equivalent --both attest to $n$ not being prime).
If you don't require the test to be definitive, then, well, um, maybe.
For it to be practical, however, I would change "all $b$" to "all $1 < b < n$". Obviously $n^{n - 1} \equiv 0 \bmod n$. Since this test involves exponentiation, I would want to avoid having to do more multiplications than necessary.
Let's try $n = 341$. Clearly 341 is an odd number. Either Wolfram Mathematica or Wolfram Alpha readily tells us that $2^{340} \equiv 1 \bmod 341$. Likewise we see that with a base 10 digital root of 8, 341 is not divisible by 3 but $3^{340} \equiv 56 \bmod 341$. Indeed $341 = 11 \times 31$. Looks like the test works.
But let's not be satisfied with one successful run. How about $n = 561$? I'm not pulling that number out of a hat. It came from one of the comments.
Okay, so 561 is also odd, and we see that $2^{560} \equiv 1 \bmod 561$. With a digit sum of 12, we see that 561 is divisible by 3, so we skip 3 and move on to 4. We see that $4^{560} \equiv 1 \bmod 561$ which means that the...
Hey, wait a minute! If 561 is divisible by 3, can it be prime? No, it's composite. Indeed $3^{560} \equiv 375 \bmod 561$. But you will find that if $b$ is not divisible by 3, 11 or 17, then $b^{560} \equiv 1 \bmod 561$.
This is why 341 is called a pseudoprime to base 2 and a few others, and 561 is called a pseudoprime to bases 2, 4, 5, 7, 8, 10, 13, 14, etc.
This means that the Fermat primality test is by itself not reliable, it gives a lot of small false positives. However, if for a largish odd number you can rule out a lot of small primes (like 3, 11, 17) as prime factors with trial division, and it's a pseudoprime to a lot of small bases, that might be a good enough indication of primality for your purposes.
By the way,
PrimeQin Wolfram Mathematica is not definitive either. But no one has found a false positive yet, maybe no one ever will.