From The Sunday Times, 15th March 1981 [link]
Young Mary, who is good at manipulating figures, was playing with her standard set of dominoes and seeing what numerical structures she could form with them, treating each half-domino as a digit according to its number of spots (“blank” being 0).
Starting with double-6/double-5/double-4 laid end-to-end in a row, she built forwards and downwards from that beginning, placing dominoes across and apparently at random, until she had completed a solid rectangle by slotting into the only remaining gaps, in its farther half, her last two dominoes which were 4-&-blank and 2-&-blank.
Whereupon she called out to me: “Uncle, do come and look! I’ve managed to arrange all my dominoes in a rectangle so that the numbers formed by the four halves in each column are all dissimilar 4-digit squares”.
“Are you quite sure about that?” I temporised.
“Certain,” she replied. “And they’re pretty well in correct order of magnitude too!”.
When I protested that this was quite impossible with dominoes, she said reproachfully: “But Uncle, I never said that ALL the squares appear in the right order, did I? Actually there are just two of them — both among the seven smallest present — which do not; but every single square except those two is definitely just where it would be if all those appearing were strictly in order of magnitude”.
She was perfectly correct. And you don’t need dominoes to figure out …
Which four of Mary’s together formed columns 7 and 8 of her rectangle? (Use figures, e.g. 4-0 = four-&-blank).
This was the final puzzle to use the title Brain-Teaser. The next puzzle was Brain teaser 974.
[teaser973]
Jim Randell 4:58 pm on 16 June 2023 Permalink |
If I understand this correctly the mechanics of it are:
The input signal is initially multiplied by a 2-digit value (= I, the “intrinsic gain”), and then goes through 3 stages where it is reduced by fractions a/p, b/q, c/r, where each of the fractions is less than 1/20, and p, q and r are prime numbers. This gives the “effective gain” E, which is a whole number, and which has the same first digit as I.
So:
Here’s my initial stab at this.
It runs in 137ms. (Internal runtime is 38ms).
from enigma import ( primes, irange, first, subsets, cproduct, div, nsplit, fdiv, unpack, seq2str, sprintf, printf ) # map prime denominators to numerators that give fractions < 1/20 nums = lambda p: first(irange(1, p), count=(lambda n: 20 * n < p)) d = dict((p, nums(p)) for p in primes.between(20, 99)) # choose the prime denominators (p, q, r) for (p, q, r) in subsets(d.keys(), size=3): Rd = p * q * r # and choose the numerators (a, b, c) for (a, b, c) in cproduct(d[k] for k in (p, q, r)): # calculate the ratio E/I (= Rn / Rd) Rn = (p - a) * (q - b) * (r - c) # choose a 2-digit number for I (= intrinsic gain) for I in irange(10, 99): # calculate E (= effective gain) (a whole number) E = div(I * Rn, Rd) if E is None: continue # check E and I have the same first digit if nsplit(E)[0] != nsplit(I)[0]: continue # order the fractions fs = sorted([(a, p), (b, q), (c, r)], key=unpack(fdiv)) # output solution printf("{fs} [I={I} E={E}]", fs=seq2str(sprintf("{x}/{y}") for (x, y) in fs))Solution: The three fractions are: 1/41, 3/89, 2/43.
The intrinsic gain (I) is 89, and so the effective gain (E) is:
Without the requirement that the first digits of E and I are the same we find there are 5 potential solutions:
LikeLike
Hugo 6:03 pm on 16 June 2023 Permalink |
A more realistic way of looking at it is that each stage is designed to have a certain gain, the product of the three gains being I. In practice the circuitry is imperfect and each gain is reduced by a certain fraction, so that the overall gain E is somewhat lower. The overall effect is the same as in the situation described by Jim, but the losses occur during amplification, not after it.
LikeLike
Jim Randell 7:56 am on 17 June 2023 Permalink |
Here is a faster version. Once the ratio E:I is determined we can look for equivalent fractions with a 2-digit denominator to give values for E and I.
It runs in 77ms. (Internal runtime is 17ms).
from enigma import ( primes, irange, divf, subsets, cproduct, fraction, nsplit, fdiv, unpack, seq2str, format_fraction, printf ) # map denominators to numerators that give fractions < 1/20 nums = lambda n: irange(1, divf(n - 1, 20)) # choose the prime denominators (p, q, r) for (p, q, r) in subsets(primes.between(20, 99), size=3): Rd = p * q * r # and choose the numerators (a, b, c) for (a, b, c) in cproduct(nums(k) for k in (p, q, r)): # calculate the ratio E/I (= Rn / Rd) Rn = (p - a) * (q - b) * (r - c) # consider fractions E/I = Rn/Rd where I is 2-digits (n, d) = fraction(Rn, Rd) for k in irange(1, divf(99, d)): (E, I) = (k * n, k * d) if I < 10: continue # check E and I have the same first digit if nsplit(E)[0] != nsplit(I)[0]: continue # order the fractions fs = sorted([(a, p), (b, q), (c, r)], key=unpack(fdiv)) # output solution printf("{fs} [I={I} E={E}]", fs=seq2str(format_fraction(x, y) for (x, y) in fs))LikeLike
GeoffR 11:15 pm on 16 June 2023 Permalink |
A slow solution – about 8.5 sec.
It finds the three fractions, but not in ascending order.
LikeLike
Jim Randell 11:31 pm on 16 June 2023 Permalink |
You can add the following to get the fractions in order:
LikeLike
GeoffR 11:08 am on 17 June 2023 Permalink |
@Jim:
Yes, that extra line of code works well.
it also reduces the run-time for me to just over 2sec.
LikeLike
Frits 12:31 am on 17 June 2023 Permalink |
from enigma import SubstitutedExpression from fractions import Fraction as rf # invalid digit / symbol assignments d2i = dict() for d in range(0, 10): vs = set() if d == 0: vs.update('ACEPXYZ') if d == 1: vs.update('ACE') if d > 4: vs.update('XYZ') if d % 2 == 0 or d == 5: vs.update('BDF') d2i[d] = vs # remaining checks for prime check = lambda n: all(n % x for x in [3, 7]) # PQ intrinsic gain, PR = effective gain # primes AB, CD and EF # the alphametic puzzle p = SubstitutedExpression( [ "20 * (AB - X) > 19 * AB", # prime check "check(AB)", "CD > AB", # prime check "check(CD)", "20 * (CD - Y) > 19 * CD", "EF > CD", # prime check "check(EF)", # if (AB - X) * (CD - Y) is not divisible by either prime # then (EF - Z) * PQ must be a multiple of AB * CD "any(((AB - X) * (CD - Y)) % x == 0 for x in [AB, CD]) or \ ((AB - X) * (CD - Y)) % EF == 0", "20 * (EF - Z) > 19 * EF", "PQ * (AB - X) * (CD - Y) * (EF - Z) == PR * (AB * CD * EF)" ], answer="ordered(rf(X, AB), rf(Y, CD), rf(Z, EF))", d2i=d2i, env=dict(check=check, rf=rf), distinct="", verbose=0, # use 256 to see the generated code ) # print answers for (_, ans) in p.solve(): print(f"{ans[0]}, {ans[1]} and {ans[2]}")LikeLike
GeoffR 12:31 pm on 17 June 2023 Permalink |
from enigma import is_prime from itertools import permutations PR = [ x for x in range(11, 98) if is_prime(x)] for A, B, C in permutations(range(1, 5), 3): for P in PR: if not(20 * A < P):continue for Q in PR: if Q == P:continue if not (20 * B < Q):continue if not(A * Q < B * P):continue for R in PR: if R in (P, Q):continue if not (20 * C < R):continue if not(B * R < C * Q):continue for E in range(10, 99): for I in range(10, 99): if E // 10 != I // 10:continue # Same equation as the MiniZinc solution if E * P * Q * R == I * (P - A) * (Q - B) * (R - C): print(f"Three fractions: {A}/{P}, {B}/{Q}, {C}/{R}.")LikeLike