I periodically attempt the FiveThirtyEight Riddler, edited by Oliver Roeder. This week he’s actually presenting two, a shorter “Riddler Express”, and a more time consuming “Riddler Classic”.

The Sesame Street character Count von Count likes to, well, count, and he now has his own twitter feed! For those who don’t recall the character from the show, he’s a purple muppet dressed in black, as a sort of kindly Dracula, who would count up in an eastern European accent.

Well, the twitter feed is simply the Count counting, albeit in words written out describing the number. As I type this, his latest tweet is “Eight Hundred Seventy Nine!”

So the Riddler Express is to find out how high can Count von Count count on twitter in this way before hitting its 140 character limit. Because he is enthusiastic, all his tweets must end with an exclamation mark.

I actually first tried to write a Perl script to brute force the answer before realizing that the number was big, and if I simply counted sequentially until I hit 140, it might literally take years if the program just started at one. But in building a program simply to convert a number to text, you do have to break the problem down into easier steps.

I classified each of the numeric words by length. Of digits, three, seven, and eight are longest at 5 letters. For tens, seventy is longest at 7, followed by twenty, thirty, eighty, and ninety at 6 letters. And among words for numbers between 10 and 19, seventeen is 9 letters (longest) followed by thirteen, fourteen, eighteen, and nineteen at 8, fifteen and sixteen at 7, and eleven and twelve at 6 letters.

In a given place, you’d want the lowest value number for a given length to maximize length while minimizing numeric value. Digits to the right should tend to be lower than ones to the left.

You can start by finding lengths of numbers containing only the digit 7 until you get one longer than the target. 777777777777 works out to just 137 letters, but 7777777777777 is 152 letters. First you can replace 7s that aren’t in a place that uses the word seventy with 3s to keep the same length. That gets me to 3373373373373 and a length of 152. Next I can start replacing digits at the front with shorter, lower values and stay at/above 140 total. Replacing the first 4 digits with 1s gets me to 1111373373373, whose length in text is 141 characters. So I can’t replace the first 3 in that number with a 2 or 1, since that would put me down to 139. But twenty is, at 6 letters long, just 1 letter less than seventy, so I can replace the first 7 with a 2 to get the first 140 character tweet, 1111323373373. And so Count von Count could go one less, 1111323373372, before hitting the 140 character limit.

Of course, twitter *does* allow 140 characters in a tweet, so the Count can indeed tweet that, and many more following. He only can’t keep going on Twitter when his length becomes greater than 140. So our earlier waypoint, 1111373373373, is the smallest number that is too big to fit in a tweet, and thus 1111373373372 would be the last number Count von Count could successfully count consecutively on Twitter.

Now suppose the Count likes spatial efficiency (or, perhaps his device’s space bar is broken). Then he could use “CamelCase“, mashing together the words in a number without spaces between them, but capitalizing the first letter of each word. If he does that, the Count can reach higher numbers before running out of space. Now 111123373373373 is the first number to reach exactly 140 characters, and 111173373373373 is the first to hit 141, leaving 111173373373372 as the last number Count von Count could consecutively count in CamelCase.

OneHundredElevenTrillionOneHundredSeventyThreeBillionThreeHundredSeventyThreeMillionThreeHundredSeventyThreeThousandThreeHundredSeventyTwo!

Ah ah ah!