Olympiad in Informatics

Miller-Rabin & Pollard-Rho Algorithm Notes

Miller-Rabin Primality Test

First, I strongly recommend reading this article. (If you get it, there's no need to come here.)

The Miller-Robin algorithm has two parts: one is the Fermat test, and the other is quadratic probing. These two things sound very black magic (and they really are), so I'll explain them clearly and include the code. The number we are testing is \(p\).

The Fermat Test and Michael Numbers

Fermat's little theorem tells us that if a number \(a\) is prime, then \(\forall x, x^{a-1} \equiv 1 \; (mod \; a\). Intuition tells us we could try running the Fermat test with the numbers in \([1,a)\) one by one to check whether it is prime. First, the complexity of that is still sky-high; second, there is a class of composite numbers that can pass the Fermat test, called Michael numbers. So the Fermat test can be wrong.

So how do we test efficiently?

Quadratic Probing

But we can also use a theorem like this:

\[\begin{gathered}\forall x, x^2 \equiv 1 (\mod\; p)\\如果p为质数 \\ 则x的解集只包含x \equiv 1或x \equiv p-1 \\ 否则p是合数。\end{gathered}\]

So we can let \(p - 1 = 2^k x\). After that, we can test by adjusting the value of \(k\), using quadratic probing and the Fermat test. Let's use code to go through exactly how these tests are done:

bool isPrime(ll p)
{
    // 把 p - 1 设为我们需要的值形式
    ll x = p - 1, k = 0, j;
    while (!(x & 1))
        x >>= 1, k++;
    // 进行随机的素数测试,这是 Miller-Rabin 的核心部分
    for (int i = 0; i <= 4; i++)
    {
        // 用于减少常数的判断
        if (p == bs[i])
            return true;
        // 算出本身的基底以便后期运算
        ll r = quick_pow(bs[i], x, p);
        // 常数判断
        if (r == 1 || r == p - 1)
            continue;
        for (j = 1; j <= k; j++)
        {
            // 重点:
            // 在二次探查中,p - 1 = 2^(kx),如果
            // 这个出现了意外解,那么 p 就是合数
            r = r * r % p;
            if (r == p - 1)
                break;
        }
        // 判断
        if (j > k)
            return false;
    }
    return true;
}

Pollard-Rho Prime Factorization

This algorithm is also a very nasty, randomized, black-magic algorithm. The usual prime factorization algorithm is trial division, whose time complexity is \(O(\sqrt{n})\). The Pollard-Rho algorithm uses randomization to solve the factorization problem with a smaller time complexity. There is a very good set of lecture notes (by BigBallon) on Crazydave's blog; do go and have a look.

The basis of this algorithm is actually a black-magic algorithm: randomly enumerating prime factors. Yep, that's right, it's just blindly ×× picking random numbers for trial division, also known as the “feel-good algorithm”. This algorithm mainly works by enumerating factors and doing trial division. Well, the complexity of this algorithm is \(O(上天)\).

So how can we optimize this seemingly unoptimizable algorithm? A black-magic algorithm naturally needs a black-magic optimization, so here I'll introduce a black-magic theorem to speed it up.

The Birthday Paradox

Actually, this whole paradox makes only one point:

The probability of simply picking numbers blindly ×× is \(\frac{1}{n}\), but if we enumerate \(k=i-j\), this probability goes up a lot.

Yes, now let me describe this theory.

If we pick numbers at random, our probability of picking the number is \(\frac{1}{n}\). This probability is too small, but we can enumerate two variables so that \(k=i-j\), and then our chance of getting a number goes up a lot; this can be found by computer simulation (the experimental data is in the lecture notes). The conclusion is that when we do \(\sqrt{n}\) enumerations, our probability increases to \(\sqrt{\frac{1}{n}}\). This is a very important finding.

Back to the Pollard-Rho Algorithm

We can let the random function be \(f(x)=(x^2+c) \mod \; n\), then make two variables \(x,y\). After that we can initialize the value of \(x\), then use \(x\) as the seed to generate \(y\), then set \(x\) to \(y\), and so on and so forth, and in each loop iteration we take the difference and compute \(gcd(|y-x|,n)\). The code is too nasty, so I won't post it (all that constant-squeezing has left my mindset pretty wrecked).