In this lecture we’re going to focus on solving more numerical problems starting with
We saw that by Wilson’s Theorem, that the solution is exactly
But even though you might think now that we’re done, this formula is pretty useless. Because if \(p\) was large, then we’ll have to perform tons of computations to figure out \(x\). Can we find a faster method? Yes. Suppose we pick a number \(b\) (we’ll discuss later how to pick it). Then we compute the following
Square both sides above we see that
But by Fermat, we know that \(b^{(p-1)} \equiv 1 \pmod{p}\). Therefore,
Now, if \(a \equiv -1 \pmod{p}\), then we’re done. Why? substitute for \(a\) in
We want to write \(b^{\frac{p-1}{2}}\) as a square. We know that \(p \equiv 1 \pmod{4}\). Therefore,
So yes, we can write \(b\) as a square. To see this, observe that
So this means that \(x = b^{\frac{p-1}{4}}\)
is a solution to \(x^2 \equiv -1 \pmod{p}\). So now how do we choose \(b\) such that we get it is congruent to \(-1\) instead of being congruent to \(1\)? The easy way to just guess at random! Each guess has a probability of \(0.5\) since it’s either going to be congruent to \(1\) or \(-1\).
But how fast is this? First of all, raising \(b\) to the power of \((p-1)/2\) is polynomial in the input so it’s fast. The number of guesses will be bounded by \(p/2\). But this is going to be exponential time. So the worst case of this algorithm is actually exponential time. However the average case is polynomial time.
Example
We saw above that the solution will have the form
We just need to guess \(b\) such that \(x\) becomes congruent to \(-1\) and not \(1\). Observe that since \(p = 41\), then
We can start with \(b = 2\) and compute \(2^{10} \pmod{41}\). We saw in the previous lecture that we can do this fast by computing powers of \(2\) so
Therefore,
But this means that
So this fails since its square is not congruent to \(-1\) module \(41\). Next, we can try \(b = 3\). So
That doesn’t work either. If we’re a computer, then this will go on to the next iteration. But notice that earlier we found out
which is
Therefore, \(x = 2^5\) is a solution in this case.
Primality Testing (Simple Method)
The first method that we already saw is to test all factors up to the square root of \(x\). This is okay for up to a few hundred. If \(x\) is really large, then this method is slow since it takes \(\sqrt{x}\) steps. So it’s exponential in the number of digits in \(x\).
Primality Testing (Difference of Squares)
Next, the second method writes \(x\) as the difference of squares. Then
This method was used by Fermat to factor numbers. As an example, take \(x = 7313\). Pick squares slightly larger than \(x\). Trying this
Then, we can factor \(x\) to see that
This method works best if \(x = a \cdot b\) with \(a\) and \(b\) close.
Primality Testing (Fermat's Theorem)
Another method to test primes is asking if
If this is not true, then we know that \(m\) is not prime. So now we want to improve this test a little bit. Suppose that
If \(m\) is prime, then this implies that
But this means that (since we have no zero divisors)
So now instead of computing \(a^{m-1}\), we can instead compute \(a^{\frac{m-1}{2}}\) and check that
If this is false. Then,
So \(m\) can’t be prime.
Example
To use the Fermat test, we want to check if
To use the improved test, we want to see if
To use successive squaring, the professor says to factor \(m - 1\) into \(2^c \cdot d\) where \(d\) is odd. Then pick some number, say \(a=2\) since it’s the smallest choice. Then
Now, take \(2^d = 2^{35}\) and use successive squaring until you reach \((a^d)^{2^c} = a^{m-1}\)
At first glance, this seems to suggest \(561\) could be prime since the final result is \(1\) but notice that
However, \(67 \not\equiv \pm 1 \pmod{561}\) so \(67\) is a non-trivial square root of \(1\). If \(561\) was prime, then only \(-1\) and \(1\) can only be square roots of \(1 \pmod{p}\). So \(561\) is not a prime! in fact, we saw that the end result was \(1\) because it is a Carmichael number.