Skip to content
Vamshi Jandhyala

Books · The Riddler

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 1/20201/2020 and 1/20191/2019. 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 1/20201/2020 and less than 1/20191/2019, 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: 1/20201/2020 and 1/20191/2019 are neighbours in the Farey sense, meaning the cross-difference of their numerators and denominators is exactly 11: 2020120191=1.2020\cdot 1 - 2019\cdot 1 = 1. For neighbours ab<cd\tfrac{a}{b} < \tfrac{c}{d} with bcad=1bc - ad = 1, the fraction of smallest denominator strictly between them is forced to be their mediant a+cb+d\tfrac{a+c}{b+d}, and nothing with a smaller denominator can fit.

Here is the one-line reason. Suppose ab<pq<cd\tfrac{a}{b} < \tfrac{p}{q} < \tfrac{c}{d}. The first inequality says pqab=pbaqqb>0\tfrac{p}{q} - \tfrac{a}{b} = \tfrac{pb - aq}{qb} > 0, so the whole number pbaqpb - aq is at least 11; the second says cqdp1cq - dp \ge 1 likewise. Now use the neighbour identity bcad=1bc - ad = 1 to rewrite the denominator: q=q(bcad)=b(cqdp)+d(pbaq)b1+d1=b+d.q = q\,(bc - ad) = b\,(cq - dp) + d\,(pb - aq) \ge b\cdot 1 + d\cdot 1 = b + d. So every in-between fraction has denominator at least b+d=2020+2019=4039b + d = 2020 + 2019 = 4039, and equality needs both cqdp=1cq - dp = 1 and pbaq=1pb - aq = 1, which pins pq\tfrac{p}{q} to the mediant. The answer is 24039.\boxed{\dfrac{2}{4039}}. A quick sanity check on the decimals: 1/20200.000495051/2020 \approx 0.00049505, then 2/40390.000495172/4039 \approx 0.00049517, then 1/20190.000495301/2019 \approx 0.00049530, so 2/40392/4039 sits neatly between the two.

The computation

Forget the theory and just search by increasing denominator: for q=1,2,3,q = 1, 2, 3, \ldots test whether some integer pp gives 1/2020<p/q<1/20191/2020 < p/q < 1/2019, and stop at the first qq 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 40394039, and the fraction is 2/40392/4039, as boxed.

Riddler Classic

Assign 11 to A, 22 to B, and so on up to 2626 for Z, and give a word the total of its letters. So ONE is worth 15+14+5=3415+14+5 = 34 and TWO is worth 5858, both bigger than the numbers they name. Larger numbers fall behind: 1,4171{,}417 (ONE THOUSAND FOUR HUNDRED SEVENTEEN) is worth only 379379. 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 NN 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 NN 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 279,\boxed{279}, spelled TWO HUNDRED SEVENTY NINE, whose letters sum to 284>279284 > 279. Every number above it spells out to less than itself.

The computation

Spell each number out, total its letters (A=1,,Z=26A=1,\dots,Z=26), 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 279279.

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