Chapter 36
Can You Find A Number Worth Its Weight In Letters?
The Riddler for January 10, 2020. The Express asks for the fraction of smallest denominator wedged between and . The Classic spells numbers out and adds up their letters, asking for the largest number that beats its own alphanumeric value.
Riddler Express
Of all the fractions (with whole-number numerator and denominator) that are greater than and less than , one has the smallest denominator. Which fraction is it?
The Riddler, FiveThirtyEight, January 10, 2020(original post)
Solution
The two bounds are no ordinary pair: and are neighbours in the Farey sense, meaning the cross-difference of their numerators and denominators is exactly : For neighbours with , the fraction of smallest denominator strictly between them is forced to be their mediant , and nothing with a smaller denominator can fit.
Here is the one-line reason. Suppose . The first inequality says , so the whole number is at least ; the second says likewise. Now use the neighbour identity to rewrite the denominator: So every in-between fraction has denominator at least , and equality needs both and , which pins to the mediant. The answer is A quick sanity check on the decimals: , then , then , so sits neatly between the two.
The computation
Forget the theory and just search by increasing denominator: for test whether some integer gives , and stop at the first that works.
from fractions import Fraction as F
lo, hi = F(1, 2020), F(1, 2019)
q = 1
while True:
p = next((p for p in range(int(lo*q)-1, int(hi*q)+2)
if lo < F(p, q) < hi), None)
if p is not None:
print(f"{p}/{q}") # 2/4039
break
q += 1
The first denominator admitting an in-between fraction is , and the fraction is , as boxed.
Riddler Classic
Assign to A, to B, and so on up to for Z, and give a word the total of its letters. So ONE is worth and TWO is worth , both bigger than the numbers they name. Larger numbers fall behind: (ONE THOUSAND FOUR HUNDRED SEVENTEEN) is worth only . Among all whole numbers that are less than their own alphanumeric value, what is the largest?
The Riddler, FiveThirtyEight, January 10, 2020(original post)
Solution
A number near is written with only a handful of words, and each word contributes a bounded letter total, so the alphanumeric value grows roughly like the length of the spelled-out number, far slower than itself. Past a modest point every number outruns its letters, so a largest qualifying number must exist, and it is small.
Searching upward, the values keep pace with the numbers only into the low hundreds. The last number that still beats its spelling is spelled TWO HUNDRED SEVENTY NINE, whose letters sum to . Every number above it spells out to less than itself.
The computation
Spell each number out, total its letters (), and take the largest number whose total exceeds it. A range up to a few thousand is far more than enough, since the gap only widens past .
ones = ("ZERO ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE TEN "
"ELEVEN TWELVE THIRTEEN FOURTEEN FIFTEEN SIXTEEN "
"SEVENTEEN EIGHTEEN NINETEEN").split()
tens = " TWENTY THIRTY FORTY FIFTY SIXTY SEVENTY EIGHTY NINETY".split(" ")
def spell(n):
if n < 20: return ones[n]
if n < 100: return tens[n // 10] + ones[n % 10] * (n % 10 != 0)
if n < 1000:
rest = spell(n % 100) if n % 100 else ""
return ones[n // 100] + "HUNDRED" + rest
rest = spell(n % 1000) if n % 1000 else ""
return spell(n // 1000) + "THOUSAND" + rest
value = lambda n: sum(ord(c) - 64 for c in spell(n))
print(max(n for n in range(1, 3000) if value(n) > n)) # 279