Skip to content
Vamshi Jandhyala

Books · The Fiddler: Solutions

Chapter 13

Can You Pile the Primes?

As a warm-up, suppose you split the first N2N_2 prime numbers into two groups with equal sums. The smallest value that works is N2=3N_2 = 3: the primes 2,3,52,3,5 split as {5}\{5\} and {2,3}\{2,3\}, each summing to 55.

Your puzzle asks for three groups instead. Splitting the first N3N_3 primes into three groups with equal sums, what is the smallest N3N_3 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 33, so first find when the running sum of the primes is a multiple of 33. The sums of the first one through nine primes are never divisible by 33; the first time is at the tenth prime, 2+3+5+7+11+13+17+19+23+29=129=343.2+3+5+7+11+13+17+19+23+29 = 129 = 3\cdot 43. Divisibility is only necessary, so check that a split into three 4343s actually exists, and it does: {29,11,3}\{29,11,3\}, {23,13,7}\{23,13,7\}, {19,17,5,2}\{19,17,5,2\}. Since no smaller count even clears the divisibility test, the answer is N3=10.N_3 = \boxed{10}.

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 1010.

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 N6N_6 primes into six groups with equal sums. What is the smallest N6N_6?

Solution

The same reasoning applies, with the total now needing to be a multiple of 66 and each group large enough to hold the biggest prime. The first count meeting both conditions, and admitting an actual six-way split, is N6=57.N_6 = \boxed{57}. The first 5757 primes sum to 6870=611456870 = 6\cdot 1145, and they partition into six groups of 11451145. No smaller count works.

The computation

Exhibit a split for the first 5757 primes: peel off one group summing to the target 11451145 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