Triangular Numbers Revisited

The $n$th triangular number is

$$T(n) = 1 + 2 + 3 + \cdots + n = \frac{n(n+1)}{2}.$$

Triangular numbers satisfy many interesting properties, including a product rule for $m, n \ge 1$:

$$T(mn) = T(m) T(n) + T(m-1) T(n-1).$$

This product rule can be interpreted geometrically by subdividing a large triangle into smaller triangles. This picture illustrates the case $m = 5$ and $n = 4$.

20th-triangular-number

An interesting question (in my opinion) is whether there are other sequences that satisfy this identity. It turns out that there are exactly five such sequences:

  1. $T(n) = 0$ for all $n \ge 0$.
  2. $T(n) = \frac12$ for all $n \ge 0$.
  3. $T(0) = 0$ and $T(2n) = T(2n-1) = n$ for all $n \ge 1$.
  4. $T(3n) = T(3n+2) = 0$ and $T(3n+1) = 1$ for all $n \ge 0$.
  5. $T(n) = \frac12 n(n+1)$ for all $n \ge 0$.

I wrote about this problem previously at MathBlag, but now I am trying to prepare the result for publication. I would appreciate feedback on my e-print.

Symbolic Computation with Python

In the beginning, computers were used for numerical computations, and it was good. But it was soon discovered that computers could perform symbolic computations as well. The first computer algebra system was Macsyma, which was developed by MIT in 1968. Today, the leading computer algebra systems are Mathematica and Maple. These systems are very powerful; but they are closed-source commercial products, and they can be quite expensive.

Fortunately, there are some excellent free alternatives. SymPy is an open-source Python library for symbolic computation. In this note, I will demonstrate the solution of two algebraic problems using SymPy.

A problem from Crux

The following problem is from Crux Mathematicorum, a journal for secondary and undergraduate students published by the Canadian Mathematical Society.

M379. The integers $27 + C$, $555 + C$, and $1371 + C$ are all perfect squares, the square roots of which form an arithmetic sequence. Determine all possible values of C. (Crux v35 n1, Feb 2009)

We need to solve the following nonlinear system of equations.

$$
\begin{align*}
(x-y)^2 &= 27 + C \\
x^2 &= 555 + C \\
(x+y)^2 &= 1371 + C
\end{align*}
$$

The system can be solved manually by combining the equations in clever ways. (Hint: $(x-y)^2 + (x+y)^2 = 2x^2 + 2y^2$.) We present a solution using Python and SymPy.

from sympy.solvers import solve
from sympy import symbols

x, y, C = symbols('x y C')
system = [(x - y)**2 - (27 + C),
          x**2 - (555 + C),
          (x + y)**2 - (1371 + C)]
print solve(system)

The code is almost self-explanatory. Line 4 creates three symbols named x, y, and C. In lines 5-7 we define an array of equations (the right side of each equation is assumed to be zero). We print the solution in line 8. The program produces the following output:

[{C: 229, x: -28, y: -12}, {C: 229, x: 28, y: 12}]

Euler’s four-square Identity

Our second example is Euler’s four-square identity. This identity shows that if two numbers $A$ and $B$ can be expressed as the sum of four squares, then the product $AB$ can also be so expressed. This is a key step in the proof of Lagrange’s four square theorem, which states that every positive integer can be expressed as the sum of four squares.

Behold Euler’s four-square identity in all its glory.
\[
(a_1^2+a_2^2+a_3^2+a_4^2)(b_1^2+b_2^2+b_3^2+b_4^2)= \\
(a_1 b_1 + a_2 b_2 + a_3 b_3 + a_4 b_4)^2 +\\
(a_1 b_2 – a_2 b_1 + a_3 b_4 – a_4 b_3)^2 +\\
(a_1 b_3 – a_2 b_4 – a_3 b_1 + a_4 b_2)^2 +\\
(a_1 b_4 + a_2 b_3 – a_3 b_2 – a_4 b_1)^2.
\]

This identity can be verified by hand. The calculation is straightforward, but tedious. Why not let the computer do the work?

from sympy import symbols, expand

a1, a2, a3, a4 = symbols("a1 a2 a3 a4")
b1, b2, b3, b4 = symbols("b1 b2 b3 b4")

c1 = a1*b1 + a2*b2 + a3*b3 + a4*b4
c2 = a1*b2 - a2*b1 - a3*b4 + a4*b3
c3 = a1*b3 - a3*b1 + a2*b4 - a4*b2
c4 = a1*b4 - a4*b1 - a2*b3 + a3*b2

aa = a1**2 + a2**2 + a3**2 + a4**2
bb = b1**2 + b2**2 + b3**2 + b4**2
cc = c1**2 + c2**2 + c3**2 + c4**2

print expand(aa*bb - cc)

The result is 0, which confirms the identity.

Getting started with SymPy

I recommend the Anaconda distribution of Python, because it includes SymPy and most other tools that you will need for scientific computation. There is also a “live” version of SymPy that runs in a web browser and does not require installing additional software.

Vietnamese math puzzle

Alex Bellos shared the following math puzzle that was given to Vietnamese eight-year-olds. The challenge is to place the digits from 1 to 9 into the grid.

Vietnamese math puzzle

Preliminary remarks

  1. Although it wasn’t stated explicitly, I assume that each digit should be used only once.
  2. The colon signifies division.
  3. There are many correct answers. I assume that students were meant to use enlightened trial and error to find a solution, but were not expected to find all solutions.
  4. I am assuming the standard order of operations, but I am not sure if this was the original intention. Would an eight-year-old be expected to understand that multiplication and division have higher precedence than addition and subtraction?
  5. Frankly, this puzzle is kind of boring for grown-ups.

Python code

Searching for solutions by hand did not appeal to me, so I wrote a Python program to search for all solutions. I hope that this example is helpful for people who are learning Python. (Perhaps it will inspire someone to write a better program than this.)

from itertools import permutations
standard_order = True
tolerance = 1e-7
target = 66

if standard_order:
    expr = "? + 13 * ? / ? + ? + 12 * ? - ? - 11 + ? * ? / ? - 10"
else:
    expr = "(((? + 13) * ? / ? + ? + 12) * ? - ? - 11 + ?) * ? / ? - 10"
expr = expr.replace("?", "%d")

for inputs in permutations(range(1,10)):
    expression = expr % inputs
    result = eval(expression)
    if abs(result - target) < tolerance:
        print ("%s = %s" % (expression, target))

Discussion

The itertools package provides many useful tools for iteration, including the permutation function, which generates all permutations of a list. Our script iterates through all 362880 permutations of the set of integers from 1 to 9.

Each permutation is substituted into the expression and evaluated. The program uses the eval function, which allows Python to run Python code within itself. This can be very dangerous, so use this function with care.

The division operations can lead to round-off error, so we only check that the result is very close to the target value. The variable tolerance sets the required precision.

Output

There are 136 solutions.

1 + 13 * 2 / 6 + 4 + 12 * 7 - 8 - 11 + 3 * 5 / 9 - 10 = 66
1 + 13 * 2 / 6 + 4 + 12 * 7 - 8 - 11 + 5 * 3 / 9 - 10 = 66
1 + 13 * 3 / 2 + 4 + 12 * 5 - 8 - 11 + 7 * 9 / 6 - 10 = 66
1 + 13 * 3 / 2 + 4 + 12 * 5 - 8 - 11 + 9 * 7 / 6 - 10 = 66
1 + 13 * 3 / 2 + 9 + 12 * 5 - 6 - 11 + 4 * 7 / 8 - 10 = 66
1 + 13 * 3 / 2 + 9 + 12 * 5 - 6 - 11 + 7 *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 5 - 11 + 3 * 2 / 8 - 10 = 66
9 + 13 * 2 / 8 + 7 + 12 * 6 - 5 - 11 + 1 * 3 / 4 - 10 = 66
9 + 13 * 2 / 8 + 7 + 12 * 6 - 5 - 11 + 3 * 1 / 4 - 10 = 66
9 + 13 * 3 / 1 + 6 + 12 * 2 - 5 - 11 + 7 * 8 / 4 - 10 = 66
9 + 13 * 3 / 1 + 6 + 12 * 2 - 5 - 11 + 8 * 7 / 4 - 10 = 66
9 + 13 * 3 / 2 + 1 + 12 * 5 - 6 - 11 + 4 * 7 / 8 - 10 = 66
9 + 13 * 3 / 2 + 1 + 12 * 5 - 6 - 11 + 7 * 4 / 8 - 10 = 66
9 + 13 * 4 / 1 + 5 + 12 * 2 - 7 - 11 + 3 * 8 / 6 - 10 = 66
9 + 13 * 4 / 1 + 5 + 12 * 2 - 7 - 11 + 8 * 3 / 6 - 10 = 66
9 + 13 * 4 / 8 + 5 + 12 * 6 - 7 - 11 + 1 * 3 / 2 - 10 = 66
9 + 13 * 4 / 8 + 5 + 12 * 6 - 7 - 11 + 3 * 1 / 2 - 10 = 66
9 + 13 * 5 / 3 + 1 + 12 * 4 - 2 - 11 + 7 * 8 / 6 - 10 = 66
9 + 13 * 5 / 3 + 1 + 12 * 4 - 2 - 11 + 8 * 7 / 6 - 10 = 66
9 + 13 * 6 / 4 + 3 + 12 * 5 - 8 - 11 + 1 * 7 / 2 - 10 = 66
9 + 13 * 6 / 4 + 3 + 12 * 5 - 8 - 11 + 7 * 1 / 2 - 10 = 66
9 + 13 * 8 / 6 + 2 + 12 * 4 - 1 - 11 + 5 * 7 / 3 - 10 = 66
9 + 13 * 8 / 6 + 2 + 12 * 4 - 1 - 11 + 7 * 5 / 3 - 10 = 66

Russian peasant multiplication and loop invariants

Russian peasant multiplication is a method for multiplying numbers based on repeated doubling and halving. Nobody is quite sure why it is called “Russian peasant multiplication,” but the name has stuck.

Here is the procedure:

  1. Write each number at the head of a column.
  2. Double the number in the first column.
  3. Halve the number in the second column. (If odd, ignore the remainder.)
  4. Repeat (2) and (3) until the number in the second column is 1.
  5. Cross out the rows with an even number in the second column.
  6. Add up the remaining numbers in the first column.

Example: Multiply 43 by 18.

First number Second number
 43   18 
\(86\) \(9\)
 172   4 
 344   2 
\(688\) \(1\)

\(\implies 43 \times 18 = 86 + 688 = 774.\)

Here is a Python implementation of the algorithm.

def multiply(m, n):
    p = 0
    while n > 0:
        if n % 2: # n is odd
            p = p + m
        m = m * 2
        n = n // 2
    return p

Correctness

Let’s see what happens to the values of \(m\), \(n\), and \(p\) in one iteration. If \(n\) is even, then \(m\) is doubled and \(n\) is halved, so the product \(mn\) is unchanged. If \(n\) is odd, then \(m\) is doubled and \(n\) is replaced with \((n-1)/2\), so the value of the product \(mn\) is decreased.
\[(2m) \cdot (n-1)/2 = mn – m\]
To compensate for this decrease, the value of \(p\) is increased by \(m\) when \(n\) is odd. Consequently, the value of \(Q = mn + p\) does not change – it is a loop invariant. At the beginning of the loop we have \(Q = mn\) (since \(p=0\)) and at the end we have \(Q = p\) (since \(n=0\)). Therefore, the product \(mn\) is equal to the final value of \(p\).

A decimal variation

The Russian Peasant method is associated with binary (base two) arithmetic, since it involves multiplying and dividing by two. Is there a decimal (base ten) version? Consider the following:

def multiply_decimal(m, n):
    p = 0
    while n > 0:
        r = n % 10
        p = p + m * r
        m = m * 10
        n = n // 10
    return p

Let’s analyze this algorithm. Each time through the loop, the values of \(m\), \(n\), and \(p\) are replaced with new values \(m’\), \(n’\), and \(p’\). The following relations hold between the new values and the old values.
\[
\begin{align*}
m’ &= 10m\\
n &= 10n’ + r\\
p’ &= p + mr
\end{align*}
\]

As before, the quantity \(Q = mn+p\) is a loop invariant, since
\(
mn + p = m(10n’ + r) + p = (10m)n’ + (p + mr) = m’n’ + p’.
\)
Therefore, the function returns the product \(mn\).

Example: Multiply 123 by 456.

\(m\) \(n\) \(mr\)
\(123\) \(456\) \(123 \times 6 = 738\)
\(1230\) \(45\) \(1230 \times 5 = 6150\)
\(12300\) \(4\) \(12300 \times 4 = 49200\)

\(\implies 123 \times 456 = 738 + 6150 + 49200 = 56088.\)

Note that this is essentially the standard algorithm for multiplying numbers.

  123
× 456
—————
  738
 6150
49200
—————
56088

Fast exponentiation

We have seen that we multiply numbers via repeated doubling. In a similar way, we can raise a number to a power via repeated squaring. Here is a Python implementation:

def power(m, n):
    p = 1
    while n > 0:
       if n % 2: # n is odd
            p = p * m
        m = m * m
        n = n // 2
    return p

The loop invariant \(Q = m^n p\) can be used to prove that the function correctly returns the value of \(m^n\).

Emoji and Zipf’s Law

Emojitracker.com is a very interesting site that tracks how often each emoji is used on Twitter. The statistics are updated in real time, and it’s really quite amazing to watch.

Screenshot from 2015-05-06 23:40:09

(In case the reader is unaware, an emoji is a small icon that is often used to convey ideas or emotions in electronic communications. It should not be confused with an emoticon, which uses ordinary text characters for a similar purpose.)

Is there is a formula that describes these frequencies? In 1935, the American linguist George Zipf proposed that, in any corpus of words, the frequency of a word is inversely proportional to its rank in the frequency table. That is, the most common word should be \(N\) times more frequent than the \(N\)th word. More generally, we may suppose that the frequencies follow an inverse power law of the form \(y = Cx^{-r}\), where \(y\) is the frequency and \(x\) is the rank.

Power laws can be identified using a log-log plot, which has logarithmic scales on both axes. When we take the logarithm of both sides, we get

\[\log y = \log C – r \log x\]

which appears on a log-log plot as a straight line with slope \((-r)\) .

Does the frequency of emojis follow a power law? To answer this question, we need to find a way to scrape the data from the webpage. My solution was crude but effective. I selected all of the text on the page, and used copy-and-paste to get it into a text file. I wrote a short Python script to extract the numbers into a list.

import re
with open("emoji.txt") as f:
    text = '\n'.join(f.readlines())
y = [int(m) for m in re.findall(r"\d+", text)]
x = range(1, len(y)+1)

Next, I used matplotlib to plot the data on a log-log scale, and scipy.stats to compute the line of best fit for the transformed data.

from math import log, exp
from scipy import stats
import matplotlib.pyplot as plt

logx = map(log, x)
logy = map(log, y)
slope, intercept, r_value, p_value, std_er = stats.linregress(logx[:40], logy[:40])
y_pred = [exp(slope*t + intercept) for t in logx]
plt.loglog(x, y)
plt.loglog(x, y_pred)
plt.xlabel('Rank')
plt.ylabel('Frequency')
plt.title('Frequency of Emoji characters on Twitter')
plt.grid(True)
plt.text(100, 3e7, r'$y = (%.3f \times 10^8)\ x^{%.3f}$' % (exp(intercept)/10**8, slope)) 
plt.text(100, 1e8, r'$(r^2 = \ %.4f)$' % r_value**2)
plt.savefig('emoji_zipf.png')
plt.show()

From the graph shown below, it appears that the numbers do not follow a power law. However, the 40 most common emojis do appear to follow a power law. The graph shows the frequencies for all of the emojis, with a line of best fit for the 40 most common emojis.

emoji_zipf

Primes with average digit 1

In this note, we will use Python to explore the following problem: How many primes are there whose average digit is 1? In other words, how many \(k\)-digit prime numbers exist whose sum of digits is \(k\)?

Here is a direct approach. We generate all prime numbers up to some limit and calculate the sum of digits for each prime. We use the primerange function from the SymPy package to generate the prime numbers.

from sympy import primerange

def digitsum(n):
    s = 0
    while n > 0:
        s += (n % 10)
        n //= 10
    return s

def primes_avg1(N):
    for n in primerange(2, N):
        if digitsum(n) == len(str(n)):
            yield n

Listed below are the prime numbers less than a million whose average digit is 1. Note that there are no examples with 3 or 6 digits. In fact, there is never an example with \(3k\) digits, because a number is divisible by 3 if and only if the sum of its digits is divisible by 3.

print list(primes_avg1(10**6))

[11, 1021, 1201, 2011, 3001, 
10103, 10211, 10301, 11003, 
12011, 12101, 13001, 20021, 
20201, 21011, 21101, 30011]

This method is inefficient for large numbers because prime numbers are relatively common, even when the number of digits is large. According to the prime number theorem, the number of prime numbers less than \(N\) is approximately \(N/\ln(N)\). The exact number of primes having \(k\) digits or fewer has been computed up to \(k = 26\). (See http://en.wikipedia.org/wiki/Prime-counting_function)

But the number of \(k\)-digit numbers with average digit 1 increases more slowly as a function of \(k\) than the number of \(k\)-digit prime numbers. We can estimate this number as follows. Any positive integer can be represented by a pattern of stars and bars, where the bars separate the stars into groups. For example, the pattern shown below represents the number 4102 because there are 4 stars in the first group, 1 star in the second group, 0 stars in the third group, and 2 stars in the fourth group.

★ ★ ★ ★ | ★ |   | ★ ★

If a number has \(k\) digits that sum to \(k\), then it can be represented by a certain arrangement of \(k\) stars and \((k-1)\) bars. The total number of permutations of \(k\) stars and \((k-1)\) bars is

\[\binom{2k-1}{k-1} = \frac{(2k-1)!}{(k-1)!\,k!}\]

which is less than \(2^{2k-1}\), since the sum of the numbers in row \((2k-1)\) of Pascal’s triangle is \(2^{2k-1}\). Therefore, the number of \(k\) digits with sum of digits \(k\) is less than \(2^{2k-1}\). Although this quantity grows exponentially with \(k\), it grows much more slowly than the number of \(k\)-digit primes.

The exact count of \(k\)-digit numbers with digit sum \(k\) is given by the following summation, which can be obtained by means of the inclusion-exclusion principle.

\[\sum_{j\ge0} (-1)^j \binom{k}{j} \binom{2k-10j-2}{k-1} .\]

Wait, where are you going with this?

Since \(k\)-digit primes are common, while \(k\)-digit numbers with digit sum \(k\) are rare, we realize that filtering a list of primes was a mistake. It is much better to generate a list of numbers having average digit 1, and test each of these numbers for primality. Fortunately, this list can be generated using an efficient recursive algorithm.

def gen(k, s):
    """k-digit numbers
       with digit sum s"""
    if k == 1:
        if s < 10:
            yield s
    else:
        for a in range(min(s, 10)):
            for m in gen(k-1, s-a):
                yield 10*m + a

As a further refinement, we can use the fact that the last digit of a prime greater than 5 is always 1, 3, 7, or 9. Here is our Python code for generating \(k\)-digit prime numbers with sum of digits \(s\).

from sympy.ntheory import primetest

def primes(k, s):
    for a in 1, 3, 7, 9:
        if s <= a:
            break
        for m in gen(k-1, s-a):
            n = 10*m + a
            if primetest.isprime(n):
                yield n

We can use this to function to count the number of \(k\)-digit primes with digit sum \(k\). Since we are using generators instead of lists, our code does not use a lot of memory. However, it is time consuming to test a large number for primality, so a probabilistic test is used.

for k in range(2, 18):
    if k % 3 > 0:
        print k, sum(1 
          for p in primes(k, k)) 

The output is shown below. The code uses the Miller-Rabin primality test, which is guaranteed to be correct for \(n < 10^{16}\) but has a small chance of error \((<10^{-28})\) for large inputs. Consequently, the last value in the table should be viewed as highly probable, but not certain.

Digits Count
2 1
4 4
5 12
7 95
8 212
10 2395
11 10657
13 126068
14 375941
16 4943357
17 20513691

Mahler’s Quinary Conundrum

The following question was posed by the German mathematician Kurt Mahler (1903 – 1988) in a letter that he wrote to Alf van der Poorten.

I am interested in the problem of whether there are squares of integers which, to the base g = 5, have only digits 0 or 1. I could not find a single example although I went quite far on my calculator.

Now some examples come immediately to mind. If \( n = 5^k\) then \( n^2 = 5^{2k}\) which is expressed in base 5 as \( 1000\ldots0\) with \( 2k\) zeros. But these solutions are trivial. We are more interested in solutions that are not divisible by 5, since they generate all other solutions. If \( n^2\) has only digits 0 or 1, then the same is true for \( (5^k \cdot n)^2\) and conversely. (The word “digits” always refers to base 5 digits in this note.)

I wrote a Python script to search for examples. The first step is to write a function to check if a number has only digits 0 or 1 in a given base.

def check(n, base):
    while n > 0:
        if n % base > 1:
            return False
        n = n // base
    return True

We could use this function to search for examples directly, but this is inefficient.

def naive_search(N):
    for n in range(N):
        if n % 5 > 0 and check(n*n, 5):
            yield n

It is inefficient because we are checking many values that could have been excluded from the outset. For example, the last digit of n must be 1 or 4, otherwise the last digit of \( n^2\) would be 4. We can find similar conditions on the last two digits, the last three digits, and so on.

  • The last two digits of n must be 01, 14, 31, or 44.
  • The last three digits of n must be 001, 031, 114, 144, 301, 331, 414, or 444.
  • The last four digits of n must be 0001, 0301, 0331, 0414, 1444, 2031, 2114, 2144, 2301, 2331, 2414, 3001, 4031, 4114, 4144, or 4444.

In fact, the number of possible \( k\)-digit endings is \( 2^k\). Each \( k\)-digit ending gives birth to two \( (k+1)\)-digit endings by adding digits to the left. I will leave it to the reader to prove this fact.

Here is a Python function that generates the possible \( k\)-digit endings. It works recursively by generating and extending the possible \( (k-1)\)-digit endings.

def gen(k):
    if k == 1:
        yield 1
        yield 4
    else:
        P = 5 ** (k-1)
        for n in gen(k-1):
            x = (n*n) // P
            delta = 2 * (n % 5) % 5
            d = x % 5
            m = n
            for a in range(5):
                if d < 2:
                    yield m
                d = (d + delta) % 5
                m += P        

The final step is to feed these candidates into our digit checker. By this means, I was able to find all positive integers n less than \( 5^{32} \approx 2.3 \times 10^{22}\) and not divisible by 5 such that \( n^2\) has only digits 0 or 1 when written in base 5.

results = sorted([n for n in gen(32) 
                    if check(n*n, 5)])

Results: 1, 972799, 3051273374, 6132750376, 839228035909, 3818357814376, 2384643515634376, 1490173338867234376, 931329727148437734376.

If we write these numbers in base 5, a pattern becomes apparent. Here is a Python script that converts the numbers to base 5.

def toBase5(n):
    if n < 5: 
        return n
    return 10 * toBase5(n//5) + (n % 5)

for n in results:
    print (toBase5(n))

# Base 10 Base 5
1 1 1
2 972799 222112144
3 3051273374 22222111221444
4 6132750376 100024441003001
5 839228035909 102222214334122114
6 3818357814376 1000024444100030001
7 2384643515634376 10000024444410000300001
8 1490173338867234376 100000024444441000003000001
9 931329727148437734376 1000000024444444100000030000001

One sees a similarity between the fourth, sixth, seventh, eighth, and ninth terms of this sequence. The runs of 0s and 4s increase by one in each step. This can be described by a polynomial function. If \( P(x) = 25x^4 + 15x^3 – 4x^2 + 3x + 1\) then these terms are \( P(5^3), P(5^4), P(5^5), P(5^6)\), and \( P(5^7)\).

We can verify that

$$
\begin{align*}
[P(x)]^2 & = 625x^8+750x^7+25x^6+30x^5 \\
& + 156x^4+6x^3+x^2+6x+1.
\end{align*}
$$

Note that the coefficients are positive integers less than \( 5^5\) and they can be written in base 5 using only 0s and 1s. Therefore, \( (P(5^k))^2\) has only digits 0 or 1 for all \( k \ge 3\).

This gives a striking, albeit partial, answer to Mahler’s question. There exist infinitely many squares that are not divisible by 5, but which have only the digits 0 or 1 when written in base 5.

Credits: Thanks to Gary Davis (@republicofmath) for bringing this problem to my attention. The problem is mentioned in the article The Legacy of Kurt Mahler, which appears in the May 2015 edition of the Notices of the American Mathematical Society.