Enigmatic Code

Programming Enigma Puzzles

Enigma 1627: A riddle for the sphinx

From New Scientist #2792, 25th December 2010 [link]

Those with a classical education will know that the sphinx was a riddle-setting monster associated with the Greek city of Thebes. I took the sum


and replaced letters consistently with digits. Even if I told you the value of B, you still could not find all the digits, but if in addition I told you that PSI and PHI were prime, you could.




2 responses to “Enigma 1627: A riddle for the sphinx

  1. jimrandell 14 December 2011 at 11:56 am

    This Python program runs in 65ms.

    I have another version that runs in 34ms that does more early rejection based on the fact 3×A must end in A, and 3×TA must end in TA, but it’s a much longer program. In fact, it’s pretty obvious what A and T must be given this constraint, and if you modify this program to start with those values for A and T it only takes 32ms to run.

    from itertools import permutations
    from enigma import is_prime, printf
    d = set(range(10))
    for (E, T, A, B, H) in permutations(d, 5):
      TA = 10*T + A
      ETA = 100*E + TA
      BETA = 1000*B + ETA
      THETA = 10000*T + 1000*H + ETA
      if not(DELTA % 100 == TA): continue
      s = str(DELTA)
      if len(s) != 5: continue
      if not(int(s[-4]) == E): continue
      D = int(s[-5])
      L = int(s[-3])
      d1 = d.difference((E, T, A, B, H, D, L))
      if len(d) - len(d1) != 7: continue
      for (S, I, P) in permutations(d1, 3):
        PSI = 100*P + 10*S + I
        if not is_prime(PSI): continue
        PHI = 100*P + 10*H + I
        if not is_prime(PHI): continue
        THEBES = 100000*T + 10000*H + 1010*E + 100*B + S
        printf("ETA={ETA} BETA={BETA} THETA={THETA} DELTA={DELTA} A={A} T={T} E={E} L={L} B={B} H={H} D={D}")
        printf("THEBES={THEBES} PSI={PSI} PHI={PHI} S={S} I={I} P={P}")

    Solution: THEBES = 542823.

  2. Jim Randell 8 January 2013 at 3:27 pm

    Here’s a solution that uses the SubstitutedSum() solver from the enigma.py module. Although it’s not that much shorter, but it is a little bit faster – it runs in 45ms.

    from collections import defaultdict
    from itertools import permutations
    from enigma import irange, is_prime, SubstitutedSum, printf
    # record solutions to the sum by the value of B
    r = defaultdict(list)
    p = SubstitutedSum(['ETA', 'BETA', 'THETA'], 'DELTA')
    for s in p.solve():
    # consider non-unique solutions for B
    ds = set(irange(0, 9))
    for (k, v) in r.items():
      if len(v) < 2: continue
      for s in v:
        # the remaining digits are P, S, I
        for x in permutations(ds.difference(s.values())):
          s.update(zip('PSI', x))
          PSI = p.substitute(s, 'PSI')
          if not is_prime(int(PSI)): continue
          PHI = p.substitute(s, 'PHI')
          if not is_prime(int(PHI)): continue
          THEBES = p.substitute(s, 'THEBES')
          printf("{p.text} / {s}", s=p.substitute(s, p.text))
          printf("THEBES={THEBES} PSI={PSI} PHI={PHI}")

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: