Skip to content

Instantly share code, notes, and snippets.

@Fiona-J-W
Last active January 30, 2019 00:43
Show Gist options
  • Save Fiona-J-W/cf88b41a23cd5d92d7ca2dd387260c81 to your computer and use it in GitHub Desktop.
Save Fiona-J-W/cf88b41a23cd5d92d7ca2dd387260c81 to your computer and use it in GitHub Desktop.
def scalar_product(lhs, rhs):
return sum((l*r for l,r in zip(lhs, rhs)))
def possible_sums(primes):
n = len(primes)
factors = ([(2*int(x) - 1) for x in f"{m:0{n}b}"] for m in range(2**n))
return sorted({ scalar_product(primes, factor) for factor in factors })
def some_primes():
primes = [2,3,5,7,11,13,17,19,23,29]
for i in range(2,len(primes)):
yield primes[:i]
for primes in some_primes():
print(possible_sums(primes))
[-5, -1, 1, 5]
[-10, -6, -4, 0, 4, 6, 10]
[-17, -13, -11, -7, -3, -1, 1, 3, 7, 11, 13, 17]
[-28, -24, -22, -18, -14, -12, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 18, 22, 24, 28]
[-41, -37, -35, -31, -27, -25, -23, -21, -19, -17, -15, -13, -11, -9, -7, -5, -3, -1, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 31, 35, 37, 41]
[-58, -54, -52, -48, -44, -42, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 48, 52, 54, 58]
[-77, -73, -71, -67, -63, -61, -59, -57, -55, -53, -51, -49, -47, -45, -43, -41, -39, -37, -35, -33, -31, -29, -27, -25, -23, -21, -19, -17, -15, -13, -11, -9, -7, -5, -3, -1, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 67, 71, 73, 77]
[-100, -96, -94, -90, -86, -84, -82, -80, -78, -76, -74, -72, -70, -68, -66, -64, -62, -60, -58, -56, -54, -52, -50, -48, -46, -44, -42, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 90, 94, 96, 100]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment