Teaser 3068: Valued playwrights
From The Sunday Times, 11th July 2021 [link] [link]
I have given each letter of the alphabet a different whole-number value from 1 to 26. For example, P=4, L=8, A=3 and Y=24. With my numbers I can work out the value of any word by adding up the values of its letters, for example the word PLAY has a value of 39.
It turns out that the playwrights
BECKETT
FRAYN
PIRANDELLO
RATTIGAN
SHAKESPEARE
SHAWall have the same prime value.
Also COWARD, PINERO and STOPPARD have prime values ….
…what are those three prime numbers?
[teaser3068]
Jim Randell 4:15 pm on 9 July 2021 Permalink |
(See also: Teaser 2884).
Using the [[
SubstitutedExpression]] solver from the enigma.py library.The following run file executes in 355ms.
Run: [ @replit ]
Solution: COWARD = 67; PINERO = 31; STOPPARD = 53.
LikeLike
Frits 9:35 pm on 9 July 2021 Permalink |
@Jim, I don’t see why P has to be 4, etc (it says “for example”).
LikeLike
Jim Randell 9:47 pm on 9 July 2021 Permalink |
@Frits: The “for example” tells us some, but not all, of the assignments.
If it had said: “if, for example, P=4, L=8, A=3 and Y=24, then PLAY would have the value 39″, that would be different.
But without the “example” values, there are many possible answers.
LikeLike
GeoffR 8:07 pm on 9 July 2021 Permalink |
LikeLike
Frits 5:19 pm on 10 July 2021 Permalink |
Jim’s code with some extra analysis.
The following run file executes in approx 15ms.
#!/usr/bin/env python3 -m enigma -r SubstitutedExpression --base=27 --digits="1-26" --assign="P,4" --assign="L,8" --assign="A,3" --assign="Y,24" # W = KESPEARE has 6 distinct characters --> W >= 21 + 2 --invalid="1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22,W" # SHAW >= 47 --> S + H + W >= 44 --> H >= 9 --invalid="1|2|3|4|5|6|7|8,H" # RATTIGAN in {47, 53, 59, 61} --> RTTIGN <= 55 -- > T <= 18 --invalid="19|20|21|22|23|24|25|26,T" # PIRANDELLO in {47, 53, 59, 61} --> IRNDEO <= 38 -- all I,R,N,D,E,O <= 17 --invalid="18|19|20|21|22|23|24|25|26,INDO" # W >= 23 so all letters in KESPEARE have to be smaller than 10, E is 1 or 2 --invalid="10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26,RKS" --invalid="3|4|5|6|7|8|910|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26,E" # equal to SHAW (prime) "W - (K + E + S + P + E + A + E) = R" # SHAKESPEARE "S + H + W - F - R - Y = N" # FRAYN "S + H + W - P - I - R - N - E - L - L - D = O" # PIRANDELLO "S + H + W - R - T - T - I - A - N = G" # RATTIGAN "S + H + A + W - E - C - K - E - T - T = B" # BECKETT # SHAW >= 45 as PIRANDELLO has 9 distinct characters # SHAW <= 63 as S + A <= 12 "S + H + A + W in {47, 53, 59, 61}" # is_prime(S + H + A + W) # COWARD, PINERO, STOPPARD are also prime # PINERO + ADLL = PIRANDELLO = SHAW -- > PINERO = SHAW - ADLL = SHW - DLL "is_prime(S + H + W - D - L - L)" # is_prime(P + I + N + E + R + O) "is_prime(C + O + W + A + R + D)" "is_prime(S + T + O + P + P + A + R + D)" --answer="(C + O + W + A + R + D, P + I + N + E + R + O, S + T + O + P + P + A + R + D)" # [optional] --template="" #--reorder=0LikeLike
GeoffR 9:57 am on 11 July 2021 Permalink |
An excellent run-time.
I do not know if it is possible with SubstitutedExpression, but an irange statement would give shorter code and look much neater than the many consecutive integers in the –invalid statements:
e.g.
–invalid = “irange(1,22), W” and
–invalid = “irange(10,26), RKS” , and other examples.
Is there a typo in line 23 for E ? ….|8|910|11|12| ….
LikeLike
Jim Randell 10:16 am on 11 July 2021 Permalink |
Yes. The [[
--invalid]] parameters can be written more concisely:LikeLike
Frits 11:26 am on 11 July 2021 Permalink |
Some more analysis, leading to D being odd.
My stored version of [ https://s2t2.home.blog/2020/08/13/teaser-2705-in-the-pub/ ] contained:
Later Jim updated it to
2 ranges doesn’t seem to be supported.
The following run file once executed in 8.30ms.
#!/usr/bin/env python3 -m enigma -r SubstitutedExpression --base=27 --digits="1-26" --assign="P,4" --assign="L,8" --assign="A,3" --assign="Y,24" # W = KESPEARE has 6 distinct characters --> W >= 21 + 2 --invalid="1-22,W" # SHAW >= 59 --> SHW >= 56 --> H >= 21 --invalid="1-20,H" # RATTIGAN in {59, 61} --> RTTIGN <= 55 -- > 9 < T <= 18 --invalid="1|2|3|4|5|6|7|8|9|19|20|21|22|23|24|25|26,T" # PIRANDELLO in {59, 61} --> IRNDEO <= 38 -- all I,R,N,D,E,O <= 17 # out of I,N,D,O at most 2 may be single digit (besides ERKSPLA) # out of I,N,D,O at most 2 may be double digit numbers --> exactly 2 --invalid="18-26,INO" # D must be odd (otherwise S + H + W - D - L - L is even and not prime) # if D is 7 or 15 then S + H + W - D - L - L is not prime --invalid="2|4|6|7|8|10|12|14|15|16|18|19|20|21|22|23|24|25|26,D" # W >= 23 so all letters in KESPEARE have to be smaller than 10, E is 1 or 2 --invalid="10-26,RKS" --invalid="3-26,E" # all single digit numbers are already in use (ERKSPLA and 2 out of INDO) --invalid="1-9,BCFG" # equal to SHAW (prime) "W - (K + E + S + P + E + A + E) = R" # SHAKESPEARE "S + H + W - R - Y - N = F" # FRAYN "S + H + W - P - I - R - N - E - L - L - D = O" # PIRANDELLO "S + H + W - R - T - T - I - A - N = G" # RATTIGAN "S + H + A + W - E - C - K - E - T - T = B" # BECKETT # SHAW >= 45 + 8 as PIRANDELO has 9 distinct characters # SHAW > 45 + 8 as PIRANDELO can't use only digits 1-9 (as S < 10) # SHAW <= 63 as S + A <= 12 "S + H + A + W in {59, 61}" # is_prime(S + H + A + W) # COWARD, PINERO, STOPPARD are also prime # PINERO + ADLL = PIRANDELLO = SHAW --> PINERO = SHAW - ADLL = SHW - DLL "S + H + W - D - L - L in {23, 29, 31, 37, 41}" # is_prime(PINERO) "is_prime(C + O + W + A + R + D)" "is_prime(S + T + O + P + P + A + R + D)" --answer="(C + O + W + A + R + D, P + I + N + E + R + O, S + T + O + P + P + A + R + D)" # [optional] --template="" #--reorder=0 #--verbose=256 # print generated codeLikeLike
Iain Turner 11:41 am on 21 July 2021 Permalink |
I wrote a python solution just using itertools rather than SubstitutedExpression:
import itertools def isprime(x): #quick & dirty prime test if x%2==0: return(False) for i in range(3,1+int(x**0.5),2): if x%i==0:return(False) return(True) e=1 p=4 a=3 l=8 y=24 count=0 alpha26={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26} for w in [23,25,26]: rem=alpha26-{e,w,p,a,l,y} #shakespeare = shaw so w = kspar +3e #if e=1, min value of kspar is 2+3+4+5+6 = 20, so min value of w is 23 #if e=2, min value of kspar is 19, so min w is 25 for ksr in itertools.permutations(rem,3): k,s,r=ksr if w == k+e+s+p+e+a+r+e: rem2=rem-set(ksr) for prime in [51,53,59,61,67]: #shaw h=prime-a-w-s if h in rem2: rem3=rem2-{h} #pirandello #min value of pirandello is 1+2+2+3+4+5+6+7+8+9 = 47 #max value of shaw is 26+25+8+9 = 68 #so prime range is 51 to 67 (see above) for indo in itertools.permutations(rem3,4): xx=prime-p-e-a-r-l-l if sum(indo)==xx: i,n,d,o=indo pinero=p+i+n+e+r+o if isprime(pinero): rem4=rem3-set(indo) #frayn for f in rem4: if f+r+a+y+n==prime: rem5=rem4-{f} #beckett for bct in itertools.permutations(rem5,3): xx=prime-e-e-k if sum(bct)+bct[2]==xx: b,c,t=bct rem6=rem5-set(bct) stoppard=s+t+o+p+p+a+r+d coward=c+o+w+a+r+d if isprime(stoppard) and isprime(coward): g=prime-r-a-t-t-i-a-n #rattigan if g in rem6: print(stoppard,coward,pinero,'prime:',prime,'remaining:',rem6-{g}) count+=1 print('total count',count)LikeLike
Jim Randell 4:49 pm on 21 July 2021 Permalink |
Thanks for your comment.
I wrapped your code in
[code language="python"]...[/code]tags and changed the indents to 2 spaces (so that it doesn’t disappear off the RHS).The [[
SubstitutedExpression]] solver uses the expressions given to it to write a similar program, and then runs it.If you want to know more about how it works see “Solving Alphametics with Python, Part 2” [ link ].
LikeLike
Frits 6:03 pm on 21 July 2021 Permalink |
Hi Iain,
Why do you set e to 1?
51 is not a prime number so it might lead to incorrect answers.
Min value of pirandello can be set to 53 (2 l’s with value 8).
Max value of shaw can be set to 26+25+3+9 = 63 (as a is 3 and s less than 10).
LikeLike