How High Can Count Von Count Count?
Photo by Richard Termine at BBC.com

Riddler

How High Can Count Von Count Count?

A solution to this week's Riddler Express from fivethirtyeight.com.

There are two riddles from fivethirtyeight.com this week. I think the first one – called the “Riddler Express,” and about Count Von Count from Sesame Street – sounds more fun than the “Riddler Classic,” which is just another expected value problem. So, here’s Von Count’s conundrum:

Count Von Count, the counting count on “Sesame Street,” counts aloud on Twitter. If he counts up by one with each tweet — “One!” “Two!” “Three!” … “Five hundred thirty eight!” etc. — how high can he go before hitting the 140-character limit? Note: The count is enthusiastic and must end all of his tweets with an exclamation point.

There is a little lack of clarity here. Are we looking for the first number at 140 characters or the first number over 140 characters? I think the spirit of the puzzle suggests the second reading: “What’s the last number he can tweet?” So, we’re after the number that breaks the limit minus one.

This really is more of a riddle than a math problem, so let’s start with some observations. “Three” is the longest-lowest word for the ones place, and “seventy” is the longest-lowest word for the tens place. So, the longest two-digit number is “seventy three,” and the longest-lowest three-digit number is “three hundred seventy three.”

From here, we can see that we have to go up to at least a trillion, because the longest tweet we could spell out under this would be:

Three hundred seventy three billion three hundred seventy three million three hundred seventy three thousand three hundred seventy three!

But this is only 137 characters. So, we must add one to the trillions place, and in the millions, thousands, and hundreds places we keep the longest-lowest 373. The idea is that we want maximum length in lower places, and minimum values in higher places. That leaves us with missing digits in the billions, a sort of “fill-in-the-blank” problem to figure out.

So, what is lowest three-digit number that when spelled out is at least 18 characters in length? How do we know that we’re looking for a three-digit number at least 18 characters in length? R is convenient for character counting. We know our number is going to start with “one trillion  ” and end with “ billion three hundred seventy three million three hundred seventy three thousand three hundred seventy three!” So, we can easily calculate the number of characters we need to “fill” to break the 140 character limit.

# taking care with white spaces
start.string<-nchar("one trillion ") 
end.string<-nchar(" billion three hundred seventy three million three hundred seventy three thousand three hundred seventy three!")
141 - start.string - end.string
[1] 18

There is doubtless and easier way to do this, but I began with a function (see bottom of post for code), and then I looped through spellings starting with 1 000 373 373 373, and then adding a billion with each loop until the character count was exceeded. At 1 111 373 373 373, the limit is exceeded with 141 characters; therefore, the last number the count can tweet is one less than this, which is: 1 111 373 373 372. Here’s the last tweet the Count Von Count can make:

One trillion one hundred eleven billion three hundred seventy three million three hundred seventy three thousand three hundred seventy two!

And, here’s the tweet that exceeds the limit:

One trillion one hundred eleven billion three hundred seventy three million three hundred seventy three thousand three hundred seventy three!

That 111 is in the billions place is unsurprising, as this is the minimum value to also spell out all place names. Since we have all the data from this loop, it’s easy to plot the transition over the 140 character limit (again, see bottom of post for code). If the Count tweeted one number every second, it would take him nearly 600 years to reach this limit, by which time, Twitter will most likely be obsolete, thus rendering this whole riddle moot.

out<-matrix(nrow=200,ncol=3)
a<-1000000000000
c<-373373373
for(i in 1:200){
  b<-i*1000000000
  d<-a+b+c
  thecount<-paste0(spell.number(d),"!")
  out[i,1]<-d
  out[i,2]<-thecount
  out[i,3]<-nchar(thecount)
}
library(ggplot2)
out2<-data.frame(out)
colnames(out2)<-c("number","spelling","chars")
# annoying how data.frame() factorizes or characterizes everything
# there's a stringsAsFactors option but why no integersAsIntegeres?
out2$number <- as.numeric(as.character(out2$number))
out2$chars <- as.numeric(as.character(out2$chars))
ggplot(out2, aes(x=number, y=chars)) + geom_line(color="#45B29D") +
  geom_hline(aes(yintercept=140)) +
  xlab("\nThe number spelled out") +
  ylab("Number of characters in tweet\n") +
  theme(panel.background = element_rect(fill = "#ffffff")) +
  ggtitle("Count Von Count's Twitter Threshold")

plot of chunk the_count_1

Spell Number Function and Threshold Plot

# this function does not append the "!"
# nothing fancy here:
  # floor() get us the first three digits
  # the modulo operator (%%) gets us the last three digits
  # everything else just references a couple of lists
  # noting that the 'teens' need to be considered seperately
ones<-c("one","two","three","four","five","six","seven","eight","nine")
teens<-c("ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen", "eighteen","nineteen")
tens<-c("NOPE", "twenty", "thirty","forty","fifty","sixty","seventy","eighty","ninety")
library(stringr)
spell.number<-function(x){
if(x<10){mycount<-ones[x]}
else if(x<20){mycount<-teens[x-9]}
else if(x>=20){
  if(x<100){mycount<-paste0(tens[floor(x/10)], " ", spell.number(x%%10))}
  else if(x<1000){mycount<-paste0(spell.number(floor(x/100)), " hundred ", spell.number(x%%100))}
  else if(x<1000000){mycount<-paste0(spell.number(floor(x/1000)), " thousand ", spell.number(x%%1000))}
  else if(x<1000000000){mycount<-paste0(spell.number(floor(x/1000000)), " million ", spell.number(x%%1000000))}
  else if(x<1000000000000){mycount<-paste0(spell.number(floor(x/1000000000)), " billion ", spell.number(x%%1000000000))}
  else if(x<1000000000000000){mycount<-paste0(spell.number(floor(x/1000000000000)), " trillion ", spell.number(x%%1000000000000))}
}
mycount<-str_trim(mycount,"both")
return(mycount)

#and plot threshold
# this one does add the "!"

out<-matrix(nrow=200,ncol=3)
a<-1000000000000
c<-373373373
for(i in 1:200){
  b<-i*1000000000
  d<-a+b+c
  thecount<-paste0(spell.number(d),"!")
  out[i,1]<-d
  out[i,2]<-thecount
  out[i,3]<-nchar(thecount)
}
library(ggplot2)
out2<-data.frame(out)
colnames(out2)<-c("number","spelling","chars")
# annoying how data.frame() factorizes or characterizes everything
# there's a stringsAsFactors option but why no integersAsIntegeres?
out2$number <- as.numeric(as.character(out2$number))
out2$chars <- as.numeric(as.character(out2$chars))
ggplot(out2, aes(x=number, y=chars)) + geom_line(color="#45B29D") +
  geom_hline(aes(yintercept=140)) +
  xlab("\nThe number spelled out") +
  ylab("Number of characters in tweet\n") +
  theme(panel.background = element_rect(fill = "#ffffff")) +
  ggtitle("Count Von Count's Twitter Threshold")
}

Dialogue & Discussion