Post · 4 min read· pp. 75–77

Garner's algorithm

A consequence of the Chinese Remainder Theorem is, that we can represent big numbers using an array of small integers. For example, let pp be the product of the first 10001000 primes. pp has around 30003000 digits.

Any number aa less than pp can be represented as an array a1,…,aka_1, \ldots, a_k, where ai≡a(modpi)a_i \equiv a \pmod{p_i}. But to do this we obviously need to know how to get back the number aa from its representation. One way is discussed in the article about the Chinese Remainder Theorem.

In this article we discuss an alternative, Garner's Algorithm, which can also be used for this purpose.

Mixed Radix Representation

We can represent the number aa in the mixed radix representation:

a=x1+x2p1+x3p1p2+…+xkp1⋯pk−1 with xi∈[0,pi)a = x_1 + x_2 p_1 + x_3 p_1 p_2 + \ldots + x_k p_1 \cdots p_{k-1} \text{ with }x_i \in [0, p_i)

A mixed radix representation is a positional numeral system, that's a generalization of the typical number systems, like the binary numeral system or the decimal numeral system. For instance, the decimal numeral system is a positional numeral system with the radix (or base) 10. Every number is represented as a string of digits d1d2d3…dnd_1 d_2 d_3 \dots d_n between 00 and 99. For example, the string 415415 represents the number 4⋅102+1⋅101+5⋅1004 \cdot 10^2 + 1 \cdot 10^1 + 5 \cdot 10^0. In general the string of digits d1d2d3…dnd_1 d_2 d_3 \dots d_n represents the number d1bn−1+d2bn−2+⋯+dnb0d_1 b^{n-1} + d_2 b^{n-2} + \cdots + d_n b^0 in the positional numeral system with radix bb.

In a mixed radix system, we don't have one radix any more. The base varies from position to position.

Garner's algorithm

Garner's algorithm computes the digits x1,…,xkx_1, \ldots, x_k. Notice, that the digits are relatively small. The digit xix_i is an integer between 00 and pi−1p_i - 1.

Let rijr_{ij} denote the inverse of pip_i modulo pjp_j

rij=(pi)−1(modpj)r_{ij} = (p_i)^{-1} \pmod{p_j}

which can be found using the algorithm described in Modular Inverse.

Substituting aa from the mixed radix representation into the first congruence equation we obtain

a1≡x1(modp1).a_1 \equiv x_1 \pmod{p_1}.

Substituting into the second equation yields

a2≡x1+x2p1(modp2),a_2 \equiv x_1 + x_2 p_1 \pmod{p_2},

which can be rewritten by subtracting x1x_1 and dividing by p1p_1 to get

a2−x1≡x2p1(modp2)(a2−x1)r12≡x2(modp2)x2≡(a2−x1)r12(modp2)\begin{array}{rclr} a_2 - x_1 &\equiv& x_2 p_1 &\pmod{p_2} \\ (a_2 - x_1) r_{12} &\equiv& x_2 &\pmod{p_2} \\ x_2 &\equiv& (a_2 - x_1) r_{12} &\pmod{p_2} \end{array}

Similarly we get that

x3≡((a3−x1)r13−x2)r23(modp3).x_3 \equiv ((a_3 - x_1) r_{13} - x_2) r_{23} \pmod{p_3}.

Now, we can clearly see an emerging pattern, which can be expressed by the following code:

text
for (int i = 0; i < k; ++i) {
    x[i] = a[i];
    for (int j = 0; j < i; ++j) {
        x[i] = r[j][i] * (x[i] - x[j]);

        x[i] = x[i] % p[i];
        if (x[i] < 0)
            x[i] += p[i];
    }
}

So we learned how to calculate digits xix_i in O(k2)O(k^2) time. The number aa can now be calculated using the previously mentioned formula

a=x1+x2⋅p1+x3⋅p1⋅p2+…+xk⋅p1⋯pk−1a = x_1 + x_2 \cdot p_1 + x_3 \cdot p_1 \cdot p_2 + \ldots + x_k \cdot p_1 \cdots p_{k-1}

It is worth noting that in practice, we almost probably need to compute the answer aa using Arbitrary-Precision Arithmetic, but the digits xix_i (because they are small) can usually be calculated using built-in types, and therefore Garner's algorithm is very efficient.

Implementation of Garner's Algorithm

It is convenient to implement this algorithm using Java, because it has built-in support for large numbers through the BigInteger class.

Here we show an implementation that can store big numbers in the form of a set of congruence equations. It supports addition, subtraction and multiplication. And with Garner's algorithm we can convert the set of equations into the unique integer. In this code, we take 100 prime numbers greater than 10910^9, which allows representing numbers as large as 1090010^{900}.

text
final int SZ = 100;
int pr[] = new int[SZ];
int r[][] = new int[SZ][SZ];

void init() {
    for (int x = 1000 * 1000 * 1000, i = 0; i < SZ; ++x)
        if (BigInteger.valueOf(x).isProbablePrime(100))
            pr[i++] = x;

    for (int i = 0; i < SZ; ++i)
        for (int j = i + 1; j < SZ; ++j)
            r[i][j] =
                BigInteger.valueOf(pr[i]).modInverse(BigInteger.valueOf(pr[j])).intValue();
}

class Number {
    int a[] = new int[SZ];

    public Number() {
    }

    public Number(int n) {
        for (int i = 0; i < SZ; ++i)
            a[i] = n % pr[i];
    }

    public Number(BigInteger n) {
        for (int i = 0; i < SZ; ++i)
            a[i] = n.mod(BigInteger.valueOf(pr[i])).intValue();
    }

    public Number add(Number n) {
        Number result = new Number();
        for (int i = 0; i < SZ; ++i)
            result.a[i] = (a[i] + n.a[i]) % pr[i];
        return result;
    }

    public Number subtract(Number n) {
        Number result = new Number();
        for (int i = 0; i < SZ; ++i)
            result.a[i] = (a[i] - n.a[i] + pr[i]) % pr[i];
        return result;
    }

    public Number multiply(Number n) {
        Number result = new Number();
        for (int i = 0; i < SZ; ++i)
            result.a[i] = (int)((a[i] * 1l * n.a[i]) % pr[i]);
        return result;
    }

    public BigInteger bigIntegerValue(boolean can_be_negative) {
        BigInteger result = BigInteger.ZERO, mult = BigInteger.ONE;
        int x[] = new int[SZ];
        for (int i = 0; i < SZ; ++i) {
            x[i] = a[i];
            for (int j = 0; j < i; ++j) {
                long cur = (x[i] - x[j]) * 1l * r[j][i];
                x[i] = (int)((cur % pr[i] + pr[i]) % pr[i]);
            }
            result = result.add(mult.multiply(BigInteger.valueOf(x[i])));
            mult = mult.multiply(BigInteger.valueOf(pr[i]));
        }

        if (can_be_negative)
            if (result.compareTo(mult.shiftRight(1)) >= 0)
                result = result.subtract(mult);

        return result;
    }
}