Problem:
I want to find all unique expressions with n
number of terms that contain all the digits (or characters) in alphabet
.
Example: Let the variable alphabet
be a given a List
of digits or characters (for now let us use base 10 or the output of Range[0, 9]
).
So I need all equations with non repeating digits, as follows:
Note that each one of the multiplications above contains all the digits without repetition from 0 to 9.
For example: 402 * 39 == 15678
contains {4,0,2,3,9,1,5,6,7,8}
- all unique elements of alphabet {0,1,2,3,4,5,6,7,8,9}
.
Solution: Draft
alphabet = ( ToString /@ Range[0, 9] );
Off[Part::partw];
(
uniqueTerms[ alphabet_List: (ToString /@ Range[0, 9] ),
size_Integer: Part[Divisors[alphabet_], 2],
selectionCriteria_: _] :=
(* Select returns a list of elements that match a given pattern. *)
Select[
DeleteDuplicates /@ Permutations[alphabet, {size}]
, selectionCriteria]
) ;
( twos = uniqueTerms[ alphabet, 2, ! SameQ[#[[1]], "0"] & ] );
( threes = uniqueTerms[alphabet, 3, ! SameQ[#[[1]], "0"] & ] ) ;
( expressions = Select[ Flatten[ Table[
{ threes[[ i ]], twos[[ j ]] ,
DeleteDuplicates[
Flatten[ { threes[[ i ]], twos[[ j ]] } ] ] },
{ i, Range[Length@threes] },
{ j, Range[ Length@twos ] }
], 1] , Length[ #[[ 3 ]] ] == 5 & ] [[
All, { 1, 2 } ]] ) ;
( eqns = (
expressions //. {a_List,
b_List} :> {t1 = FromDigits[ToExpression /@ a],
t2 = FromDigits[ToExpression /@ b], t1*t2} ) ) ;
sol = SortBy[
Select[eqns, (
IntegerDigits[ #[[1]] ] \[Union]
IntegerDigits[ #[[2]] ] \[Union] IntegerDigits[ #[[3]] ] ) ==
Range[0, 9] & ], Last] /. {a_, b_, c_} :>
TraditionalForm[a\[Cross]b == c]
This was my solution. I would like to use an alternative to Permutations
and Select
in cases where alphabet
is big enough (maybe using Signature
and PartitionQ
or Combinatorica`TransitiveReduction
).
Any suggestion to generalize or improve my approach is welcomed. Links to similar problems, posts, books and articles are also helpful.
! SameQ[#[[1]], "0"]
inuniqueTerms[alphabet, 3, ! SameQ[#[[1]], "0"]
. $\endgroup$uniqueTerms[ alphabet_List: (ToString /@ Range[0, 9] ), size_Integer: Part[Divisors[alphabet_], 2], selectionCriteria_: _]
is called as the defalt value of the second argument. It does not affect functionality. $\endgroup$Select[Permutations[Range[0, 9]], FromDigits[Take[#, 3]]* FromDigits[Take[#, {4, 5}]] == FromDigits[Take[#, -5]]&]
and it is interesting but16
seconds is quiet big. The approach I proposed took1
second. $\endgroup$01 * 345 == 1 * 345 -> {1, 3,4,5}
because that will give us an answer with less than 5 digits and therefore the expression will not contain all the characters in the alphabet. Remember402 * 39 == 15678
isTrue
and{4,0,2,3,9,1,5,6,7,8} == {0,1,2,3,4,5,6,7,8,9}
is alsoTrue
. $\endgroup$