Teaser 2489: [Digit circle]
From The Sunday Times, 6th June 2010 [link] [link]
Your task today is to put more than two digits evenly spaced around a circle so that:
(a) Any three adjacent digits clockwise form a prime number.
(b) Any three adjacent digits anti-clockwise form a prime number.
(c) If a digit occurs in the circle, then it occurs a prime number of times.
(d) The total of all the digits around the circle is a prime.
What is the total of all the digits?
This puzzle was originally published with no title.
[teaser2489]
Jim Randell 8:21 am on 15 July 2025 Permalink |
Each digit is the last digit of a 3-digit prime, so the only possible digits are 1, 3, 9, 7.
This Python program looks for solutions using increasing numbers of digits, and stops after the smallest possible sequences are found.
It runs in 60ms. (Internal runtime is 841µs).
Run: [ @codepad ]
from enigma import (irange, inf, primes, multiset, subsets, tuples, rev, nconcat, wrap, uniq, printf) # up to 3-digit primes primes.expand(999) # possible digits digits = [1, 3, 7, 9] # generate numbers from <k> adjacent digits of <ns> (in both directions) @wrap(uniq) # skip duplicate numbers def adj(ns, k=3): for ds in tuples(ns, k, circular=1): yield nconcat(ds) yield nconcat(rev(ds)) # look for solutions using <n> digits def solve(n): # choose the n digits for ds in subsets(digits, size=n, select='R'): # the sum of the digits is prime if not (sum(ds) in primes): continue # each digit occurs a prime number of times m = multiset.from_seq(ds) if not all(v in primes for v in m.values()): continue # start with the smallest number n0 = m.min() # and choose an arrangement of the rest for ns in subsets(m.difference([n0]), size=len, select='mP', fn=list): if rev(ns) < ns: continue # skip duplicate solutions ns.insert(0, n0) # check each 3 adjacent digits form a prime (in both directions) if not all(x in primes for x in adj(ns)): continue yield ns # find the smallest possible sequences for n in irange(3, inf): s = 0 for ns in solve(n): printf("[n={n}] {ns} -> {t}", t=sum(ns)) s += 1 if s: breakSolution: The total of the digits is 29.
The digits in the circle are: (1, 9, 1, 9, 9). Consisting of 2 occurrences of the digit 1, and 3 occurrences of the digit 9.
The 3-digit numbers formed are: 191, 199, 919, 991. Which are all prime.
LikeLike
Ruud 8:03 am on 16 July 2025 Permalink |
import istr import collections import itertools primes = [i for i in istr.range(2, 1000) if i.is_prime()] candidates = {str(prime) for prime in primes if len(prime) == 3 and all(c in "1379" for c in prime)} for n in itertools.count(2): for x in itertools.product("1379", repeat=int(n)): s = "".join(x) if all(count in primes for count in collections.Counter(s).values()): s2 = str(s + s[0:2]) if ( all(s2[i : i + 3] in candidates for i in range(len(s))) and all(s2[i : i + 3][::-1] in candidates for i in range(len(s))) and (sumx:=sum(map(int, x))) in primes ): print(s,sumx) assert FalseLikeLike