From The Sunday Times, 24th April 1983 [link]
Fred and I usually do the washing and drying up in our commune. Fred always washes the largest saucepan first, then the second largest, and so on down to the smallest. In this way I can dry them and store them away in the order they were washed, by putting one saucepan inside the previously washed saucepan, ending up with a single pile.
Yesterday, the cook misread the quantities for the sherry trifle recipe, with the result that Fred got the washing up order completely wrong. For example, he washed the second smallest saucepan immediately after the second largest; and the smallest saucepan immediately before the middle-sized saucepan (i.e. the saucepan with as many saucepans larger than it as there are smaller).
I realised something was wrong when, having started to put the saucepans away in the usual manner, one of the saucepans did not fit the previously washed saucepan; so I started a second pile. Thereafter each saucepan either fitted in to one, but only one pile, or did not fit into any pile, in which case I started another pile.
We ended up with a number of piles, each containing more than one saucepan. The first pile to be completed contained more saucepans than the second, which contained more than the third etc.
In what order were the saucepans washed up? (Answers in the form a, b, c, …, numbering the saucepans from 1 upwards, 1 being the smallest).
This puzzle is included in the book The Sunday Times Book of Brainteasers (1994).
[teaser1081]
Jim Randell 9:44 am on 1 August 2024 Permalink |
This Python program runs in 81ms. (Internal runtime is 381µs).
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