Another way, using `PowerRepresentations[]`: s1 = Join@@ (Select[PowersRepresentations[748,#,2], Intersection[#, Range@16] == # &] & /@ Range@8) Gives all possible ways to total `748`. For filtering the complementary ones: s = Tally[s1, Union[#1, #2] == Range@16 &][[All, 1]]; Length@s (* 57 *)