Library · The Fiddler: Solutions · Chapter 127
Can You Sum Some Cards?
You and a friend have a large deck of cards, each numbered , , or , with plenty of each number. You take turns laying one card at a time on a pile. If at any moment the most recently played group of cards has the same sum as a group of cards played immediately before it, you both slap the pile. For example, each of these sequences is slapped when its last card is played:
(the last two cards sum to , as do the two before them);
(the last card equals the card before it);
(the last two cards sum to , as does the card before them).
How many cards are in the longest possible sequence that is never slapped?
The Fiddler, Zach Wissner-Gross, February 16, 2024(original post)
The official solution appeared in the post of February 23, 2024 and gives cards, with the single longest sequence , agreeing with the answer below. The Extra Credit half of that post is paywalled, so the values , and are my own.
Solution
Written out in full, a sequence is never slapped when there are no positions with that is, no two neighbouring blocks of cards with equal sums. (A slap is checked every time a card is played, so in the finished sequence no such pair of blocks may appear anywhere.)
Block sums are differences of running totals, and that is the idea that tidies everything up. Let and let be the total after cards. The two blocks above have sums and , so they are equal exactly when which says that , , are three equally spaced numbers. So the puzzle becomes this: choose running totals , each step between and , so that no three of them form an arithmetic progression. The cards are just the steps.
Small cases show how tight this is. With only s, the totals are in progression, so one card is the most. With cards up to , the steps must alternate (two equal steps make a progression), and with totals works while every fourth card fails, so three is the most. With cards up to , the longest sequences have seven cards, for example with totals .
For cards up to , the sequence has totals To confirm there is no progression, take each total as a possible middle term and each smaller total ; the third term would be , and in every case that number is not on the list. (For instance, with middle the smaller totals would need , none of which appears.) So nine cards can be played. That nine is the most, and that this is the only nine-card sequence, takes an exhaustive search: there is no short hand argument, and the official solution also turns to the computer at this point. The search below tries every sequence, abandoning each as soon as it is slapped, and finds no unslapped sequence of ten cards.
The computation
The program plays the game as stated. It builds sequences one card at a time, and after each new card it checks every pair of neighbouring blocks that ends with that card, comparing their sums directly. A sequence that is slapped is not extended. It records the longest sequences found for each highest card from to .
def slapped(seq):
right = 0
for q in range(len(seq) - 1, 0, -1):
right += seq[q] # block ending at the end
left = 0
for p in range(q - 1, -1, -1):
left += seq[p] # block just before it
if left == right:
return True
if left > right:
break
return False
def longest(N):
best = [[]]
def grow(seq):
if len(seq) > len(best[0]):
best[:] = [seq[:]]
elif len(seq) == len(best[0]):
best.append(seq[:])
for a in range(1, N + 1):
seq.append(a)
if not slapped(seq):
grow(seq)
seq.pop()
grow([])
return best
for N in range(1, 5):
best = longest(N)
print(N, len(best[0]), len(best), best[0])
It prints the highest card, the longest length, how many sequences reach it, and one of them:
1 1 1 [1]
2 3 2 [1, 2, 1]
3 7 4 [1, 3, 2, 3, 1, 3, 2]
4 9 1 [1, 4, 3, 4, 2, 4, 3, 4, 1]
With cards up to the longest unslapped sequence has nine cards, and there is exactly one.
Extra Credit
Suppose the cards are numbered through . How many cards are in the longest sequence that is never slapped when , and ?
Solution
Nothing in the reformulation depended on the number . For any , the question is how many steps of size to can be taken from without three of the totals lying in arithmetic progression. Longer steps leave more room, so the answers grow, but not in a simple pattern. The values for are , which suggests , but gives , not , so no tidy formula is at work. The answers for , and come from the exact search that follows, which finds these witnesses: The search also shows that none of them can be extended or beaten. The record is a block of fifteen cards, then a , then the same block again, the shape the official solution noticed for and .
The computation
For and the direct search from The computation above runs in under a second. For it takes about half a minute, so the program also runs a faster search on the running totals, which keeps the set of totals so far and rejects a new total whenever some earlier total has among them (only totals can be the middle term, so it looks back just that far). The two searches must agree wherever both are run.
def longest_totals(N):
S, seen, best = [0], {0}, [0]
def makes_progression(s):
for t in reversed(S):
if 2*t < s:
return False
if 2*t - s in seen:
return True
return False
def grow():
best[0] = max(best[0], len(S) - 1)
for g in range(1, N + 1):
s = S[-1] + g
if makes_progression(s):
continue
S.append(s); seen.add(s)
grow()
S.pop(); seen.discard(s)
grow()
return best
for N in (5, 6):
print(N, "direct:", len(longest(N)[0]),
"totals:", longest_totals(N)[0])
print(7, "totals:", longest_totals(7)[0])
It prints
5 direct: 15 totals: 15
6 direct: 21 totals: 21
7 totals: 31