Teaser 2517: [Christmas alphametic]
From The Sunday Times, 19th December 2010 [link] [link]
Finley is excited about Christmas. At school, he has made a card showing a triangular-shaped Christmas tree with a fairy on top. With this in mind, please decipher:
XMAS + TREE = FAIRY
where different letters stand consistently for different digits, and where TREE is a triangular number (i.e., is the sum of some consecutive integers from 1 upwards).
What number is XMAS?
This puzzle was originally published with no title.
[teaser2517]
Jim Randell 8:26 am on 5 December 2025 Permalink |
Here is a solution using the [[
SubstitutedExpression]] solver from the enigma.py library.It runs in 78ms. (The internal runtime of the generated code is 1.2ms).
Solution: XMAS = 7209.
The sum is: 7209 + 3655 = 10864.
And: TREE = 3655 = tri(85) = sum(1..85).
LikeLike
Ruud 2:14 pm on 5 December 2025 Permalink |
To run this code, istr 1.1.12 is required.
import peek import istr for tree in istr.range(1000, 10000): if tree.is_triangular(): t, r, e, e0 = tree if e == e0 and tree[:3].all_distinct(): digits_without_tree = set(istr.digits()) - {t, r, e} for x, m, a, s, f, i, y in istr.permutations(digits_without_tree): if (xmas := x | m | a | s) + tree == (fairy := f | a | i | r | y): peek(xmas, tree, fairy)More compact, but much less efficient:
import peek import istr for x, m, a, s, t, r, e, f, i, y in istr.permutations(istr.digits()): if (tree := t | r | e | e).is_triangular() and (xmas := x | m | a | s) + tree == (fairy := f | a | i | r | y): peek(xmas, tree, fairy)LikeLike
CB Root 6:19 pm on 14 December 2025 Permalink |
Here is some (rather amateurish) Javascript code for your delectation:
function myFunction() { var carry; var s,e,y; var SE; var a,r; var AE; var m,i; var x,t; var XT; var f; var XMAS,TREE,FAIRY; for(s=0;s<=9;s++){ for(e=0;e<=9;e++){ SE = s+e; y=SE%10; if(y>=0 && y<=9){ carry=Math.floor(SE/10); for(a=0;a<=9;a++){ AE=carry+a+e; r=AE%10; if(r>=0 && r<=9){ carry=Math.floor(AE/10); for(m=0;m<=9;m++){ i = carry + m + r; carry=Math.floor(i/10); if(i>=0 && i<=9){ for(x=1;x<=9;x++){ for(t=0;t<=9;t++){ XT = carry + x + t; if(XT%10==a){ for(f=1;f<=9;f++){ if(allUnique([s,e,y,a,r,m,i,x,t,f])){ XMAS = parseInt(x.toString()+m.toString()+a.toString()+s.toString()); TREE = parseInt(t.toString()+r.toString()+e.toString()+e.toString()); FAIRY = parseInt(f.toString()+a.toString()+i.toString()+r.toString()+y.toString()); if(isTriangular(TREE)){ if(FAIRY==(XMAS+TREE)){ Logger.log(' xmas= '+x.toString()+m.toString()+a.toString()+s.toString()); Logger.log(' tree= '+t.toString()+r.toString()+e.toString()+e.toString()); Logger.log('fairy= '+f.toString()+a.toString()+i.toString()+r.toString()+y.toString()); } } } } } } } } } } } } } } } function allUnique(arrVal){ var al = arrVal.length-1; var pp,qq; for(pp=0;pp<=al;pp++){ for(qq=0;qq<=al;qq++){ if(pp!=qq){ if(arrVal[pp]==arrVal[qq]){ return false; } } } } return true; } function isTriangular(testNo){ var product = testNo * 8; product = product + 1; if(Math.sqrt(product)==Math.floor(Math.sqrt(product))){ return true; } else{ return false; } }LikeLike
Jim Randell 8:59 am on 15 December 2025 Permalink |
Hi, thanks for leaving a comment.
Unfortunately I’m not sure your code made it through the WordPress comments system unscathed. (Unprotected code often loses sections between
<and>before I see it and cannot be fixed).If you leave a comment with your code between:
I will fix it up in the original comment.
LikeLike
CB Root 11:46 am on 15 December 2025 Permalink |
OK, sorry about that. Here follows my (slightly updated ) code bracketed as requested.
[see code above]
LikeLike
GeoffR 3:08 pm on 15 December 2025 Permalink |
LikeLike