Books · The Fiddler: Solutions
Chapter 13
Can You Pile the Primes?
As a warm-up, suppose you split the first prime numbers into two groups with equal sums. The smallest value that works is : the primes split as and , each summing to .
Your puzzle asks for three groups instead. Splitting the first primes into three groups with equal sums, what is the smallest for which this is possible?
The Fiddler, Zach Wissner-Gross, March 27, 2026(original post)
Solution
Equal thirds require the total to be divisible by , so first find when the running sum of the primes is a multiple of . The sums of the first one through nine primes are never divisible by ; the first time is at the tenth prime, Divisibility is only necessary, so check that a split into three s actually exists, and it does: , , . Since no smaller count even clears the divisibility test, the answer is
The computation
Add primes one at a time and, after each, test whether the list so far splits into three equal-sum groups by a backtracking placement (each prime dropped into a bin without exceeding the target third). The first count that succeeds is .
from sympy import primerange
def can_split(nums, k): # split the full list into k equal-sum groups?
s = sum(nums)
if s % k: return False
t, nums = s // k, sorted(nums, reverse=True)
if nums[0] > t: return False
pots = [0] * k
def place(i):
if i == len(nums): return True
seen = set()
for j in range(k):
if pots[j] in seen or pots[j] + nums[i] > t: continue
seen.add(pots[j]); pots[j] += nums[i]
if place(i + 1): return True
pots[j] -= nums[i]
if pots[j] == 0: break
return False
return place(0)
g = primerange(2, 10**6); P = []
while True:
P.append(next(g))
if can_split(P, 3): print(len(P)); break # 10
Extra Credit
Now split the first primes into six groups with equal sums. What is the smallest ?
Solution
The same reasoning applies, with the total now needing to be a multiple of and each group large enough to hold the biggest prime. The first count meeting both conditions, and admitting an actual six-way split, is The first primes sum to , and they partition into six groups of . No smaller count works.
The computation
Exhibit a split for the first primes: peel off one group summing to the target at a time by a subset-sum pass, then confirm all six groups hit the target and nothing is left over.
def peel(nums, target): # one subset summing to target
reach = {0: []}
for x in nums:
for s in sorted([k for k in reach if k + x <= target], reverse=True):
reach.setdefault(s + x, reach[s] + [x])
if target in reach: break
return reach.get(target)
P = list(primerange(2, 300))[:57]
t, pool, groups = sum(P) // 6, list(P), []
for _ in range(6):
g = peel(pool, t); groups.append(g)
for x in g: pool.remove(x)
print(t, all(sum(g) == t for g in groups), not pool) # 1145 True True