You're reading: cp's mathem-o-blog

The last four digits of 16¹⁶

On the math-fun mailing list, Dick Hess posted “a couple of curiosities”:

A speaker at G4G16 noted that \(16^{16}\) ends in \(1616\).

A friend sent me this: \(499^{499}\) ends in \(499499\).

Are there any other cases including numbers with more digits?

That does make me curious! I’ve kept that email in my inbox since March, hoping to have a go at finding more examples when I get some free time. Nobody else responded to the email, which is usually a sign that an answer is hard to find.

At first glance, it might seem really hard to even check these facts: \(16^{16}\) is a very big number, and \(499^{499}\) must have hundreds of digits. Can computers even work them out accurately?

This reminds me of one of the very first clever maths thoughts I remember having, during my Cambridge entrance interview (I didn’t get in). The question was, “what’s the last digit of \(2004^{2004}\)?”, and the insight was that I don’t care about any of the other digits. I left the interview pleased with myself and full of hope for my cantabrigian future. (oops)

You can quickly say that the last digit of \(2004^{2004}\) must be the same as the last digit of \(4^{2004}\) – none of the bigger digits have any effect on the last digit. From there, I noticed that the last digit of powers of 4 follows the pattern \(4,6,4,6,\ldots\), so the last digit of \(4^{2004}\) is 6.

Dick Hess’s curious numbers follow the same logic, but instead of looking at just the last digit, you look at enough digits to write the starting number out twice. That is, you want to compute \(f(16) = 16^{16} \pmod{10^4}\), and \(f(499) = 499^{499} \pmod{10^6}\).

I’m not about to start noticing patterns in the last four digits of a sequence of numbers, so I’ll be a little bit less clever. I could work out \(f(16)\) by repeatedly multiplying by 16 and keeping only the last four digits. But that would be really dreary for \(f(499)\).

There’s a quicker way, which I think I also spotted during my Cambridge interview. You can easily find powers of the form \(x^{2^n}\) by repeatedly squaring. And since you can write any number as the sum of powers of two, you can use these to compute the big power in just a few steps:

\[ 499^{499} = 499^{1 + 2 + 16 + 32 + 64 + 128 + 256} = 499^1 \times 499^2 \times 499^{16} \times 499^{32} \times 499^{64} \times 499^{128} \times 499^{256} \]

So I wrote some Python code to run through the natural numbers, computing the last few digits of \(n^n\) and checking for the form \(n n\).

def ends_with_itself_twice(n):
    l = len(str(n))
    m = 10**(2*l)
    a = n
    square = n
    out = 1
    while a > 0:
        z = a % 2
        if z:
            out = (out * square) % m
        square = (square * square) % m
        a = a // 2

    target = (10**l+1)*n
    return out == target

This ran fast – fast enough to check up to \(n = 10,\!000,\!000\) in a few seconds – but I didn’t find any solutions other than 16 and 499. And that’s the point at which I say I don’t like number theory, and someone else will have to prove the (non-)existence of other solutions.

Actually, that’s not true: when I first ran my code, it gave me loads of solutions, before I noticed that I was only looking for one copy of \(n\) at the end, instead of two. Those numbers are in the OEIS with a low entry number: A082576. A comment from Shyam Sunder Gupta and M.F. Hasler notes,

The only known composite and prime numbers such that n^n ends in twice the string of digits of n are 16 and 499, respectively [Gupta, 2025]. (Checked up to 10^9.)

I wonder if Shyam Sunder Gupta was the person who told Dick Hess this fact in the first place. Anyway, it doesn’t look like I’m going to find another example. Maybe you can? I’ve had a nice trip down memory lane, at least.

You must be logged in to post a comment.