From The Sunday Times, 31st October 1982 [link]
Teacher was introducing his class to the binary system of notation (wherein the unit values attaching to successive digits from right to left of any integer are 1, 2, 4, 8, 16, etc., as against 1, 10, 100, 1000, etc., in the decimal system).
He went on to explain that many arithmetical relationships are equally valid in both the binary and decimal systems. And gave as the simplest example:
10 × 11 = 110
which in the binary system represents 2 × 3 = 6, pointing out this difference however – that while both factors 10 and 11 are primes in the binary system, only 11 is prime in the decimal system.
Developing this theme he observed that very often such relationships could be described as “pan-palindromic”, in the sense that both of the factors, as well as their product, are numerical palindromes (i.e. each of the three integers reads the same forwards as backwards). His first example was:
1001 × 10101 = 10111101
(which in the binary system represents 9 × 21 = 189), and he pointed out how this time neither factor was a prime using either the binary or decimal system (being patently divisible by 11 and 3 respectively).
He contrasted this with another example:
111 × 1001001 = 111111111
which in the binary system represents: 7 × 73 = 511, where both factors are primes in the binary system, but neither of them are in the decimal system (both being divisible by 3).
To test how well his pupils were taking in all this, he told them to proceed on their own and write down any binary palindromes they could find, of less than twelve digits, which simultaneously, in both binary and decimal systems, factorised into just two different palindromic primes.
What should they have written down?
[teaser1057]
Jim Randell 10:07 am on 20 April 2023 Permalink |
This Python program runs in 52ms. (Internal runtime is 320µs).
from enigma import (irange, subsets, nsplit, nconcat, printf) # choose 7 digits in decreasing order for ds in subsets(irange(9, 0, step=-1), size=7): # the sum of the digits does not use any of the digits dsum = sum(ds) if any(d in ds for d in nsplit(dsum)): continue # compute the telephone number n = nconcat(ds) if n % dsum != 0: continue # output solution(s) printf("number = {n} [dsum = {dsum}]")Solution: Their telephone number is: 8654310.
The digit sum is 27 and:
LikeLike
Jim Randell 1:39 pm on 10 May 2023 Permalink |
Here is a version that uses the [[
SubstitutedExpression]] solver from the enigma.py library, using the newly implementedmacrofunctionality to tidy things up a bit.#! python3 -m enigma -rr SubstitutedExpression # suppose the 3 unused digits are X, Y, Z "X < Y" "Y < Z" # digit sum of the remaining digits is: 45 - (X + Y + Z) --macro="@ds = (45 - X - Y - Z)" # and must be composed entirely of digits in {X, Y, Z} --macro="@xs = {X, Y, Z}" "@xs.issuperset(nsplit(@ds))" # phone number is a multiple of ds --code="number = lambda xs: nconcat(diff(irange(9, 0, step=-1), xs))" "number(@xs) % @ds == 0" --answer="number(@xs)" --template=""LikeLike
Frits 11:11 am on 20 April 2023 Permalink |
from enigma import SubstitutedExpression vars = "ABCDEFG" d2i = {d: (vars[:6 - d] if d < 6 else "") + (vars[3 - d:] if d > 3 else "") for d in range(0, 10)} # the alphametic puzzle p = SubstitutedExpression( [ "A > B", "B > C", "C > D", "D > E", "E > F", "F > G", "A + B + C + D + E + F + G = HI", "div(ABCDEFG, HI)", ], answer="ABCDEFG", d2i=d2i, reorder=0, verbose=0, # use 256 to see the generated code ) # print answers for (_, ans) in p.solve(): print(f"{ans}")LikeLike
Jim Randell 2:54 pm on 20 April 2023 Permalink |
@Frits: I think I would allow H and I to have the same value.
You can allow this with the following parameter to [[
SubstitutedExpression]]:LikeLike
Frits 5:12 pm on 20 April 2023 Permalink |
@Jim, I erronuously thought that the total number also had to have different digits
LikeLike
Frits 5:15 pm on 20 April 2023 Permalink |
dgts = "0123456789" # decompose <t> into <k> numbers from <ns> # return number with decreasing digits def decompose(t, k, ns, s=[]): if k == 1: if t in ns: yield int("".join(str(x) for x in (s + [t])[::-1])) else: for (i, n) in enumerate(ns): if t - n < 0: continue yield from decompose(t - n, k - 1, ns[i + 1:], s + [n]) # try each possible total number for tot_nr in range(sum(range(7)), sum(range(3, 10)) + 1): # remaining digits rd = [int(x) for x in dgts if x not in str(tot_nr)] # total number must lie between sum 7 smallest and sum 7 highest digits if not (sum(x for x in rd[:7]) <= tot_nr <= sum(x for x in rd[-7:])): continue pr(tot_nr, rd, sum(x for x in rd[-7:])) # pick 7 digits with sum to <tot_nr> for y in decompose(tot_nr, 7, rd): if y % tot_nr == 0: print("answer:", y)LikeLike
GeoffR 9:14 pm on 20 April 2023 Permalink |
% A Solution in MiniZinc include "globals.mzn"; var 1..9:A; var 0..9:B; var 0..9:C; var 0..9:D; var 0..9:E; var 0..9:F; var 0..9:G; var 1..9:H; var 0..9:I; % 7 digits of the telephone number are in descending order constraint A > B /\ B > C /\ C > D /\ D > E /\ E > F /\ F > G; % Telephone number is ABCDEFG var 6543210..9876543: ABCDEFG == A * pow(10,6) + B * pow(10,5) + C * pow(10,4) + D * pow(10,3) + E * pow(10,2) + F * pow(10,1) + G; % Sum of digits in Telephone Number var 21..42: digsum = A + B + C + D + E + F + G; constraint H == digsum div 10; constraint I == digsum mod 10; % Divisor of telephone number uses digits H and I constraint ABCDEFG div digsum > 0 /\ ABCDEFG mod digsum == 0; % Digits H and I could be the same or different constraint card({A, B, C, D, E, F, G, H}) == 8 /\ card({A, B, C, D, E, F, G, I}) == 8; solve satisfy; output["Tel. Num. = " ++ show(ABCDEFG) ++ ", Digit sum = " ++ show(digsum)]; % Tel. Num. = 8654310, Digit sum = 27 % ---------- % ==========LikeLike