Brain-Teaser 902: Square telephones
From The Sunday Times, 19th November 1978 [link]
“I have discovered”, said Ian, “that if I square my five-figure telephone number, the square ends in the same four figures, in the same order, as the number itself, which, incidentally, does not begin with a zero”.
“Funnily enough”, said James, “the same things apply to my five-figure telephone number, which is different from Ian’s”.
“There are quite a few numbers which fulfil that condition”, I said. “Give me some more information”.
“If I were to tell you”, said Ian, “how many of the digits 1 to 9 do not appear in the square of my number, you could deduce the number”.
“And you could deduce mine, if were to tell you how many of the digits 1 to 9 do not occur in the square of my number”, added James.
“Then I already know enough to deduce your numbers”, I said, but I am not able to say whose is which”.
My number is divisible by 29″, said James.
This last statement enabled me to deduce Ian’s and James’s telephone numbers.
What are they?
[teaser902]







Jim Randell 11:47 am on 8 February 2022 Permalink |
This Python program runs in 60ms.
Run: [ @replit ]
from enigma import irange, sq, nsplit, filter_unique, printf # find possible 5-digit phone numbers (no leading 0) # whose square ends with the last 4 digits of the original number ps = list(n for n in irange(10000, 99999) if sq(n) % 10000 == n % 10000) # look for numbers which can be deduced from the number of unused # digits 1-9 in the square of the number digits = set(irange(1, 9)) fn = lambda n: len(digits.difference(nsplit(sq(n)))) ns = filter_unique(ps, fn).unique # from the candidates: divisible by 29 = James, else Ian for n in ns: x = ("James" if n % 29 == 0 else "Ian") printf("{x} = {n}")Solution: Ian’s number is 69376. James’ number is 60001.
69376² = 4813029376, which doesn’t use 5 (i.e. 1 of the digits 1-9).
60001² = 3600120001, which doesn’t use 4, 5, 7, 8, 9 (i.e. 5 of the digits 1-9).
LikeLike
GeoffR 4:26 pm on 9 February 2022 Permalink |
@Jim:
It looks as though James(not Ian’s) number should be 60001?
“My number is divisible by 29″, said James.
>>> divmod(69376,29) = (2392, 8) – Ian’s number must be 69376
>>> divmod(60001,29) = (2069, 0) – James’ number must be 60001
LikeLike
Jim Randell 4:33 pm on 9 February 2022 Permalink |
Quite right. I’ve corrected my test.
LikeLike
GeoffR 11:01 am on 11 February 2022 Permalink |
from enigma import irange, sq, nsplit from collections import defaultdict Nums = defaultdict(list) # re-using Jim's code list of phone numbers # find possible 5-digit phone numbers (no leading 0) # whose square ends with the last 4 digits of the original number ps = list(n for n in irange(10000, 99999) if sq(n) % 10000 == n % 10000) # digits 1-9 in the square of the number digits = set(irange(1, 9)) # form a dictionary, indexing phone numbers by unused digits for p in ps: ud = len(digits.difference(nsplit(sq(p)))) Nums[ud].append(p) # counter for single phone numbers in the dictionary cnt = 0 for k, v in Nums.items(): if len(v) == 1: cnt += 1 # find the two phone numbers for James and Ian if cnt == 2: for k, v in Nums.items(): if len(v) == 1: if v[0] % 29 != 0: print(f"Ian's phone number = {v[0]}") elif v[0] % 29 == 0: print(f"James' phone number = {v[0]}") # James' phone number = 60001 # Ian's phone number = 69376LikeLike