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 8:39 am on 8 June 2023 Permalink |
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 ]
Solution: The two primes are: 467 and 523.
And the sum is:
LikeLike
Frits 1:19 pm on 8 June 2023 Permalink |
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}")LikeLike
GeoffR 7:46 am on 9 June 2023 Permalink |
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.LikeLike