Christian - Husband & Father - Web Developer

Letter Counting Function (and My First Python Script!)

Lots of numbers!

So I was watching Matt Parker's "Four has Four Letters" video, and I was interested to know how the Estonian language compared to English with regard to the letter counting function.

To summarise the video, in English each number has a word (or words) and each of those words have a certain number of letters. In all cases except the number 4, the word has a different number of letters than the number it represents. You can count these letters, then make a chain of numbers, all of which, in English, eventually lead you back to 4.

Example time! The word for the number 8, 'eight', has 5 letters. 'Five' has 4 letters, and 'four' also has 4 letters. So we can make a chain like so: 8 > 5 > (4), where 4 keeps looping back on itself. Longer chains are possible, and in other languages it's also possible to find chains that don't end up with one number looping back on itself, but with a few numbers that all loop to each other. For example, in French 4 ('quatre') > 6 ('six') > 3 ('trois') > 5 ('cinq') > 4 ('quatre') > etc. So you get a loop of 4 numbers!

At the end of the video, Matt gave some homework which was to:

  1. See if you could find a chain, in English, longer than 7 (7 was the longest chain he found)
  2. See if you could find a language with a loop larger than 4 (4 was the longest loop he found, in French)
  3. See if you could find a language/system that terminated at a number greater than 18 (18 being the largest termination value he found, in binary)

I wanted to have a go at this, and also to see how the Estonian language compared, so I decided to write some code that would calculate this for me. Being a web developer, I already know PHP and some Javascript, however these languages are geared towards web applications. So I did some research and found that Python is a generic programming language that is fairly easy to learn and, incidentally, seems to be what Matt also uses.

So I started learning Python! What I've seen so far has impressed me. It's simple and elegant. It's easy to pick up for me, knowing PHP, so I'm enjoying using it. I ended up writing a script that calculates number chains, finds loops, etc. The hard part originally was working out how to convert a number (4) to a word (four). I thought about using a spreadsheet to map words to numbers, and then save that as a CSV file which the Python script could read, but it seems too difficult to work out how to easily map words to numbers, especially in different languages. So I did some searching and found num2words, a Python module that converts numbers to words, and in different languages. Unfortunately Estonian isn't one of the currently-supported languages, and I don't know the language enough to try submitting a pull request to add it, so I won't get to see how Estonian compares to English. But I can see how other languages compare, and I can answer Matt's homework questions:

  1. In English, the longest chain I found (counting up to 1,000,000) was also 7 (e.g. 124 > 23 > 11 > 6 > 3 > 5 > (4))
    I also tried counting chains up to 1,000,000,000, but it took too long to run and I got sick of waiting (over an hour)
  2. Counting up to 1,000,000 in 21 languages, I also found that French has the longest chain, at 4
  3. Counting up to 1,000,000 in 21 languages, I found that Latvian and Russian both have loops at 11 (unfortunately nowhere near binary's 18)

Some other interesting stats that I discovered:

  • Polish has a maximum chain length of 10 (e.g. '269 > 29 > 19 > 14 > 11 > 10 > 8 > (5 > 4 > 6)'). 28,955 chains (out of 1,000,000) have that length.
  • Lithuanian has the most number of termination loops: 4. All 1,000,000 chains end up at one of: (5), (6 > 4), (2) or (7).

So that was fun, right? I enjoyed it anyway. Looking forward to seeing what else I can do with Python in future...