Teaser 2177: Basic sum
From The Sunday Times, 6th June 2004 [link]
You might look askance at the addition sum:
But in fact each of the numbers is written in a different base. Just one of the four numbers is prime and just one is divisible by three. Also, when written in the normal decimal form, the last digits of the three numbers being added are all different, and the total is still a four-figure number.
What is the total?
[teaser2177]

Jim Randell 11:52 am on 13 June 2023 Permalink |
See also: Enigma 1358.
This Python program collects numerical values of “1101” in various bases, such that the value is not more than 4 digits in decimal.
It then looks for three of these values that sum to a fourth, and applies the remaining conditions.
It runs in 56ms. (Internal runtime is 518µs).
Run: [ @replit ]
from enigma import ( irange, inf, base2int, subsets, icount_exactly, is_prime, seq_all_different, join, printf ) # is x divisible by 3? div3 = lambda x: x % 3 == 0 # find values for <s> in various bases d = dict() for b in irange(2, inf): n = base2int('1101', base=b) if n > 9999: break d[n] = b # look for 3 of the values that sum to a 4th for ns in subsets(d.keys(), size=3, fn=list): t = sum(ns) if t < 1000 or t not in d: continue # the last digits of the summands (in base 10) are all different if not seq_all_different(n % 10 for n in ns): continue ns.append(t) # exactly one of the 4 numbers is divisible by 3 if not icount_exactly(ns, div3, 1): continue # exactly one of the 4 numbers is prime if not icount_exactly(ns, is_prime, 1): continue # output solution bs = list(d[n] for n in ns) printf("{ns} = {t} [bases = {bs}]", ns=join(ns[:-1], sep=" + "), bs=join(bs, sep=", ", enc="()"), )Solution: The result of the sum is 7221 (in decimal).
The sum is:
LikeLike
Frits 12:01 pm on 14 June 2023 Permalink |
@Jim, you don’t seem to have a proper check on “the total is still a four-figure number”.
LikeLike
Jim Randell 12:12 pm on 14 June 2023 Permalink |
@Frits: Oops. I’ve added in a check for that at line 19.
LikeLike
GeoffR 10:21 pm on 13 June 2023 Permalink |
from enigma import is_prime, all_different from itertools import combinations # max base for 4 digits is 21 # sum is n1 + n2 + n3 = n4 for b1, b2, b3, b4 in combinations(range(2, 22),4): n4 = int('1101', base = b4) # the total n4 is a four-figure number if n4 < 10000: n1 = int('1101', base = b1) n2 = int('1101', base = b2) n3 = int('1101', base = b3) # one of the four numbers is prime if sum ((is_prime(n1), is_prime(n2), is_prime(n3),\ is_prime(n4))) == 1: # last digits of the three summands are all different if all_different( str(n1)[:-1], str(n2)[:-1], \ str(n3)[:-1]): # one of the four numbers is divisible by three if sum((n1 % 3 == 0, n2 % 3 == 0, n3 % 3 == 0, \ n4 % 3 == 0))== 1: # check total of sum is correct if n4 == n1 + n2 + n3: print(f"Sum is : {n1} + {n2} + {n3} = {n4}.") print(f"The bases are {b1}, {b2}, {b3} and {b4}.") # Sum is : 253 + 811 + 6157 = 7221. # The bases are 6, 9, 18 and 19.LikeLike
GeoffR 8:38 am on 14 June 2023 Permalink |
Quite slow in MiniZinc – nearly 3 sec with the Chuffed Solver.
LikeLike