Tagged: by: Peter Morris Toggle Comment Threads | Keyboard Shortcuts

  • Unknown's avatar

    Jim Randell 8:38 am on 8 June 2023 Permalink | Reply
    Tags: by: Peter Morris   

    Teaser 2052: Back to back 

    From The Sunday Times, 13th January 2002 [link]

    Your Teaser today is to take the 10 digits 0 to 9. Use three of them to make a three-figure prime number. Then write that number down backwards (i,e. with the digits in reverse order). Then take three of the remaining digits, use them to form a larger prime number, and write that number down backwards. Finally, use the remaining four digits to form a four-figure perfect square and then write that number down backwards. You should do all this in such a way that the sum of the two reversed primes equals the reversed square.

    What are the two primes?

    [teaser2052]

     
    • Jim Randell's avatar

      Jim Randell 8:39 am on 8 June 2023 Permalink | Reply

      We can use the [[ SubstitutedExpression ]] solver from the enigma.py library to solve this puzzle.

      The following run file executes in 75ms. (Internal runtime of the generated program is 5.7ms).

      Run: [ @replit ]

      #! python3 -m enigma -rr
      
      #     C B A -> ABC is prime
      #  +  F E D -> DEF is prime
      #   -------
      #   J I H G -> GHIJ is square
      #   =======
      
      SubstitutedExpression
      
      "CBA + FED = JIHG"
      
      "is_prime(ABC)"
      "is_prime(DEF)"
      "A < D"
      "is_square(GHIJ)"
      
      --answer="(ABC, DEF)"
      

      Solution: The two primes are: 467 and 523.

      And the sum is:

      764 + 325 = 1089

      Like

    • Frits's avatar

      Frits 1:19 pm on 8 June 2023 Permalink | Reply

         
      from enigma import is_square_p
      from itertools import combinations
      
      # prime numbers between 100 and 1000 with different digits
      # without using digit 1
      P = [3, 5, 7]
      P += [x for x in range(11, 33, 2) if all(x % p for p in P)]
      P = [s for x in range(203, 1000, 2) if all(x % p for p in P) and 
           '1' not in (s := str(x)) and len(set(s)) == 3]
      
      # pick two primes 
      for p1, p2 in combinations(P, 2):
        # different digits
        if len(s := set(p1 + p2)) != 6: continue
        
        # sum of reversed primes should be a 4-digit number using different digits 
        sm = int(p1[::-1]) + int(p2[::-1])
        if sm < 1000 or s.difference(str(sm)) != s: continue
        # and a square
        if not is_square_p(sm): continue
        
        print(f"the two primes are {p1} and {p2}")
      

      Like

    • GeoffR's avatar

      GeoffR 7:46 am on 9 June 2023 Permalink | Reply

      
      from enigma import is_prime
      from math import isqrt
      
      def is_sq(x):
         return isqrt(x) ** 2 == x
      
      digits = set('1234567890')
      
      from itertools import permutations
      for p1 in permutations(digits, 3):
          A, B, C = p1
          if '0' in (A, C):continue
          # 1st prime
          ABC = A + B + C
          if not is_prime(int(ABC)):continue
          q1 = digits.difference([A, B, C])
          
          # find 2nd prime
          for p2 in permutations(q1, 3):
              D, E, F = p2
              if D < A: continue  # 2nd prime > 1st prime
              if '0' in (D, F):continue
              DEF = D + E + F
              if not is_prime(int(DEF)):continue
              q2 = q1.difference(p2)
              for p3 in permutations(q2):
                  G, H, I, J = p3
                  if '0' in (G, J):continue
                  GHIJ = G + H + I + J
                  rGHIJ = GHIJ[::-1]
                  
                  # check sum of two reversed primes equals the reversed square
                  if int(ABC[::-1]) + int(DEF[::-1]) == int(rGHIJ):
                      if is_sq(int(rGHIJ)):
                          print(f"Two primes are {ABC} and {DEF}.")
                                                  
      # Two primes are 467 and 523.
      
      

      Like

  • Unknown's avatar

    Jim Randell 10:46 am on 11 May 2023 Permalink | Reply
    Tags: by: Peter Morris   

    Teaser 1978: Sunday study 

    From The Sunday Times, 13th August 2000 [link]

    This TEASER is odd and it needs careful STUDY. Throughout, different letters consistently stand for different non-zero digits. Each letter on the lower line is the sum of the two letters above it (so that, for example, U = A + S).

    What is SUNDAY‘s number?

    [teaser1978]

     
    • Jim Randell's avatar

      Jim Randell 10:47 am on 11 May 2023 Permalink | Reply

      See also: Teaser 1688, Teaser 2533.

      We can solve this puzzle using the [[ SubstitutedExpression ]] solver from the enigma.py library.

      The following run file executes in 66ms. (Internal runtime of the generated program is 80µs).

      Run: [ @replit ]

      #! python3 -m enigma -rr
      
      SubstitutedExpression
      
      --digits="1-9"
      
      "T + E = S"
      "E + A = T"
      "A + S = U"
      "S + E = D"
      "E + R = Y"
      
      # TEASER is odd
      "R % 2 = 1"
      
      --answer="SUNDAY"
      

      Solution: SUNDAY = 469528.

      Like

    • GeoffR's avatar

      GeoffR 12:09 pm on 11 May 2023 Permalink | Reply

      
      % A Solution in MiniZinc
      include "globals.mzn";
      
      set of int: INT = 1..9;
      var INT: T; var INT: E; var INT: A;
      var INT: S; var INT: R; var INT: U;
      var INT: D; var INT: Y; var INT: N;
      
      constraint all_different([T, E, A, S, R, U, D, Y, N]);
      
      constraint S == T + E /\ T == E + A /\ U == A + S /\ D == S + E
       /\ Y == E + R /\ R mod 2 == 1;
      
      solve satisfy;
      output ["SUNDAY = " ++  "\(S)\(U)\(N)\(D)\(A)\(Y)" ];
      
      % SUNDAY = 469528
      % ----------
      % ==========
      
      

      Like

    • GeoffR's avatar

      GeoffR 1:47 pm on 11 May 2023 Permalink | Reply

      from itertools import permutations
      
      digits = {1, 2, 3, 4, 5, 6, 7, 8, 9}
      
      for p1 in permutations(range(1, 10), 4):
          T, E, A, S = p1
          if S != T + E:continue
          if T != E + A:continue
          
          # find 5 remaining digits
          q1 = digits.difference(p1)
          for p2 in permutations(q1):
             U, D, R, Y, N = p2
             if U != A + S:continue
             if D != S + E:continue
             if R % 2 != 1:continue  # TEASER is odd
             if Y != E + R:continue
             SUNDAY = 100000*S + 10000*U + 1000*N + 100*D + 10*A + Y
             print('SUNDAY = ', SUNDAY)
      
      
      

      Like

  • Unknown's avatar

    Jim Randell 9:23 am on 10 December 2020 Permalink | Reply
    Tags: by: Peter Morris   

    Teaser 1966: Square of primes 

    From The Sunday Times, 21st May 2000 [link]

    If you place a digit in each of the eight unshaded boxes, with no zeros in the corners, then you can read off various three-figure numbers along the sides of the square, four in a clockwise direction and four anticlockwise.

    Place eight different digits in those boxes with the largest of the eight in the top right-hand corner so that, of the eight resulting three-figure numbers, seven are prime and the other (an anticlockwise one) is a square.

    Fill in the grid.

    This puzzle is included in the book Brainteasers (2002). The puzzle text above is taken from the book.

    [teaser1966]

     
    • Jim Randell's avatar

      Jim Randell 9:24 am on 10 December 2020 Permalink | Reply

      We can use the [[ SubstitutedExpression ]] solver from the enigma.py library to solve this puzzle.

      The following run file executes in 86ms. (Internal runtime of the generated program is 15ms).

      Run: [ @replit ]

      #! python3 -m enigma -rr
      
      #  A B C
      #  D   E
      #  F G H
      
      SubstitutedExpression
      
      # no zeros in the corners
      --invalid="0,ACFH"
      
      # C is the largest number
      "C > max(A, B, D, E, F, G, H)"
      
      # all the clockwise numbers are prime
      "is_prime(ABC)"
      "is_prime(CEH)"
      "is_prime(HGF)"
      "is_prime(FDA)"
      
      # three of the anticlockwise numbers are prime
      "icount((ADF, FGH, HEC, CBA), is_prime) = 3"
      
      # and the other one is square
      "icount((ADF, FGH, HEC, CBA), is_square) = 1"
      
      # answer grid
      --answer="((A, B, C), (D, E), (F, G, H))"
      

      Solution: The completed grid looks like this:

      Like

      • Frits's avatar

        Frits 2:42 pm on 10 December 2020 Permalink | Reply

        @Jim,

        A further optimization would be to limit A, F, and H to {1, 3, 5, 7} and C to {7, 9}.

        Like

    • Frits's avatar

      Frits 10:54 am on 10 December 2020 Permalink | Reply

      A solution with miniZinc.

      % A Solution in MiniZinc
      include "globals.mzn";
      
      %  A B C
      %  D   E
      %  F G H
      
      % no zeros in the corners 
      var 1..9:A; var 0..9:B; var 1..9:C; 
      var 0..9:D; var 0..9:E; 
      var 1..9:F; var 0..9:G; var 1..9:H;
      
      % 3-digit numbers clockwise
      var 100..999: ABC = 100*A + 10*B + C;
      var 100..999: CEH = 100*C + 10*E + H;
      var 100..999: HGF = 100*H + 10*G + F;
      var 100..999: FDA = 100*F + 10*D + A;
      
      % 3-digit numbers anticlockwise
      var 100..999: CBA = 100*C + 10*B + A;
      var 100..999: HEC = 100*H + 10*E + C;
      var 100..999: FGH = 100*F + 10*G + H;
      var 100..999: ADF = 100*A + 10*D + F;
      
      constraint all_different([A, B, C, D, E, F, G, H]);
      
      predicate is_prime(var int: x) = 
      x > 1 /\ forall(i in 2..1 + ceil(sqrt(int2float(ub(x))))) 
      ((i < x) -> (x mod i > 0));
       
      predicate is_square(var int: y) = exists(z in 1..ceil(sqrt(int2float(ub(y)))))
       (z*z = y );
       
      % C is the largest number
      constraint C > max([A, B, D, E, F, G, H]);
      
      % all the clockwise numbers are prime
      constraint is_prime(ABC) /\ is_prime(CEH)
      /\ is_prime(HGF) /\ is_prime(FDA);
      
      % three of the anticlockwise numbers are prime
      constraint (is_prime(CBA) /\ is_prime(HEC)/\ is_prime(FGH))
      \/ (is_prime(CBA) /\ is_prime(HEC)/\ is_prime(ADF)) 
      \/ (is_prime(CBA) /\ is_prime(FGH) /\ is_prime(ADF))
      \/ (is_prime(HEC) /\ is_prime(FGH) /\ is_prime(ADF));
      
      % and the other one is square
      constraint is_square(CBA) \/ is_square(HEC)
      \/ is_square(FGH) \/ is_square(ADF);
       
      solve satisfy;
       
      output [ show(ABC) ++ "\n" ++ show(D) ++ " " ++ show(E) ++ "\n" ++ show(FGH) ];
      

      Like

    • GeoffR's avatar

      GeoffR 9:44 am on 11 December 2020 Permalink | Reply

      A solution in Python. formatted to PEP8:

      
      import time
      start = time.time()
      
      from itertools import permutations
      from enigma import is_prime
      
      digits = set(range(10))
      
      def is_sq(n):
        a = 2
        while a * a < n:
          a += 1
        return a * a == n
      
      for P1 in permutations(digits, 4):
        A, F, H, C = P1
        if C not in (7, 9):
          continue
        if 0 in (A, F, H, C):
          continue
        Q1 = digits.difference(P1)
        for P2 in permutations(Q1, 4):
          B, D, E, G = P2
          if C > max(A, B, D, E, F, G, H):
      
            # Four clockwise primes
            ABC, CEH = 100 * A + 10 * B + C, 100 * C + 10 * E + H
            HGF, FDA = 100 * H + 10 * G + F, 100 * F + 10 * D + A
            if sum([is_prime(ABC), is_prime(CEH),
                    is_prime(HGF), is_prime(FDA)]) == 4:
              ADF, FGH = 100 * A + 10 * D + F, 100 * F + 10 * G + H
              HEC, CBA = 100 * H + 10 * E + C, 100 * C + 10 * B + A
      
              # Three anti-clockwise primes
              if sum([is_prime(ADF), is_prime(FGH),
                      is_prime(HEC), is_prime(CBA)]) == 3:
      
                # One anti-clockwise square
                if sum([is_sq(ADF), is_sq(FGH),
                        is_sq(HEC), is_sq(CBA)]) == 1:
                  print(f"{A}{B}{C}")
                  print(f"{D} {E}")
                  print(f"{F}{G}{H}")
                  print(time.time() - start) # 0.515 sec
      
      # 389
      # 6 0
      # 157
      
      
      

      A solution in MiniZinc, first part similar to previous MiniZinc solution, the last part using a different approach to find the seven primes and the one square number:

      
      % A Solution in MiniZinc
      include "globals.mzn";
      %  A B C
      %  D   E
      %  F G H
       
      var 0..9:A; var 0..9:B; var 0..9:C; 
      var 0..9:D; var 0..9:E; var 0..9:F; 
      var 0..9:G; var 0..9:H;
      
      constraint A > 0 /\ C > 0 /\ H > 0 /\ F > 0;
      constraint all_different([A, B, C, D, E, F, G, H]);
       
      % Four 3-digit numbers clockwise
      var 100..999: ABC = 100*A + 10*B + C;
      var 100..999: CEH = 100*C + 10*E + H;
      var 100..999: HGF = 100*H + 10*G + F;
      var 100..999: FDA = 100*F + 10*D + A;
       
      % Four 3-digit numbers anti-clockwise
      var 100..999: CBA = 100*C + 10*B + A;
      var 100..999: HEC = 100*H + 10*E + C;
      var 100..999: FGH = 100*F + 10*G + H;
      var 100..999: ADF = 100*A + 10*D + F;
       
      set of int: sq3 = {n*n | n in 10..31}; 
       
      % Hakank's prime predicate
      predicate is_prime(var int: x) =
      x > 1 /\ forall(i in 2..1 + ceil(sqrt(int2float(ub(x))))) 
      ((i < x) -> (x mod i > 0));
       
      % C is the largest number
      constraint C > max([A, B, D, E, F, G, H]);
      
      % four clockwise prime numbers
      constraint sum([is_prime(ABC), is_prime(CEH), is_prime(HGF),
       is_prime(FDA)]) == 4;
       
      % three anti-clockwise prime numbers
      constraint sum([is_prime(CBA), is_prime(HEC), is_prime(FGH),
      is_prime(ADF)]) == 3;
      
      % one anti-clockwise square
      constraint sum([CBA in sq3, HEC in sq3, FGH in sq3,
      ADF in sq3]) == 1;
      
      solve satisfy;
        
      output ["Completed Grid: " ++ "\n"  ++ 
      show(ABC) ++ "\n" ++ 
      show(D) ++ " " ++ show(E) ++ "\n" ++ show(FGH) ];
      
      % Completed Grid: 
      % 389
      % 6 0
      % 157
      % ----------
      % ==========
      
      
      
      

      Like

    • Frits's avatar

      Frits 2:05 pm on 12 December 2020 Permalink | Reply

      Further analysis leads to C = 9 and square 361.

      from enigma import SubstitutedExpression
      
      #  A B C
      #  D   E
      #  F G H
      
      # the alphametic puzzle
      p = SubstitutedExpression(
        [
        # C = 9
        # All corner numbers have to be odd and can't be number 5"
        # (otherwise somewhere a number xx5 is not prime and has to be square.
        #  xx5 is either 225 or 625 but their 1st digit is not odd)
         
        # all the clockwise numbers are prime
        "is_prime(10*AB + 9)",  # ABC
        "is_prime(900 + EH)",   # CEH
        "is_prime(HGF)",
        "is_prime(FDA)",
         
        # three of the anticlockwise numbers are prime
        "icount((ADF, FGH, 10*HE + 9, 900 + BA), is_prime) = 3",
         
        # and the other one is square
        "icount((ADF, FGH, 10*HE + 9, 900 + BA), is_square) = 1",
       
        ],
        answer="((A, B, 9), (D, E), (F, G, H))",
        digits=range(0, 9),
        d2i=dict([(k, "AFH") for k in {0, 2, 4, 5, 6, 8}] +
                 [(k, "BEDG") for k in {1, 3, 7}]),
        distinct="ABDEFGH",
        verbose=0)   
      
      # Print answers 
      for (_, ans) in p.solve():
          print(f"{ans}")
      
      # ((3, 8, 9), (6, 0), (1, 5, 7))
      

      Only one square is possible:

      from enigma import is_prime
      
      alldiff = lambda seq: len(seq) == len(set(seq))
      
      sqs = []
      # check 3-digit squares
      for i in range(11, 32):
        i2 = i * i
        # digits in square must all be different 
        # and number must start/end with an odd digit
        if not(alldiff(str(i2)) and i2 % 2 == 1 and (i2 // 100) % 2 == 1):
          continue
        # reverse square has to be prime
        if not(is_prime(int(str(i2)[::-1]))): continue
        sqs.append(i2)
      
      print("possible squares:")  
      for sq in sqs: 
        print(sq)
      
      # possible squares:
      # 361
      

      Like

  • Unknown's avatar

    Jim Randell 9:19 am on 8 September 2019 Permalink | Reply
    Tags: by: Peter Morris   

    Brainteaser 1688: Sunday paper 

    From The Sunday Times, 22nd January 1995 [link]

    As usual, in this letters-for-digits puzzle different digits have consistently been replaced by different letters:

    Each letter in the second row is the sum of its two nearest letters in the row above.

    What is the value of READER?

    This puzzle is included in the book Brainteasers (2002). The puzzle text above is taken from the book.

    [teaser1688]

     
    • Jim Randell's avatar

      Jim Randell 9:20 am on 8 September 2019 Permalink | Reply

      I used the [[ SubstitutedExpression() ]] solver from the enigma.py library to solve the 5 sums.

      The following run files executes in 150ms.

      Run: [ @replit ]

      #! python3 -m enigma -rr
      
      SubstitutedExpression
      
      "S + U = P"
      "U + N = A"
      "N + D = P"
      "D + A = E"
      "A + Y = R"
      
      --answer="READER"
      

      Solution: READER = 694596.

      Like

    • GeoffR's avatar

      GeoffR 8:25 pm on 8 September 2019 Permalink | Reply

      from itertools import permutations
      
      for x in permutations (range(1,10)):
          s, u, n, d, a, y, p, e, r = x
          if s + u == p:
            if u + n == a:
              if n + d == p:
                if d + a == e:
                  if a + y == r: 
                    reader = 100001*r + 10010*e + 1000*a + 100*d
                    print("READER = ", reader)
                    # READER =  694596
      
      

      Like

c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel
Design a site like this with WordPress.com
Get started