archimedes' cattle problem

Around 2300 years ago, Archimedes developed a “BigInteger” scheme in order to express integers larger than supported by the Greek numeral system, ie, larger than a myriad-myriad = 108. His system is described in a paper he wrote called The Sand Reckoner, where he calculated an upper bound of 1063 for the number of grains of sand that could fit in the known universe.

It was around this time that Archimedes sent a letter to a colleague in Egypt with a problem that would surely strain any BigInteger system.


the letter

If thou art diligent and wise, O stranger, compute the number of cattle of the Sun, who once upon a time grazed on the fields of the Thrinacian isle of Sicily, divided into four herds of different colours, one milk white, another a glossy black, a third yellow and the last dappled. In each herd were bulls, mighty in number according to these proportions: Understand, stranger, that the white bulls were equal to a half and a third of the black together with the whole of the yellow, while the black were equal to the fourth part of the dappled and a fifth, together with, once more, the whole of the yellow. Observe further that the remaining bulls, the dappled, were equal to a sixth part of the white and a seventh, together with all of the yellow. These were the proportions of the cows: The white were precisely equal to the third part and a fourth of the whole herd of the black; while the black were equal to the fourth part once more of the dappled and with it a fifth part, when all, including the bulls, went to pasture together. Now the dappled in four parts were equal in number to a fifth part and a sixth of the yellow herd. Finally the yellow were in number equal to a sixth part and a seventh of the white herd. If thou canst accurately tell, O stranger, the number of cattle of the Sun, giving separately the number of well-fed bulls and again the number of females according to each colour, thou wouldst not be called unskilled or ignorant of numbers, but not yet shalt thou be numbered among the wise.

But come, understand also all these conditions regarding the cattle of the Sun. When the white bulls mingled their number with the black, they stood firm, equal in depth and breadth, and the plains of Thrinacia, stretching far in all ways, were filled with their multitude. Again, when the yellow and the dappled bulls were gathered into one herd they stood in such a manner that their number, beginning from one, grew slowly greater till it completed a triangular figure, there being no bulls of other colours in their midst nor none of them lacking. If thou art able, O stranger, to find out all these things and gather them together in your mind, giving all the relations, thou shalt depart crowned with glory and knowing that thou hast been adjudged perfect in this species of wisdom.


the cattle problem

Translating the letter, there are four colors of cattle: black, white, yellow, and dappled. We label the bulls by the lower-case letters b, w, y, d and the cows by the upper-case letters B, W, Y, D. The problem is given in two parts.

part 1

The first paragraph translates to a system of 7 linear of equations in 8 variables.

linear system
w=(12+13)b+y
b=(14+15)d+y
d=(16+1y)w+y
W=(13+14)(b+B)
B=(14+15)(d+D)
D=(15+16)(y+Y)
Y=(16+17)(w+W)

Clearing denominators results in a system of 7 linear integer equations.

linear system
6w=5b+6y
20b=9b+20y
42d=13b+42y
12W=7b+7B
20B=9d+9D
30D=11y+11Y
42Y=13w+13W

This is an underdetermined system, so there are infinitely many integer solutions. A solution can be found either using algebraic manipulations or by reducing the following matrix to its reduced-row-echelon form.

[[  6,  -5,   0,  -6,   0,   0,   0,   0],
 [  0,  20,  -9, -20,   0,   0,   0    0],
 [-13,   0,  42, -42,   0,   0,   0,   0],
 [  0,  -7,   0,   0,  12,  -7,   0,   0],
 [  0,   0,  -9,   0,   0,  20,  -9,   0],
 [  0,   0,   0, -11,   0,   0,  30, -11],
 [-13,   0,   0,   0, -13,   0,   0,  42]]

Reducing the matrix and computing the null-space will result in a vector with one free parameter. After clearing denominators to get integer solutions, or after solving via algebraic manipulations, you will end up with the the following solution.

k= any integer
w=10355482k
b=7460514k
d=7358060k
y=4149387k
W=7206360k
B=4893246k
D=3515820k
Y=5439213k
Total =50389082k

The smallest solution has 50,389,082 cattle, but Archimedes is unimpressed. We are, by his accounting, not entirely unskilled in mathematics!

part 2

In the second paragraph, Archimedes imposes two additional conditions. The first condition is that w+b must a square number. Using the computed solution, we know that

w+b=17826996k=22311294657k.

In order for this to be a square number, k must equal 311294657 multiplied by a square number, ie, there must be some integer t such that

k=9574657t2.

The second condition is that d+y must be a triangular number. Using the computed solutions above and the value of k, we get

d+y=24714657k=9572471(4657t)2.

For this to be a triangular number, it must equal m(m+1)/2 for some integer m. In other words,

m2m29572471(4657t)2=0.

The quadratic formula implies that

m=1+1+29572471(9314t)22,

or equivalently, putting v=9314t,

m=1+1+4729494v22.

In order for this to be an integer, the inside of the square root must be a perfect square, say u2. Therefore, we are left trying to find an integer solution (u,v) to u24729494v2=1, where v is divisible by 9314.

In modern mathematics, an equation of this sort is called Pell’s equation. It was misattributed to John Pell, who never worked on the problem. Long before being misnamed though, these equations were studied by the Ancient Greeks and then solved by Indian mathematicians.


continued fractions

The continued fraction algorithm begins with a real number α, computes its integer part called the quotient q, and its fractional part β=αq. This β is between 0 and 1, so it’s reciprocal is greater than 1.

At each iteration of the algorithm, α is replaced by the reciprocal of β and the same process is applied. This procdure is continued indefinitely or until β is equal to 0. The pseudo-code for the continued fraction algorithm is as follows.

loop do
  beta = alpha - alpha.floor
  if beta == 0
    break
  else
    alpha = beta.reciprocal
  end
end

If α is a rational number, the algorithm is essentially equivalent to the Euclidean algorithm and terminates after finitely many steps. Otherwise, the algorithm is infinite.

The name continued fraction stems from the fact that the quotients in the algorithm can be used to express α as an “infinite fraction”. In particular, indexing the quotients as q0,q1,q2,... results in

α=q0+1q1+1q2+1.

When α=R for some non-square integer R, it turns out that the sequence of quotients in periodic. For example, the continued fraction algorithm for α=33 is the following.

α q β
33 5 5+33
58+1833 1 38+1833
1+1333 2 1+1333
38+1833 1 58+1833
5+33 10 5+33

The β in the last row shown is the same as the β in the first row. This implies that these last 4 rows will repeat indefinitely. The quotients will be 5,1,2,1,10,1,2,1,10,1,2,1,10, which we abbreviate as 33=[5;1,2,1,10] The semicolon separates the initial part from the periodic part.

The same pattern holds in general: for α=R, the algorithm only needs to run until α=q0+R. The proof of this fact can be found in many elementary number theory books.

In order to do this programatically, we need to track the arithmetic of subtracting floor and then inverting a quadratic rational number. In order to take the reciprocal, the denominator must be rationalized and the result must be reduced by any common factors. We will represent the quadratic rational (a+bR)/c by the tuple (a,b,c).

Subtracting the quotient:

a+bRcq=(aqc)+bRc

Inverting:

c(aqc)+bR=c(aqc)bcR(aqc)2b2R c(aqc)+bR=c(qca)+bcRb2R(qca)2

Therefore, the transformation on the tuple (a,b,c) is the following.

transformation
a(qca)c
bbc
cb2R(qca)2

The only thing left is to reduce by any common factors. The pseudo-code for the continued fraction algorithm might look something like this.

def continued_fraction(root) do
  q0 = sqrt(root).floor
  alpha = (0, 1, 1)
  quotients = []

  while alpha != (q0, 1, 1) do
    a, b, c = alpha
    q = ((a + b * q0) / c).floor
    a = q * c - a
    b = b * c
    c = b**2 * root - (q * c - a)**2
    d = gcd(a, b, c)

    alpha = (a/d, b/d, c/d)
    quotients.push(q)
  end

  return quotients
end

Back to the cattle problem, using this or some equivalent continued fraction algorithm implementation, we can compute the continued fraction for 4729494. It turns out to have a period of 92. The first ten quotients are [2174,1,2,1,5,2,25,3,1,1,...].


convergents

The convergents of a continued fraction are the rational numbers obtained using finitely many of the quotients. The first few convergents for the example above are the following.

33=[5;1,2,1,10]
5=51
5+11=61
5+11+12=173
5+11+12+11=234
5+11+12+11+110=24743

These rational numbers are approximations to 33. The approximations alternate between under- and over-estimates and grow more accurate as we go.


pell’s equation

Finding integer solutions to an equation of the form u2Rv2=1 is known as solving Pell’s equation.

The Indian mathematician Brahmagupta (~600 CE) developed an identity to derive infinitely many solutions of Pell’s equation from a single solution. He was also able to find solutions to some notoriously difficult examples.

Bhaskara II (~1150 CE) extended Brahmagupta’s techniques to develop his “Circle Method”, which was capable of solving any instance of Pell’s equation.

But it wasn’t until 1768 that Lagrange proved that Bhaskara’s method will always terminate after finitely many steps and yield a solution. Lagrange used continued fractions for his proof.

In particular, Lagrange was able to prove that the convergent right before the end of the period of quotients can be used to produce a solution to Pell’s equation.

In the example above, the convergent of interest is 23/4. The numerator and denominator (23,4) form a solution to u233v2=1. In the general case, the convergent may form a solution to u233v2=1, in which case the convergent at the end of the second period will form a solution to Pell’s equation itself.

In order to calculate the convergents efficiently, we use another property of the continued fraction of R, namely that the numerators and denominators of the convergents both satisfy the same recurrence relation: next_value = quotient * current_value + previous_value.

Here is a modified version of the pseudo-code for the continued fraction algorithm that also records convergents as we go.

def continued_fraction(root) do
  q0 = sqrt(root).floor
  alpha = (0, 1, 1)
  quotients = []
  convergents = [(0, 1), (1, 0)]

  while alpha != (q0, 1, 1) do
    a, b, c = alpha
    q = ((a + b * q0) / c).floor
    a = q * c - a
    b = b * c
    c = b**2 * root - (q * c - a)**2
    d = gcd(a, b, c)

    alpha = (a/d, b/d, c/d)

    prev_conv = convergents[-2]
    curr_conv = convergents[-1]
    next_conv = (q * curr_conv[0] + prev_conv[0], q * curr_conv[1] + prev_conv[1])

    quotients.push(q)
    convergents.push(next_conv)
  end

  return (quotients, convergents)
end

producing all solutions from a first

The last ingredient we need to solve Archimedes’ cattle problem is the identity that Brahmagupta found to produce more solutions from a single solution. This identity can be framed in modern terms as follows: if (a,b) is the smallest integer solution (found using the continued fraction convergents) to u2Rv2=1, then every solution is of the form (A,B), where (a+bR)n=A+BR for a positive integer n.

Using the example of R=33 again, we can compute powers to find new solutions to Pell’s equations.

power computation solution
(23+433)1 23+433 (23,4)
(23+433)2 1057+18433 (1057,184)
(23+433)3 48599+846033 (48599,8460)

Computing powers programatically will rely on the arithmetic of multiplying two quadratic integers.

(a+br)(c+dr)=(ac+rbd)+(ad+bc)r

Using this, the pseudo code for exponentiation might look something like this.

def multiply(root, (a, b), (c, d)) do
  return (a * c + b * d * root, a * d + b * c)
end

def power(root, (a, b), exponent) do
  if exponent == 0
    return (1, 0)
  elseif exponent == 1
    return (a, b)
  else
    half_power = power(root, (a, b), exponent/2)
    half_power_squared = multiply(root, half_power, half_power)

    if exponent % 2 == 0
      return half_power_squared
    else
      return multiply(root, (a, b), half_power_squared)
    end
  end
end

the solution to the cattle problem

Recall that we need to find a solution to u24729494v2=1 such that v is divisible by 9314. Using the continued fraction (which has a period of 92) and its convergents, we arrive at a first solution (a,b) with

a=109931986732829734979866232821433543901088049 b=50549485234315033074477819735540408986340

Amthor in fact computed this by hand in 1880. He was also able to prove that the 2329th power of a+b4729494 is the one where v is finally divisible by 9314.

In particular, if (a+b4729494)2329=A+B4729494, Amthor used logarithms to show that A has 103,273 digits and B has 103,270 digits. Setting t=B/9314, we have

k=9574657t2=957B229314

so the total number of cattle is

50389082k=50389082957B218628

which has 206,545 digits!!

The first and last fifty digits of the smallest solution to Archimedes’ cattle problem are

77602714064868182695302328332138866642323224059233 05994630144292500354883118973723406626719455081800

epilogue

As mentioned, Amthor was able to find the exact solution by hand in 1880, but was unable to compute the solution. He correctly computed the number of digits and calculated the first four digits to be 7766. The first three of his digits were correct.

In 1965, mathematicians Williams, German, and Zarnke at the University of Waterloo used an IBM 7040 and an IBM 1620 to compute the full solution. It took 7:49 hours. They published their methods in the Mathematics of Computation. The number itself was deposited in the unpublished mathematical tables of the journal.

In 1981, Harry Nelson used a Cray-1 computer at Livermore National Laboratory to compute and verify the solution in about 10 minutes. He published the result with 1/3 font size on 47 pages of the Journal of Recreational Mathematics.


bang bang con west (feb 2020)

I gave a 10-minute soap opera version of this as a talk at !!con west in februrary of 2020. At the end of the talk, I used a 2017 13in MacBook Pro to compute the solution in a tenth of a second using some ruby code that I wrote.