A list of the digits 1–9 and all its permutations
In[]:=
digits=Range[9]
Out[]=
{1,2,3,4,5,6,7,8,9}
In[]:=
perms=Permutations[digits];
In[]:=
Length[perms]
Out[]=
362880
In[]:=
Length[perms]==9!
Out[]=
True
The problem’s equation expressed as a Boolean function
In[]:=
f[n_]:=n[[1]]+13n[[2]]/n[[3]]+n[[4]]+12n[[5]]-n[[6]]-11+n[[7]]n[[8]]/n[[9]]-10==66
Select all the permutations for which the equation is solved
In[]:=
solns=Select[perms,f];
In[]:=
Length[solns]
Out[]=
136
Eliminate the solutions that are identical when (1, 4) or (7, 8) are switched
In[]:=
g[n_]:=n[[1]]<n[[4]]&&n[[7]]<n[[8]]
In[]:=
basesolns=Select[solns,g]
Out[]=
{{1,2,6,4,7,8,3,5,9},{1,3,2,4,5,8,7,9,6},{1,3,2,9,5,6,4,7,8},{1,3,4,7,6,5,2,9,8},{1,3,6,2,7,9,4,5,8},{1,3,9,4,7,8,2,5,6},{1,4,8,2,7,9,3,5,6},{1,5,2,3,4,8,7,9,6},{1,5,2,8,4,7,3,9,6},{1,5,3,9,4,2,7,8,6},{1,8,3,7,4,5,2,6,9},{1,9,6,4,5,8,3,7,2},{1,9,6,7,5,2,3,4,8},{2,1,4,3,7,9,5,6,8},{2,6,9,8,5,1,4,7,3},{2,8,6,9,4,1,5,7,3},{2,9,6,3,5,1,4,7,8},{3,2,1,5,4,7,8,9,6},{3,2,4,8,5,1,7,9,6},{3,2,8,6,5,1,7,9,4},{3,6,4,9,5,8,1,7,2},{3,9,2,8,1,5,6,7,4},{5,1,2,9,6,7,3,4,8},{5,3,1,7,2,6,8,9,4},{5,4,1,9,2,7,3,8,6},{5,4,8,9,6,7,1,3,2},{5,7,2,8,3,9,1,6,4},{5,9,3,6,2,1,7,8,4},{6,3,1,9,2,5,7,8,4},{7,1,4,9,6,5,2,3,8},{7,2,8,9,6,5,1,3,4},{7,3,2,8,5,9,1,6,4},{7,5,2,8,4,9,1,3,6},{7,6,4,8,5,9,1,3,2}}
In[]:=
Length[basesolns]
Out[]=
34
Are both fraction terms integers?
In[]:=
h[n_]:=IntegerQ[n[[2]]/n[[3]]]&&IntegerQ[n[[7]]n[[8]]/n[[9]]]
In[]:=
allints=Select[solns,h]
Out[]=
{{3,2,1,5,4,7,8,9,6},{3,2,1,5,4,7,9,8,6},{5,2,1,3,4,7,8,9,6},{5,2,1,3,4,7,9,8,6},{5,3,1,7,2,6,8,9,4},{5,3,1,7,2,6,9,8,4},{5,4,1,9,2,7,3,8,6},{5,4,1,9,2,7,8,3,6},{5,9,3,6,2,1,7,8,4},{5,9,3,6,2,1,8,7,4},{6,3,1,9,2,5,7,8,4},{6,3,1,9,2,5,8,7,4},{6,9,3,5,2,1,7,8,4},{6,9,3,5,2,1,8,7,4},{7,3,1,5,2,6,8,9,4},{7,3,1,5,2,6,9,8,4},{9,3,1,6,2,5,7,8,4},{9,3,1,6,2,5,8,7,4},{9,4,1,5,2,7,3,8,6},{9,4,1,5,2,7,8,3,6}}
In[]:=
Length[allints]
Out[]=
20
In[]:=
Select[allints,g]
Out[]=
{{3,2,1,5,4,7,8,9,6},{5,3,1,7,2,6,8,9,4},{5,4,1,9,2,7,3,8,6},{5,9,3,6,2,1,7,8,4},{6,3,1,9,2,5,7,8,4}}