Teaser 2584: Truncation
From The Sunday Times, 1st April 2012 [link] [link]
Callum is learning about squares and primes. He told me that he had discovered a four-figure square that becomes a three-figure prime if you delete its last digit. I could not work out his square from this information and he informed me that, if I knew the sum of its digits, then I would be able to work it out. So I then asked whether the square had a repeated digit: his answer enabled me to work out his square.
What was it?
[teaser2584]
Jim Randell 9:44 am on 1 August 2024 Permalink |
This Python program runs in 81ms. (Internal runtime is 381µs).
Run: [ @replit ]
from enigma import ( powers, is_prime, filter_unique, dsum, is_duplicate, singleton, group, printf ) # find 4-digit squares, where the first 3 digits form a prime sqs = list(n for n in powers(32, 99, 2) if is_prime(n // 10)) # select numbers unique by digit sum ns = filter_unique(sqs, f=dsum).unique # group results by whether there are repeated digits g = group(ns, by=is_duplicate) # look for unique values for (k, vs) in g.items(): n = singleton(vs) if n is not None: printf("(duplicate = {k}) => n = {n}")Solution: Callum’s number was: 5476.
There are 8 candidate squares that can be truncated to a prime, but only 3 have a unique digit sum:
Two of these 3 have repeated digits, so Callum must have answered that square did not have repeated digits, which uniquely identifies 5476 as his number.
LikeLike
GeoffR 4:17 pm on 1 August 2024 Permalink |
from collections import defaultdict from enigma import is_prime, dsum ans = defaultdict(list) sq4 = [ x**2 for x in range(32, 100)] for n1 in sq4: flag = 0 # for repeating or non-repeating digits n2 = int(str(n1)[0:3]) if is_prime(n2): # non rpeeating digits in squares if len(set(str(n1))) == len(str(n1)): flag = 1 ans[(flag, dsum(n1))] += [n1] # repeating digits in squares if len(set(str(n1))) != len(str(n1)): flag = 2 ans[(flag, dsum(n1))] += [n1] for k, v in ans.items(): print(k,v) ''' (1, 19) [1936, 4096] <<< multiple squares for digit sum (2, 10) [2116] <<< same number of repeating digits as square 3136 (2, 13) [3136] <<< same number of repeating digits as square 2116 (1, 22) [5476] <<< Answer (2, 25) [5776, 8836] <<< multiple squares for digit sum (1, 25) [7396] <<< same digit sum as squares 5776, 8836 '''LikeLike
Ruud 8:50 pm on 1 August 2024 Permalink |
from istr import istr import math import collections def is_prime(n): if n < 2: return False if n % 2 == 0: return n == 2 # return False k = 3 while k * k <= n: if n % k == 0: return False k += 2 return True squares_per_sum_digits = collections.defaultdict(list) for i in range(round(math.sqrt(1000)), round(math.sqrt(10000))): square = istr(i * i) if is_prime(square[:-1]): prime = square[:-1] sum_digits = sum(n for n in square) squares_per_sum_digits[sum_digits].append(square) potential_squares = [squares[0] for squares in squares_per_sum_digits.values() if len(squares) == 1] has_repeated_digits = collections.defaultdict(list) for square in potential_squares: has_repeated_digits[len(set(square))!=4].append(square) solution = [square[0] for square in has_repeated_digits.values() if len(square) == 1] print(*solution)LikeLike
GeoffR 7:57 pm on 3 August 2024 Permalink |
% A Solution in MiniZinc include "globals.mzn"; var 1..9:A; var 0..9:B; var 1..9:C; var 0..9:D; var 1024..9081: sq_dig4 == 1000*A + 100*B + 10*C + D; set of int: sq4 = {n * n | n in 32..99}; predicate is_prime(var int: x) = x > 1 /\ forall(i in 2..1 + ceil(sqrt(int2float(ub(x))))) ((i < x) -> (x mod i > 0)); % Square with last digit deleted is a prime constraint is_prime(100*A + 10*B + C); constraint sq_dig4 in sq4; solve satisfy; output[ "Digit sum = " ++ show(A + B + C + D) ++ ", Square = " ++ show(sq_dig4) ]; % Digit sum = 10, Square = 2116 - repeating digits - 2nd stage elimination % ---------- % Digit sum = 19, Square = 1936 - not a square with a single digit sum - 1st stage elimination % ---------- % Digit sum = 13, Square = 3136 - repeating digits - 2nd stage eliminations % ---------- % Digit sum = 25, Square = 8836 - not a square with a single digit sum - 1st stage elimination % ---------- % Digit sum = 22, Square = 5476 - *** ANSWER *** (unique digit sum, no repeating digits) % ---------- % Digit sum = 25, Square = 5776 - not a square with a single digit sum - 1st stage elimination % ---------- % Digit sum = 19, Square = 4096 - not a square with a single digit sum - 1st stage elimination % ---------- % Digit sum = 25, Square = 7396 - not a square with a single digit sum - 1st stage elimination % ---------- % ==========LikeLike