Enigmatic Code

Programming Enigma Puzzles

Enigma 1180: Anomalies

From New Scientist #2336, 30th March 2002 [link]

SEVEN is a prime, and as one would expect, SEVEN minus THREE equals FOUR. But perversely, FOUR is a prime, (and is also a prime when the digits are in reverse order), so THREE is not a prime. Another anomaly is that TEN is a perfect square.

Each different capital letter above represents a different digit, which is the same for that letter everywhere.

Find the numerical values of FOUR and TEN.

[enigma1180]

Advertisements

4 responses to “Enigma 1180: Anomalies

  1. Jim Randell 15 February 2016 at 8:51 am

    We can use the SubstitutedSum() solver from the enigma.py library to find candidate solutions.

    This Python program runs in 60ms.

    from enigma import SubstitutedSum, is_square, is_prime, printf
    
    p = SubstitutedSum(['THREE', 'FOUR'], 'SEVEN')
    for s in p.solve():
      # TEN is a perfect square
      TEN = int(p.substitute(s, 'TEN'))
      if not is_square(TEN): continue
      # FOUR is a prime
      FOUR = int(p.substitute(s, 'FOUR'))
      if not is_prime(FOUR): continue
      # and RUOF is also a prime
      RUOF = int(p.substitute(s, 'RUOF'))
      if not is_prime(RUOF): continue
      # check THREE is not a prime
      THREE = int(p.substitute(s, 'THREE'))
      if is_prime(THREE): continue
      # and SEVEN is also a prime
      SEVEN = THREE + FOUR
      if not is_prime(SEVEN): continue
    
      printf("FOUR={FOUR} TEN={TEN} [SEVEN={SEVEN} THREE={THREE}]")
    

    Solution: FOUR = 3407. TEN = 529.

    • geoffrounce 18 February 2016 at 8:28 am
      % A solution in MiniZinc  
      
      include "globals.mzn";
      solve satisfy;
      
      var 0..9: E;  var 0..9: V; var 0..9: N; var 0..9: T; var 0..9: H;
      var 0..9: R;  var 0..9: F; var 0..9: O; var 0..9: U; var 0..9: S;
      
      array[1..10] of var int : fd = [E,V,N,T,H,R,F,O,U,S];
      
      constraint all_different(fd) /\ S>0 /\ T>0 /\ F>0;
      
      % is_square 
      predicate is_square(var int: c) =
         let {
            var 0..ceil(pow(int2float(ub(c)),(1/2.0))): z
         } in z * z = c ;
      
      % is_prime 
      predicate is_prime(var int: x) = x > 1 /\
         forall(i in 2..1 + ceil(sqrt(int2float(ub(x))))) ( 
              (i < x) -> (x mod i > 0));
      
      % SEVEN is prime
      constraint is_prime(S*10000 + E*1000 + V*100 + E*10 + N);
      
      % SEVEN - FOUR = THREE
      constraint (S*10000 + E*1000 + V*100 + E*10 + N)
       - (F*1000 + O*100 + U*10 + R) == (T*10000 + H*1000 + R*100 + E*11);
      
      % FOUR is a prime, inc its reversed digits
      constraint is_prime (F*1000 + O*100 + U*10 + R)
      /\ is_prime(R*1000 + U*100 + O*10 + F);
      
      % THREE is not a prime
      constraint not is_prime (T*10000 + H*1000 + R*100 + E*11);
      
      % TEN is a perfect square
      constraint is_square(T*100 + E*10 + N);
      
      output ["SEVEN is " ++ show(S),show(E),show(V),show(E),show(N) ++
      "\n" ++ "TEN is  " ++ show(T), show(E),show(N) ++
      "\n" ++ "FOUR is " ++ show(F),show(O),show(U),show(R) ++
      "\n" ++ "THREE is " ++ show(T),show(H),show(R),show(E),show(E)];
      
      % Output
      % -------
      % SEVEN is 62129
      % TEN is  529
      % FOUR is 3407
      % THREE is 58722
      % --------------
      % Finished in 245msec
      %
      
      • geoffrounce 21 February 2016 at 11:01 am
        # A faster Python 3 solution (125 msec)
        
        from itertools import permutations
        digits = set('1234567890')
        
        def is_square(x):
          return int(x ** 0.5 + 0.5) ** 2 == x
        
        def is_prime(n): 
            for x in range(2, int(n**0.5)+1): 
                if n % x == 0: 
                    return False 
            return True
         
        # construct SEVEN
        for (s,e,v,n) in permutations(digits,4):
          if s == '0' : continue
          seven = int(s + e + v + e + n)
          if not is_prime(seven): continue
          
          #construct TEN and THREE
          for (t,h,r) in permutations(digits.difference(set((s,e,v,n))),3):
            if t == '0': continue
            ten = int(t + e + n)
            if not is_square(ten): continue
            three = int(t + h + r + e + e)
            if is_prime(three) : continue
                 
            #construct FOUR
            for (f,o,u) in permutations(digits.difference(set((s,e,v,n,t,h,r))),3):
                if f == '0' : continue
                four = int(f + o + u + r)
                if not is_prime(four):  continue
                ruof = int(r + u + o + f)
                if not is_prime(ruof): continue
        
                #test sum SEVEN - FOUR = THREE
                if seven - four != three: continue
                print('FOUR = {}, TEN = {}, SEVEN ={}, THREE = {}' \
                      .format(four, ten, seven, three))
        
        # FOUR = 3407, TEN = 529, SEVEN =62129, THREE = 58722
        
        
        
    • Jim Randell 25 October 2016 at 10:32 pm

      The following run file gives the arguments to the SubstitutedExpression() solver in the enigma.py library necessary to solve this puzzle.

      #!/usr/bin/env python -m enigma -r
      
      # solver to use
      SubstitutedExpression
      
      # solver parameters
      --answer="(FOUR, TEN)"
      
      # expressions to solve
      "is_prime(SEVEN)"
      "SEVEN - THREE = FOUR"
      "is_prime(FOUR)"
      "is_prime(RUOF)"
      "not is_prime(THREE)"
      "is_square(TEN)"
      

      It can be run as follows:

      % python -m enigma -r enigma1180.run
      (is_prime(SEVEN)) (SEVEN - THREE = FOUR) (is_prime(FOUR)) (is_prime(RUOF)) (not is_prime(THREE)) (is_square(TEN))
      (is_prime(62129)) (62129 - 58722 = 3407) (is_prime(3407)) (is_prime(7043)) (not is_prime(58722)) (is_square(529)) / E=2 F=3 H=8 N=9 O=4 R=7 S=6 T=5 U=0 V=1 / (3407, 529)
      (FOUR, TEN) = (3407, 529) [1 solution]
      

      Execution time is 81ms.

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: