Enigmatic Code

Programming Enigma Puzzles

Enigma 1650: Some prime magic

From New Scientist #2816, 11th June 2011 [link]

The diagram below is a magic square in which the letters A, B, …P, Q each represent a positive whole number not exceeding 200. Taken together, the 16 letters form, in some order, an arithmetic sequence of 16 terms. The letters E, N, I, G, M and A form, in that order, an increasing arithmetic sequence of six prime numbers.

Enigma 1650

Find the product C × H × J × L.

[enigma1650]

Advertisements

One response to “Enigma 1650: Some prime magic

  1. Jim Randell 5 December 2011 at 5:15 pm

    The following Python code runs in 36ms.

    from enigma import irange, is_prime, is_distinct, printf
    
    # (E, N, I, G, M, A) forms an arithmetic progression of primes < 200
    
    # find primes < 200
    primes = list(filter(is_prime, irange(3, 199, step=2)))
    
    n = len(primes)
    
    for (e, E) in enumerate(primes):
      for i in irange(e + 1, n - 1):
        N = primes[i]
        d = N - E
        I = N + d
        if not I in primes: continue
        G = I + d
        if not G in primes: continue
        M = G + d
        if not M in primes: continue
        A = M + d
        if not A in primes: continue
    
        print(d, (E, N, I, G, M, A))
    
        # the other letters must fit in between, also in an arithmetic progression
        # and the common difference of the full sequence must be a factor of d
    
        s = set((E, N, I, G, M, A))
        for d0 in irange(1, d):
          if d % d0 > 0: continue
          for a in irange(1, E):
            s0 = list(filter(lambda x: x < 200, (a + n * d0 for n in range(16))))
            if len(s0) < 16: continue
            if not s.issubset(s0): continue
            print(d0, s0)
         
            # now consider the magic square:
            # A B N C  x _ x _
            # D F H G  _ _ _ x
            # M J E K  x _ x _
            # L I P Q  _ x _ _
    
            s0 = set(s0)
            s0.difference_update(s)
            for B in s0:
              for C in s0:
                if C == B: continue
                c = A + B + N + C
    
                for D in s0:
                  if not is_distinct(D, B, C): continue
                  L = c - (A + D + M)
                  if not L in s0: continue
                  if not is_distinct(L, B, C, D): continue
    
                  for F in s0:
                    if not is_distinct(F, B, C, D, L): continue
                    H = c - (D + F + G)
                    if not H in s0: continue
                    if not is_distinct(H, B, C, D, L, F): continue
                    J = c - (B + F + I)
                    if not(L + J + H + C == c): continue
                    if not J in s0: continue
                    if not is_distinct(J, B, C, D, L, F, H): continue
                    K = c - (M + J + E)
                    if not K in s0: continue
                    if not is_distinct(K, B, C, D, L, F, H, J): continue
                    P = c - (N + H + E)
                    if not P in s0: continue
                    if not is_distinct(P, B, C, D, L, F, H, J, K): continue
                    Q = c - (C + G + K)
                    if not(L + I + P + Q == c): continue
                    if not(A + F + E + Q == c): continue
                    if not Q in s0: continue
                    if not is_distinct(Q, B, C, D, L, F, H, J, K, P): continue
    
                    printf("CxHxJxL={CHJL} A={A} B={B} C={C} D={D} E={E} F={F} G={G} H={H} I={I} J={J} K={K} L={L} M={M} N={N} P={P} Q={Q}", CHJL=C*H*J*L)
    

    Solution: C x H x J x L = 13742001.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: