# A quick python hack for a mathematical puzzle

Published: Sat 12 November 2011

So, today I saw this in my Twitter feed:

«Phil Harvey wants us to partition {1,…,16} into two sets of equal size so each subset has the same sum, sum of squares and sum of cubes.» -- posted by @MathsJam and retweeted @haggismaths.

Sounds implausible was my first though. My second thought was that there aren't actually ALL that many of those: we can actually test this.

So, here's a short collection of python lines to _do_ that testing.

import itertools allIdx = range(1,17) sets = itertools.combinations(allIdx,8) setpairs = [(list(s), [i for i in allIdx if i not in s]) for s in sets] def f((s1,s2)): return (sum(s1)-sum(s2), sum(map(lambda n: n**2, s1))-sum(map(lambda n: n**2, s2)), sum(map(lambda n: n**3, s1))-sum(map(lambda n: n**3, s2)))

goodsets = [ss for ss in setpairs if f(ss) == (0,0,0)]

And back comes one single response (actually, two, because the comparison is symmetric).

[([1, 4, 6, 7, 10, 11, 13, 16], [2, 3, 5, 8, 9, 12, 14, 15]),  ([2, 3, 5, 8, 9, 12, 14, 15], [1, 4, 6, 7, 10, 11, 13, 16])]