Teaser 2156: Some cubes
From The Sunday Times, 11th January 2004 [link]
In what follows, digits have consistently been replaced by letters, with different letters used for different digits:
THREE
CAN
BE
CUBESAs stated, three of those numbers are cubes (and the other one is less than five away from a cube).
What is the value of CUBE?
[teaser2156]
Jim Randell 9:32 am on 20 June 2023 Permalink |
This Python program uses the [[
SubstitutedExpression()]] solver from the enigma.py library to find candidate values that are each individually within 5 of a cube number. We then check the answers for cases where three of the distances are 0.It runs in 68ms. (Internal runtime is 11ms).
Run: [ @replit ]
from enigma import (SubstitutedExpression, iroot, printf) # distance to nearest kth root def dist(n, k=3): x = iroot(n, k) return min(n - x**k, (x + 1)**k - n) # make an alphametic puzzle p = SubstitutedExpression( [ # each must be less than 5 away from a cube "dist(THREE) < 5", "dist(CAN) < 5", "dist(BE) < 5", "dist(CUBES) < 5", ], answer="(THREE, CAN, BE, CUBES)", env=dict(dist=dist), verbose=0 ) # solve the puzzle for ans in p.answers(): # count the distances ds = list(dist(x) for x in ans) # three must have distance 0 if ds.count(0) != 3: continue # output solution CUBE = ans[-1] // 10 printf("CUBE = {CUBE} {ans}")Solution: CUBE = 1968.
The numbers are:
LikeLike
Frits 1:57 pm on 20 June 2023 Permalink |
More messy (the reorder=0 isn’t even necessary).
LikeLike
Frits 7:02 pm on 20 June 2023 Permalink |
The 2 missing ” and ” don’t cause a syntax error.
LikeLike
Jim Randell 8:26 am on 21 June 2023 Permalink |
@Frits: I think I see what this is doing. (I removed the extraneous trailing commas, and fixed up some missing
andkeywords).But I think it would allow the case where all four of the words were cubes.
This is what my [[
SubstitutedExpression]] run file looks like:Run: [ @replit ]
#! python3 -m enigma -rr SubstitutedExpression # distance to nearest kth root --code="def _dist(n, k=3): x = iroot(n, k); return min(n - x**k, (x + 1)**k - n)" --code="dist = cache(_dist)" # check cube distance is less than 5 (if flag) or 0 --code="def check(n, f): x = dist(n); return (0 < x < 5 if f else x == 0)" # n = 1..4 denotes which statement is not a cube --invalid="0|5|6|7|8|9,n" # each is less than 5 away from a cube "check({THREE}, {n} == 1)" "check({CAN}, {n} == 2)" "check({BE}, {n} == 3)" "check({CUBES}, {n} == 4)" --distinct="ABCEHNRSTU" --answer="CUBE" --template="(THREE, CAN, BE, CUBES)" --solution="n"LikeLike
GeoffR 5:43 pm on 20 June 2023 Permalink |
Using Frits method of solution i.e.each number must be a cube or less than 5 away from a cube.
% A Solution in MiniZinc include "globals.mzn"; var 1..9:T; var 0..9:H; var 0..9:R; var 0..9:E; var 1..9:C; var 0..9:A; var 0..9:N; var 1..9:B; var 0..9:U; var 0..9:S; constraint all_different ([T, H, R, E, C, A, N, B, U, S]); % Four numbers are BE, CAN, CUBES, THREE var 10..99:BE == 10*B + E; var 100..999:CAN == 100*C + 10*A + N; var 10000..99999:CUBES == 10000*C + 1000*U + 100*B + 10*E + S; var 10000..99999:THREE == 10000*T + 1000*H + 100*R + 11*E; % Required answer is CUBE var 1000..9999:CUBE == 1000*C + 100*U + 10*B + E; % Sets of cubes for 2-digit, 3-digit and 5-digit numbers set of int: cb2 = {27, 64}; set of int: cb3 = {n * n * n | n in 5..9}; set of int: cb5 = {n * n * n | n in 22..46}; % Three of the numbers are cubes constraint sum([THREE in cb5, CAN in cb3, BE in cb2, CUBES in cb5]) == 3; % Allow for the fourth number to be less than five away from a cube % Number BE constraint BE in cb2 \/ (BE - 1) in cb2 \/ (BE - 2) in cb2 \/ (BE - 3) in cb2 \/ (BE - 4) in cb2 \/ (BE + 1) in cb2 \/ (BE + 2) in cb2 \/ (BE + 3) in cb2 \/ (BE + 4) in cb2; % Number CAN constraint CAN in cb3 \/ (CAN - 1) in cb3 \/ (CAN - 2) in cb3 \/ (CAN - 3) in cb3 \/ (CAN - 4) in cb3 \/ (CAN + 1) in cb3 \/ (CAN + 2) in cb3 \/ (CAN + 3) in cb3 \/ (CAN + 4) in cb3; % Number THREE constraint THREE in cb5 \/ (THREE - 1) in cb5 \/ (THREE - 2) in cb5 \/ (THREE - 3) in cb5 \/ (THREE - 4) in cb5 \/ (THREE + 1) in cb5 \/ (THREE + 2) in cb5 \/ (THREE + 3) in cb5 \/ (THREE + 4) in cb5; % Number CUBES constraint CUBES in cb5 \/ (CUBES - 1) in cb5 \/ (CUBES - 2) in cb5 \/ (CUBES - 3) in cb5 \/ (CUBES - 4) in cb5 \/ (CUBES + 1) in cb5 \/ (CUBES + 2) in cb5 \/ (CUBES + 3) in cb5 \/ (CUBES + 4) in cb5; solve satisfy; output["CUBE = " ++ show(CUBE) ++ "\n" ++ "[THREE, CAN, BE, CUBES] = " ++ show ([THREE, CAN, BE, CUBES]) ]; % CUBE = 1968 % [THREE, CAN, BE, CUBES] = [74088, 125, 68, 19683] % ---------- % ==========LikeLike