Chapter 55
Can You Slice The Ice?
A menorah has a central shamash with four numbered candles to its left and four to its right: the pair nearest the shamash are both “,” the next pair out “,” then “,” then “” at the ends. The shamash is always lit. In how many ways can you light some of the other eight candles so the lit numbers sum to the same total on each side? (Lighting candle and on one side, and on the other, both summing to , is one such way.)
Solution
A lit pattern picks a subset of on the left and a subset on the right, and it is balanced when the two have equal sums. So count, for each possible common total , how many subsets of sum to ; the left and right choices are independent, so that count is squared.
The subset sums of split up as: one subset each for sums and ; two each for sums ; one each for sums . (Sum is the empty side, which would leave that side dark, so we drop it.) Squaring and adding the positive totals,
The computation
Enumerate every way to light a nonempty set among the eight candles and keep those whose left and right lit numbers sum to the same total.
from itertools import product, combinations
candles = list(product(["l", "r"], range(1, 5))) # (side, number)
def side(comb, s): return sum(i for t, i in comb if t == s)
count = 0
for k in range(2, len(candles) + 1):
for comb in combinations(candles, k):
if side(comb, "l") == side(comb, "r"):
count += 1
print(count) # 25