Chapter 60
Can You Climb Your Way To Victory?
Riddler Express
The sequence counts, for each , the number of ways to write as an ordered sum of two squares of integers (signs allowed). What is the average value of the whole sequence?
Solution
Write for the term: the number of integer solutions of . For instance , from and . The sequence is wildly irregular ( whenever ), so a running average is the right thing to ask for. Summing the first terms counts all integer points with , call it , so the average of is . As grows this tends to a single clean value:
, where .
Proof. This is Gauss’s circle argument. Place a unit square centred on each lattice point inside the disk of radius ; the squares have total area and roughly tile the disk. Every such square lies within distance (half a diagonal) of its centre, so all the squares fit inside the disk of radius , and they cover the disk of radius . Comparing areas, Dividing by and letting , both bounds tend to , and the squeeze theorem finishes it. ■
The computation
Count lattice points inside the disk of radius directly and watch settle towards .
from math import isqrt, pi
def t(n):
r = isqrt(n)
return sum(1 for x in range(-r, r + 1) for y in range(-r, r + 1)
if x*x + y*y <= n)
for n in (10, 100, 10_000, 1_000_000):
print(n, t(n) / (n + 1)) # -> 3.14159..., approaching pi
print(pi)
Riddler Classic
Eight climbers each get a rank through (no ties) in each of three events; a climber’s final score is the product of their three ranks, and lowest score wins. What is the highest score a climber could post and still be able to win or tie for first?
Solution
A climber can win with score only if every rival also scores at least , so the question is: how large can the smallest of the eight final scores be made? We want to assign the three rank-permutations to maximise the minimum product.
There is a ceiling. The eight final scores multiply to , since each event contributes , so their geometric mean is . The smallest score cannot exceed the geometric mean, so the best possible minimum is at most . A direct search over assignments shows the true ceiling is a little lower, and it is attainable: So a climber scoring can still tie for gold, but or more cannot, because no arrangement of ranks keeps all eight products that high.
The computation
Fix the first event’s ranks as the identity (relabelling climbers). For each permutation of the second event, find the third-event permutation that maximises the minimum product, via a bottleneck assignment (keep only edges with product and test for a perfect matching, raising as far as it will go).
from itertools import permutations
n, vals = 8, list(range(1, 9))
def perfect(adj): # Kuhn's matching, all rows matched?
matchR = [-1]*n
def aug(u, seen):
for v in adj[u]:
if not seen[v]:
seen[v] = True
if matchR[v] == -1 or aug(matchR[v], seen):
matchR[v] = u; return True
return False
return all(aug(u, [False]*n) for u in range(n))
best = 0
for s2 in permutations(vals):
W = [[(i + 1) * s2[i] * v for v in vals] for i in range(n)]
cand = sorted({w for row in W for w in row})
lo, hi, feas = 0, len(cand) - 1, 0
while lo <= hi: # binary search the bottleneck
mid = (lo + hi) // 2; T = cand[mid]
if perfect([[c for c in range(n) if W[i][c] >= T] for i in range(n)]):
feas = T; lo = mid + 1
else:
hi = mid - 1
best = max(best, feas)
print(best) # 48