Teaser 3061: Ratio
From The Sunday Times, 23rd May 2021 [link] [link]
Liam has split a standard pack of 52 cards into three piles; black cards predominate only in the second pile. In the first pile the ratio of red to black cards is 3 to 1. He transfers a black card from this pile to the second pile; the ratio of black to red cards in the second pile is now 2 to 1. He transfers a red card from the first pile to the third pile; the ratio of red to black cards in this pile is now a whole number to one.
Liam told me how many cards (a prime number) were initially in one of the piles; if I told you which pile you should be able to solve this teaser.
How many cards were initially in the third pile?
[teaser3061]
Jim Randell 5:27 pm on 21 May 2021 Permalink |
I assume that Liam told the setter a specific pile number, and the total number of cards that was initially in that pile and this number of cards was a prime number.
So knowing the number of the pile, and the fact that the total number of cards in that pile was prime (but not knowing the number itself), is sufficient for us to determine the number of cards that were in the third pile.
This Python program runs in 48ms.
Run: [ @replit ]
from enigma import (irange, div, is_prime, printf) # generate possible piles # return (piles = ((red, black), ...), {indices of prime piles}) def piles(): # choose the number of red cards in pile 1 (a multiple of 3) for r1 in irange(3, 26, step=3): # there are 3 times as many reds as blacks b1 = div(r1, 3) # choose the number of black cards in pile 2 (+1 = a multiple of 2) for b2 in irange(3, 26 - b1, step=2): # with 1 extra black there are twice as many blacks as reds r2 = div(b2 + 1, 2) if r1 + r2 > 26: break # pile 3 has the remaining cards r3 = 26 - r1 - r2 b3 = 26 - b1 - b2 if b3 > r3: continue # with 1 extra red there are k times as many reds as blacks k = div(r3 + 1, b3) if k is None: continue printf("[{r1}R+{b1}B; {r2}R+{b2}B; {r3}R+{b3}B; k={k}]") ps = ((r1, b1), (r2, b2), (r3, b3)) pr = set(i for (i, (r, b)) in enumerate(ps) if is_prime(r + b)) yield (ps, pr) # collect piles, that have a prime number of cards in one of the piles ss = list((ps, pr) for (ps, pr) in piles() if pr) # look for unique solutions, identified by pile i being prime for i in (0, 1, 2): rs = list(ps for (ps, pr) in ss if i in pr) if len(rs) == 1: ((r1, b1), (r2, b2), (r3, b3)) = rs[0] printf("{r1}R+{b1}B; {r2}R+{b2}B; {r3}R+{b3}B [pile {i} is prime]", i=i + 1)Solution: There were initially 11 cards in the third pile.
There are only 4 ways to construct the piles as described:
The only collections with a prime number of cards in at least one pile are [1] (pile 3) and [3] (pile 2, pile 3).
So Liam must have revealed there were 29 cards in pile 2, which means he has constructed collection [3] (as that is the only collection with a prime number of cards in pile 2).
Although if Liam had revealed just the prime numbers of cards in the pile (without giving the pile number); 11, 13, or 29, that would have been sufficient to determine which collection he had constructed.
So the second paragraph could be:
LikeLike
Frits 9:02 pm on 22 May 2021 Permalink |
from enigma import SubstitutedExpression, is_prime, div # the alphametic puzzle p = SubstitutedExpression( [ # (r1 , b1), (r2, b2), (r3, b3) = # (3 * C, C), (EF, GH), (IJ, KL) # both pile 1 and pile 3 contain at least 1 black card # black cards predominate only in the second pile "0 < EF < 13", "2 * EF - 1 = GH", # black cards predominate only in the second pile # "3 * C + EF <= C + GH", # "3 * C + EF <= C + 2 * EF - 1", "2 * C <= EF - 1", # EF < 13 --> C < 6 # with 1 extra red there are k times as many reds as blacks "div(27 - 3 * C - EF, 26 - C - GH)", # Liam told me how many cards (a prime number) were initially # in one of the piles "any(is_prime(x) for x in [EF + GH, 52 - 3 * C - EF - C - GH])", ], answer="(3 * C, C), (EF, GH), (26 - 3 * C - EF, 26 - C - GH)", d2i=dict([(0, "C")] + [(k, "E") for k in range(2, 6)] + [(k, "CE") for k in range(6, 10)] ), distinct="", reorder=0, verbose=256) answs = [y for _, y in p.solve()] for p in range(3): ps = [a for a in answs if is_prime(sum(a[p]))] if len(ps) == 1: # unique print(f"third pile has {sum(ps[0][2])} cards")Based on the generated code and some more analysis (we can conclude r2 is even and b1 is less than 5) only 12 (r2, b1) combinations are considered:
from enigma import is_prime answs = [] # r2 must be even as last pile can't have only 2 cards # (so we deal with odd primes only) for r2 in range(4, 13, 2): b2 = 2 * r2 - 1 p2 = is_prime(r2 + b2) for b1 in range(1, min(r2 // 2, 26 - b2)): # 26 - b1 - b2 > 0 --> b1 < 26 - b2 # with 1 extra red there are <k> times as many reds as blacks (d, r) = divmod(27 - 3 * b1 - r2, 26 - b1 - b2) if r > 0: continue # a prime number of cards were initially in one of the piles if p2 or is_prime(52 - 4 * b1 - r2 - b2): answs.append([(3 * b1, b1), (r2, b2), (26 - 3 * b1 - r2, 26 - b1 - b2)]) for p in range(3): ps = [a for a in answs if is_prime(sum(a[p]))] if len(ps) == 1: # unique print(f"third pile has {sum(ps[0][2])} cards")LikeLike
Tony Brooke-Taylor 6:31 pm on 25 May 2021 Permalink |
I wanted to visualise this problem as points on the line where two surfaces intersect. Because of the simple relationships between the red count and black count in each pile, we can easily define planes on the same 3 axes such that one plane represents the black values and the others represent the set of reds corresponding to a given value of N, where N is the ratio of reds to blacks in the third pile, after the swaps. I followed the maths through and arrived at a set of constraints such that I had to test 11 combinations of (b1,b2) to get the 4 possible results. I expect my analysis is much the same as Frits’, but the route depends on which parameters we choose to loop. My code for the solution is not very interesting, but I thought I’d share my graphical representation. The code below is based on an approach set out by banderlog013 on stackoverflow. If you draw the plot for the appropriate value of N, and run your mouse down the line of intersection, you will see that only one point has integer values for x,y,z that sum to 26.
import numpy as np import plotly.graph_objects as go from plotly.subplots import make_subplots from typing import Tuple, Iterable def plotPlane(fig: go.Figure, normal: Tuple[int, int, int], d: int, values: Iterable, colorScaleName: str) -> None: # x, y, z x, y = np.meshgrid(values, values) z = (-normal[0] * x - normal[1] * y - d) * 1. / normal[2] # draw plane surface = go.Surface(x=x, y=y, z=z, colorscale=colorScaleName, showscale=False) fig.add_trace(surface, row=1, col=1) N=1#Not correct but if you have solved the puzzle you will know what is point1 = -26 normal1 = (1,1,1) point2 = -26.5 normal2 = (3,1/2,N) # create figure fig = make_subplots(rows=1, cols=1, specs=[[{'type': 'surface'}]]) # plot two intersectioned surfaces values = range(1, 26) plotPlane(fig, normal1, point1, values, "Hot") plotPlane(fig, normal2, point2, values, "Greys") fig.show()LikeLike
Robert Brown 9:19 am on 22 May 2021 Permalink |
Following the steps in your program, I find a different answer from the one posted by Brian.
My values are
b1 = 1, r1 = 3: (r1 = 3*b1): total not prime
b2 = 23, r2 = 12: (b2+1/r2 = 2): total not prime
b3 = 2, r3 = 11: (r3+1/b3 = 6): total (13) is prime
Am I doing something silly?
LikeLike
Jim Randell 9:49 am on 22 May 2021 Permalink |
@Robert: There are actually four collections of piles that can be constructed following the rules of the first paragraph of the puzzle text.
But the second paragraph allows you to identify which one of those four provides the answer to the puzzle.
LikeLike
Robert Brown 7:53 am on 23 May 2021 Permalink |
Yes, Brian’s solution has prime totals for piles 2 & 3, my alternative just for pile 3. The other two have no prime totals. So if Liam had quoted the total for pile 3, we would have a choice of 2 solutions. It follows that he quoted the total for pile 2, leading to Brian’s solution.
LikeLike
Jim Randell 11:33 am on 23 May 2021 Permalink |
@Robert: That’s correct.
Interestingly if Liam had revealed just the initial total number of cards in one of the piles (without revealing the number of the pile), and that number was prime, it would be enough for the setter to work out the composition of each of the piles.
The setter can then tell us the number of the pile that Liam was referring to, and the fact that the total number of cards in that pile was prime, and this is enough for us to also work out the composition of each of the piles.
LikeLike
Hugh Casement 1:13 pm on 30 May 2021 Permalink |
More troublesome logic.
As far as I can see, the constraints are:
red1 = 3 × black1
black2 + 1 = 2 × red2
(red3 + 1) mod black3 = 0
and the total is 52 (with no variables being 0).
I found well over 100 sets that satisfy those conditions!
Prime numbers abound, so I don’t see how any conclusions can be drawn.
How is it that others found only four possible sets?
LikeLike
Jim Randell 1:21 pm on 30 May 2021 Permalink |
Instead of the total being 52, use:
LikeLike
Hugh Casement 6:14 pm on 30 May 2021 Permalink |
Thanks for that, Jim. You can tell it’s a long time since I had a pack of cards in my hands!
LikeLike