Enigmatic Code

Programming Enigma Puzzles

Enigma 1229: Four five six

From New Scientist #2385, 8th March 2003 [link]

Harry, Tom and I have been challenged to find a solution to the following statement in which digits have been consistently replaced by capital letters, different letters being used for different digits and no number starting with a zero:

FOUR is a perfect square, FIVE is a Fibonacci number, SIX is a semi-prime.

In the sequence of Fibonacci numbers the first two terms are 1 and 1, and every other term is the sum of the previous two terms. A semi-prime is the product of two prime numbers.

Harry, Tom and I have each found a different solution. None of the numbers used in my solution appears in either of theirs.

What are the numbers represented by FOUR, FIVE and SIX (in this order) in my solution?


2 responses to “Enigma 1229: Four five six

  1. Jim Randell 31 May 2015 at 7:31 am

    This Python program runs in 42ms. We could prune the candidate lists of numbers by noting that they cannot contain repeated digits.

    from itertools import count, combinations
    from enigma import irange, factor, printf
    # <n> digit numbers from generator <g>, as strings
    def numbers(n, g):
      for x in g:
        s = str(x)
        m = len(s)
        if m < n: continue
        if m > n: break
        yield s
    # 4-digit squares
    squares = list(numbers(4, (x * x for x in count(1))))
    # generator for fibonacci sequences
    def fib(a, b):
      while True:
        yield a
        (a, b) = (b, a + b)
    # 4-digit fibonacci numbers
    fibs = list(numbers(4, fib(1, 1)))
    # 3-digit semi-primes
    semis = list(str(x) for x in irange(100, 999) if len(factor(x)) == 2)
    # record candidate sets
    r = list()
    # choose FIVE
    for FIVE in fibs:
      # all digits must be different
      if len(set(FIVE)) != 4: continue
      # choose FOUR
      for FOUR in squares:
        # they should match on F
        if FOUR[0] != FIVE[0]: continue
        # and all other digits should be different
        if len(set(FOUR + FIVE)) != 7: continue
        # choose SIX
        for SIX in semis:
          # they should match on I
          if FIVE[1] != SIX[1]: continue
          # and all other digits should be different
          if len(set(FOUR + FIVE + SIX)) != 9: continue
          r.append((FOUR, FIVE, SIX))
          printf("[FOUR={FOUR} FIVE={FIVE} SIX={SIX}]")
    # now choose three candidate sets for Tom, Dick and Harry
    for ss in combinations(r, 3):
      # choose one set of numbers for Dick
      for (i, D) in enumerate(ss):
        s = set(D)
        # none of Dick's numbers can appear in the other two sets
        if any(s.intersection(X) for (j, X) in enumerate(ss) if j != i): continue
        # output a solution
        printf("D={D} [ss={ss}]", D=', '.join(D), ss=' / '.join(', '.join(x) for x in ss))

    Solution: FOUR=1024, FIVE=1597, SIX=358.

    Harry and Tom’s numbers are (FOUR=2601, FIVE=2584, SIX=753) and (FOUR=2916, FIVE=2584, SIX=753) – which have the same values for FIVE and SIX – but we don’t know who found which set.

  2. geoffrounce 11 May 2017 at 2:04 pm
    % A Solution in MiniZinc
    include "globals.mzn";
    var 1..9:F; var 0..9:O; var 0..9:U; var 0..9:R; var 0..9:I; 
    var 0..9:V; var 0..9:E; var 1..9:S; var 0..9:X;  
    constraint alldifferent([F,O,U,R,I,V,E,S,X]);
    var 1000..9999: FOUR = 1000*F + 100*O + 10*U + R;
    var 1000..9999: FIVE = 1000*F + 100*I + 10*V + E;
    var 100..999: SIX = 100*S + 10*I + X;
    int: max_val = 1000;
    set of int: primes = {2} union (2..max_val diff 
    { i | i in 2..max_val, 
    j in 2..ceil(sqrt(i)) where i mod j = 0});
    set of int: semi_primes = {i * j | i in primes, j in primes 
    where i * j <= max_val };
    set of int: sq4 = {n*n | n in 32..99};
    set of int: fib4 = {1597,2584,4181,6765};
    % FOUR is a perfect square and FIVE is a Fibonacci number
    constraint FOUR in sq4 /\ FIVE in fib4;
    % SIX is a semi-prime
    constraint SIX in semi_primes;
    solve satisfy;
    output ["FOUR, FIVE, SIX = " ++ show(FOUR) ++ ", " ++
    show(FIVE) ++ ", " ++ show(SIX) ++ "\n" ];
    % Multiple output configuration used
    % FOUR, FIVE, SIX = 1024, 1597, 358    <<< My solution
    % FOUR, FIVE, SIX = 2601, 2584, 753    <<< Harry or Tom's solution
    % FOUR, FIVE, SIX = 2916, 2584, 753    <<< Harry or Tom's solution
    % Finished in 130msec

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: