Keywords

1 Introduction

Charles Babbage was an English mathematician, philosopher, inventor, mechanical engineer, and “irascible genius” who pioneered computing machines [2, 4, 10, 21,22,23]. Although he held the Lucasian Chair of Mathematics at Cambridge University from 1828 to 1839, during that period he never resided in Cambridge or delivered a lecture [5, 7, p. 7].

In 1819, he published his only work on number theory, a short paper [1] that begins:

The singular theorem of Wilson respecting Prime Numbers, which was first published by Waring in his Meditationes Analyticae [31, p. 218], and to which neither himself nor its author could supply the demonstration, excited the attention of the most celebrated analysts of the continent, and to the labors of Lagrange [14] and Euler we are indebted for several modes of proof \(\cdots .\)

Babbage formulated Wilson’s theorem as a criterion for primality: an integer \(p>1\) is a prime if and only if \((p-1)! \equiv -1 \!\pmod {p}\). (For a modern proof, see Moll [20, p. 66].) He then introduced several such criteria, involving congruences for binomial coefficients (see Granville [11, Sections 1 and 4]). However, some of his claims were unproven or even wrong (as Dubbey points out in [7, pp. 139–141]). One of his valid results is a necessary and sufficient condition for primality, based on a number of simultaneous congruences. Henceforth, let n denote an integer.

Theorem 1

(Babbage’s Primality Test) An integer \(p>1\) is a prime if and only if

$$\begin{aligned} \left( {\begin{array}{c}p+n\\ n\end{array}}\right) \equiv 1 \pmod {p} \end{aligned}$$
(1)

for all n satisfying \(0 \le n \le p-1.\)

This is of only theoretical interest, the test being slower than trial division.

The “only if” part is an immediate consequence of the beautiful theorem of Lucas [15] (see [8, 11, 17, 19] and [20, p. 70]), which asserts that if p is a prime and the non-negative integers \(a = \alpha _0 + \alpha _1 \, p + \cdots + \alpha _r \, p^r and \) \(b = \beta _0 + \beta _1 \, p + \cdots + \beta _r \, p^r\) are written in base p (so that \(0 \le \alpha _i, \beta _i \le p-1\) for all i), then

$$\begin{aligned} \left( {\begin{array}{c}a\\ b\end{array}}\right) \equiv \prod _{i=0}^r \left( {\begin{array}{c}\alpha _i\\ \beta _i\end{array}}\right) \pmod {p}. \end{aligned}$$
(2)

(Here the convention is that \(\left( {\begin{array}{c}\alpha \\ \beta \end{array}}\right) =0\) if \(\alpha < \beta \).) The congruence (1) follows if \(0 \le n \le p-1\), for then all the binomial coefficients formed on the right-hand side of (2) are of the form \(\left( {\begin{array}{c}\alpha \\ \alpha \end{array}}\right) =1,\) except the last one, which is \(\left( {\begin{array}{c}1\\ 0\end{array}}\right) =1.\)

However, the theorem was not available to Babbage because when it was published in 1878 he had been dead for seven years.

Lucas’s theorem implies more generally that for p a prime and m a power of p,  the congruences

$$\begin{aligned} \left( {\begin{array}{c}m+n\\ n\end{array}}\right) \equiv 1 \pmod {p} \qquad (0 \le n \le m-1) \end{aligned}$$
(3)

hold. A converse was proven in 2013: Meštrović’s theorem [19] states that if \(m>1\) and \(p>1\) are integers such that (3) holds, then p is a prime and m is a power of p. To begin the proof, Meštrović noted that for \(n = 1\), the hypothesis gives

$$\begin{aligned} \left( {\begin{array}{c}m+1\\ 1\end{array}}\right) = m+1 \equiv 1 \pmod {p}\quad \implies \quad p\mid m. \end{aligned}$$

The rest of the proof involves combinatorial congruences modulo prime powers.

As Meštrović pointed out, “the ‘if’ part of Theorem 1 is an immediate consequence of [his theorem] (supposing a priori [that \(m = p\)]). Accordingly, [his theorem] may be considered as a generalization of Babbage’s criterion for primality.”

Here we offer another generalization of Babbage’s primality test.

Theorem 2

(Least-Prime-Factor Test) The least prime factor of an integer \(m>1\) is the smallest natural number \(\ell \) satisfying

$$\begin{aligned} \left( {\begin{array}{c}m+\ell \\ \ell \end{array}}\right) \not \equiv 1 \pmod {m}. \end{aligned}$$
(4)

For that value of \(\ell ,\) the least non-negative residue of \(\left( {\begin{array}{c}m+\ell \\ \ell \end{array}}\right) \) modulo m is \(\frac{m}{\ell } + 1.\)

The proof is given in Sect. 2.

Babbage’s primality test is an easy corollary of the least-prime-factor test. Indeed, Theorem 2 implies a sharp version of Theorem 1 noticed by Granville [11] in 1995.

Corollary 1

(Sharp Babbage Primality Test) Theorem 1 remains true if the range for n is shortened to \(0 \le n\le \sqrt{p}.\)

Proof

An integer \(m>1\) is a prime if and only if its least prime factor \(\ell \) exceeds \(\sqrt{m}.\) The corollary follows by setting \(m=p\) in Theorem 2. \(\Box \)

To see that Corollary 1 is sharp in that the range for n cannot be further shortened to \(0\le n \le \sqrt{p}-1\), let q be any prime and set \(p=q^2\). Then p is not a prime, but the least-prime-factor test with \(m=p\) and \(\ell =q\) implies (1) when \(0\le n \le q-1\).

Problem 1

Since the “if” part of Babbage’s primality test is a consequence both of Meštrović’s theorem and of the least-prime-factor test, one may ask, Is there a common generalization of Meštrović’s theorem and Theorem 2? (Note, though, that the modulus in the former is p,  while that in the latter is m.)

Actually, the incongruence (4) holds more generally if the least prime factor \(\ell \mid m\) is replaced with any prime factor \(p \mid m\). The following extension of the least-prime-factor test is proven in Sect. 2.

Theorem 3

(i) Given a positive integer m and a prime factor \(p\mid m\), we have

$$\begin{aligned} \left( {\begin{array}{c}m+p\\ p\end{array}}\right) \not \equiv 1 \pmod {m}. \end{aligned}$$
(5)

(ii) If in addition \(p^r\mid m\) but \(p^{r+1}\not \mid m\), where \(r\ge 1\), then

$$\begin{aligned} \left( {\begin{array}{c}m+p\\ p\end{array}}\right) \equiv \frac{m}{p} + 1 \not \equiv 1 \pmod {p^r}. \end{aligned}$$
(6)

Part (i) is clearly equivalent to the statement that if \(d>1\) divides m and \(\left( {\begin{array}{c}m+d\\ d\end{array}}\right) \equiv 1 \pmod {m}\), then d is composite. As an example, for \(m=260\) and \(d=10\), we have

$$ \left( {\begin{array}{c}m+d\\ d\end{array}}\right) = \left( {\begin{array}{c}270\\ 10\end{array}}\right) = 479322759878148681 \equiv 1 \pmod {260}.$$

The sequence of integers \(m>1,\) for which some integer d (necessarily composite) satisfies

$$\begin{aligned} d>1 , \qquad d \mid m, \qquad \left( {\begin{array}{c}m+d\\ d\end{array}}\right) \equiv 1 \pmod {m}, \end{aligned}$$

begins [28, Seq. A290040]

$$ m=260, 1056, 1060, 3460, 3905, 4428, 5000, 5060, 5512, 5860, 6372, 6596,\cdots $$

and the sequence of smallest such divisors d is, respectively, [28, Seq. A290041]

$$\begin{aligned} d=10, 264, 10, 10, 55, 18, 20, 10, 52, 10, 18, 34,\cdots . \end{aligned}$$
(7)

Problem 2

Does Theorem 3 extend to prime power factors, i.e., does (5) also hold when p is replaced with \(p^k\), where \(p^k\mid m\) and \(k>1\)? In particular, in the sequence (7), is any term d a prime power?

Babbage also claimed a necessary and sufficient condition for primality based on a single congruence. But he proved only necessity, so we call it a test for non-primality.

Theorem 4

(Babbage’s Non-Primality Test) An integer \(m\ge 3\) is composite if

$$\begin{aligned} \left( {\begin{array}{c}2m-1\\ m-1\end{array}}\right) \not \equiv 1 \pmod {m^2}. \end{aligned}$$
(8)

Our version of his proof is given in Sect. 3.

Not only did Babbage not prove the claimed converse, but in fact it is false. Indeed, the numbers \(m_1=p_1^2=283686649\) and \(m_2=p_2^2=4514260853041\) are composite but do not satisfy (8), where \(p_1=16843\) and \(p_2=2124679\) are primes.

Here \(p_1\) (indicated by Selfridge and Pollack in 1964) and \(p_2\) (discovered by Crandall, Ernvall, and Metsänkylä in 1993) are Wolstenholme primes, so called by Mcintosh [16] because, while Wolstenholme’s theorem [32] (see [11, 18, 29] and [20, p. 73]) of 1862 guarantees that every prime \(p\ge 5\) satisfies

$$\begin{aligned} \left( {\begin{array}{c}2p-1\\ p-1\end{array}}\right) \equiv 1 \pmod {p^3}, \end{aligned}$$
(9)

in fact \(p_1\) and \(p_2\) satisfy the congruence in (9) modulo \(p^4\), not just \(p^3\) (see Guy [12, p. 131] and Ribenboim [25, p. 23]).

Note that (9) strengthens Babbage’s non-primality test, as Theorem 4 is equivalent to the statement that the congruence in (9) holds modulo \(p^2\) for any prime \(p\ge 3\).

In their solutions to a problem by Segal in the Monthly, Brinkmann [26] and Johnson [27] made Babbage’s and Wolstenholme’s theorems more precise by showing that every prime \(p\ge 5\) satisfies the congruences

$$\left( {\begin{array}{c}2p-1\\ p-1\end{array}}\right) \equiv 1-\frac{2}{3} p^3B_{p-3} \equiv \left( {\begin{array}{c}2p^2-1\\ p^2-1\end{array}}\right) \pmod {p^4},$$

where \(B_k\) denotes the kth Bernoulli number, a rational number. (See also Gardiner [9] and Mcintosh [16].) Thus, a prime \(p\ge 5\) is a Wolstenholme prime if and only if \(B_{p-3} \equiv 0 \pmod {p}\). (The congruence means that p divides the numerator of \(B_{p-3}\).) In that case, the square of that prime, say \(m=p^2\), is composite but must satisfy

$$\left( {\begin{array}{c}2m-1\\ m-1\end{array}}\right) \equiv 1\pmod {m^2},$$

thereby providing a counterexample to the converse of Babbage’s non-primality test.

Johnson [27] commented that “interest in [Wolstenholme primes] arises from the fact that in 1857, Kummer proved that the first case of [Fermat’s Last Theorem] is true for all prime exponents p such that \(p\not \mid B_{p-3}\).”

We have seen that the converse of Babbage’s non-primality test is false. The converse of Wolstenholme’s theorem is the statement that if \(p\ge 5\) is composite, then (9) does not hold. It is not known whether this is generally true. A proof that it is true for even positive integers was outlined by Trevisan and Weber [29] in 2001. In Sect. 3, we fill in some details omitted from their argument and extend it to prove the following stronger result.

Theorem 5

(Converse of Babbage’s Non-Primality Test for Even Numbers) If a positive integer m is even, then

$$\begin{aligned} \left( {\begin{array}{c}2m-1\\ m-1\end{array}}\right) \not \equiv 1\pmod {m^2}. \end{aligned}$$
(10)

2 Proofs of the Least-Prime-Factor Test and Its Extension

We prove Theorems 2 and 3. The arguments use only mathematics available in Babbage’s time.

Proof

(Theorem 2 ) As \(\ell \) is the smallest prime factor of m,  if \(0<k < \ell \) then k! and m are coprime. In that case, Bézout’s identity (proven in 1624 by Bachet in a book with the charming title Pleasant and Delectable Problems [3, p. 18, Proposition XVIII]— see [6, Section 4.3]) gives integers a and b with \(ak!+bm=1\). Multiplying Bézout’s equation by the number \(\left( {\begin{array}{c}m\\ k\end{array}}\right) = m(m-1)\cdots (m-k+1)/k!\) yields

$$ a m(m-1)\cdots (m-k+1) + bm\left( {\begin{array}{c}m\\ k\end{array}}\right) = \left( {\begin{array}{c}m\\ k\end{array}}\right) ,$$

so \(\left( {\begin{array}{c}m\\ k\end{array}}\right) \equiv 0\!\pmod {m}\) if \(1 \le k \le \ell -1.\) Now, for \(n=0,1,\cdots , \ell -1\), Vandermonde’s convolution [30] (see [20, p. 164]) of 1772 gives

$$\begin{aligned} \left( {\begin{array}{c}m+n\\ n\end{array}}\right) = \sum _{k=0}^n \left( {\begin{array}{c}m\\ k\end{array}}\right) \left( {\begin{array}{c}n\\ n-k\end{array}}\right) \equiv \left( {\begin{array}{c}m\\ 0\end{array}}\right) \left( {\begin{array}{c}n\\ n\end{array}}\right) \pmod {m}. \end{aligned}$$

(To see the equality, equate the coefficients of \(x^n\) in the expansions of \((1+x)^{m+n}\) and \((1+x)^m(1+x)^n\)). Thus, we arrive at the congruences

$$\begin{aligned} \left( {\begin{array}{c}m+n\\ n\end{array}}\right) \equiv 1 \pmod {m} \qquad (0 \le n \le \ell -1). \end{aligned}$$
(11)

On the other hand, from the identity

$$\begin{aligned} \left( {\begin{array}{c}a\\ b\end{array}}\right) = \frac{a}{b} \left( {\begin{array}{c}a-1\\ b-1\end{array}}\right) \end{aligned}$$
(12)

(to prove it, use factorials), the congruence (11) for \(n=\ell -1\), the integrality of \(\frac{m+\ell }{\ell }=\frac{m}{\ell }+1\), and the inequality \(\ell >1\) (as \(\ell \) is a prime), we deduce that

$$\begin{aligned} \left( {\begin{array}{c}m+\ell \\ \ell \end{array}}\right) = \frac{m+\ell }{\ell }\left( {\begin{array}{c}m+\ell -1\\ \ell -1\end{array}}\right) \equiv \frac{m}{\ell }+1 \not \equiv 1 \pmod {m}. \end{aligned}$$

Together with (11), this implies the least-prime-factor test. \(\Box \)

Proof

(Theorem 3 ) It suffices to prove (ii). Set

$$\begin{aligned} g{{{\mathrm{{{\mathrm{{\mathop {=}\limits ^{\mathrm{def}}}}}}}}}}\gcd ((p-1)!,m) \qquad \text {and}\qquad m_p{{\mathrm{{{\mathrm{{\mathop {=}\limits ^{\mathrm{def}}}}}}}}}\frac{m}{g}. \end{aligned}$$

Note that

$$\begin{aligned} p \ \text {prime} \implies p\not \mid g \implies p^r\mid m_p, \end{aligned}$$
(13)

since \(p^r\mid m\). Bézout’s identity gives integers a and b with \(a(p-1)!+bm=g\). When \(0<k<p\), multiplying Bézout’s equation by \(\left( {\begin{array}{c}m\\ k\end{array}}\right) \) yields

$$ a m(m-1)\cdots (m-k+1)\frac{(p-1)!}{k!} + bm\left( {\begin{array}{c}m\\ k\end{array}}\right) = g\left( {\begin{array}{c}m\\ k\end{array}}\right) $$

with \((p-1)!/k!\) an integer, so \(g\left( {\begin{array}{c}m\\ k\end{array}}\right) \equiv 0\!\pmod {m}\). Dividing by g gives

$$\left( {\begin{array}{c}m\\ k\end{array}}\right) \equiv 0\!\pmod {m_p}\quad (1\le k \le p-1).$$

Combining this with (12) and Vandermonde’s convolution, we get

$$\begin{aligned} {\begin{matrix} \left( {\begin{array}{c}m+p\\ p\end{array}}\right) = \frac{m+p}{p}\left( {\begin{array}{c}m+p-1\\ p-1\end{array}}\right) &{}= \frac{m+p}{p}\sum _{k=0}^{p-1} \left( {\begin{array}{c}m\\ k\end{array}}\right) \left( {\begin{array}{c}p-1\\ p-1-k\end{array}}\right) \\ &{}\equiv \frac{m}{p}+1 \pmod {m_p}. \end{matrix}} \end{aligned}$$
(14)

As \(p^{r+1}\not \mid m\), we have \(p^r\not \mid \frac{m}{p}\). Now, (13) and (14) imply (6), as required. \(\Box \)

3 Proofs of Babbage’s Non-primality Test and Its Converse for Even Numbers

The following proof is close to the one Babbage gave.

Proof

(Theorem 4 ) Suppose on the contrary that m is prime. If we have \(1 \le n \le m-1\), then m divides the numerator of \(\left( {\begin{array}{c}m\\ n\end{array}}\right) = m!/n!(m-n)!\) but not the denominator, so \(\left( {\begin{array}{c}m\\ n\end{array}}\right) \equiv 0 \pmod {m}\). Thus, by (12) and a famous case of Vandermonde’s convolution,

$$\begin{aligned} 2\left( {\begin{array}{c}2m-1\\ m-1\end{array}}\right) =\left( {\begin{array}{c}2m\\ m\end{array}}\right) = \sum _{n=0}^m\left( {\begin{array}{c}m\\ n\end{array}}\right) ^2 \equiv 1^2+1^2 \equiv 2 \pmod {m^2}. \end{aligned}$$

But as \(m\ge 3\) is odd, (3) contradicts (8). Therefore, m is composite. \(\Box \)

Before giving the proof of Theorem 5, we establish two lemmas. For any positive integer k,  let \(2^{v(k)}\) denote the highest power of 2 that divides k.

Lemma 1

If \(m\ge n\ge 1\) are integers satisfying \(n\le 2^{v(m)},\) then the formula \(v(\left( {\begin{array}{c}m\\ n\end{array}}\right) ) = v(m)-v(n)\) holds.

Proof

Let \(m=2^rm'\) with \(m'\) odd. Note that \(v(2^rm'-k) = v(k)\) if \(0<k < 2^r\). (Proof. Write \(k =2^tk',\) where \(0\le t = v(k) \le r-1\) and \(k'\) is odd. Then \(2^{r-t}m'-k'\) is also odd, so \(v(2^rm'-k) =v(2^t(2^{r-t}m'-k'))=t=v(k).\)) The logarithmic formula \(v(ab)=v(a)+v(b)\) then implies that when \(1 \le n\le 2^r\), the exponent of the highest power of 2 that divides the product

$$n!\left( {\begin{array}{c}m\\ n\end{array}}\right) = 2^rm'(2^rm'-1)(2^rm'-2)\cdots (2^rm'-(n-1))$$

is \(v(n!)+v(\left( {\begin{array}{c}m\\ n\end{array}}\right) ) = r +v(1\cdot 2\cdots (n-1))\), so \(v(\left( {\begin{array}{c}m\\ n\end{array}}\right) ) = r-v(n)\). As \(r=v(m)\), this proves the desired formula. \(\Box \)

Lemma 1 is sharp in that the hypothesis \(n\le 2^{v(m)}\) cannot be replaced with the weaker hypothesis \(v(n) \le v(m).\) For example, \(v(\left( {\begin{array}{c}10\\ 6\end{array}}\right) ) = v(210) = 1\), but \(v(10)-v(6) =0.\)

Lemma 2

A binomial coefficient \(\left( {\begin{array}{c}2m-1\\ m-1\end{array}}\right) \) is odd if and only if \(m=2^r\) for some \(r\ge 0.\)

Proof

Kummer’s theorem [13] (see [20, p. 78] or [24]) for the prime 2 states that \(v(\left( {\begin{array}{c}a+b\\ a\end{array}}\right) )\) equals the number of carries when adding a and b in base 2 arithmetic. Hence, \(v(\left( {\begin{array}{c}m+m\\ m\end{array}}\right) )\) is the number of ones in the binary expansion of m, and so \(v(\left( {\begin{array}{c}2m\\ m\end{array}}\right) )=1\) if and only if \(m=2^r\) for some \(r\ge 0\). As \(\left( {\begin{array}{c}2m\\ m\end{array}}\right) = 2 \left( {\begin{array}{c}2m-1\\ m-1\end{array}}\right) \) by (12), we are done. \(\Box \)

We can now prove the converse of Babbage’s non-primality test for even numbers.

Proof

(Theorem 5 ) For \(m\ge 2\) not a power of 2,  Lemma 2 implies that \(\left( {\begin{array}{c}2m-1\\ m-1\end{array}}\right) \) is even, so \(\left( {\begin{array}{c}2m-1\\ m-1\end{array}}\right) \) is congruent modulo 4 to either 0 or 2. For \(m\ge 2\) a power of 2, say \(m=2^r\), the equalities in (3) and the symmetry \(\left( {\begin{array}{c}m\\ n\end{array}}\right) =\left( {\begin{array}{c}m\\ m-n\end{array}}\right) \) yield

$$\left( {\begin{array}{c}2m-1\\ m-1\end{array}}\right) = 1 + \frac{1}{2}\left( {\begin{array}{c}2^r\\ 2^{r-1}\end{array}}\right) ^2+ \sum _{k=1}^{2^{r-1}-1} \left( {\begin{array}{c}2^r\\ k\end{array}}\right) ^2, $$

and Lemma 1 implies that \(\frac{1}{2}\left( {\begin{array}{c}2^r\\ 2^{r-1}\end{array}}\right) ^2 \equiv 2\!\!\pmod {4}\) and that \( \left( {\begin{array}{c}2^r\\ k\end{array}}\right) ^2\equiv 0\!\!\pmod {4}\) when \(0<k<2^{r-1}\); thus, by addition \(\left( {\begin{array}{c}2m-1\\ m-1\end{array}}\right) \equiv 3\!\! \pmod {4}\). Hence for all \(m\ge 2\), we have \(\left( {\begin{array}{c}2m-1\\ m-1\end{array}}\right) \not \equiv 1\!\! \pmod {4}\). Now as 4 divides \(m^2\) when m is even, (10) holds a fortiori. This completes the proof. \(\Box \)