3

I have the grasp of the idea of crypt arithmetic and addition but I cannot figure out how to do a multiplication crypt arithmetic problem. It's simply TWO*SIX=TWELVE or something along those lines without the middle additional part of the multiplication problem given. I couldn't find anything online and I already found some constraints for the problem but nothing to leads me to some answers. Not sure where to ask this and thought this would be the best place.

I want to know how to solve a multiplication crypt arithmetic problem.

I already concluded:

         T  W  O
*        S  I  X
_________________
T  W  E  L  V  E


T \= 0 which also means S \= 0
T is 1-6
E is (O*X) mod 10
O or X cannot be 0 or 1 since E has to be different and 0 or 1 gives the same value 
as either O or X.

EDIT: I was using the generate and test method

solve(T,W,O,S,I,X,E,L,V) :-
        X = [T,W,O,S,I,X,E,L,V],
        Digits = [0,1,2,3,4,5,6,7,8,9],
        assign_digits(X, Digits),
        T > 0, 
        S > 0,
        100*T + 10*W + O * 100*S + 10*I + X =:=
        100000*T + 10000*W + 1000*E + 100*L + 10*V + E,
        write(X).

select(X, [X|R], R).
select(X, [Y|Xs], [Y|Ys]):- select(X, Xs, Ys).

assign_digits([], _List).
assign_digits([D|Ds], List):-
        select(D, List, NewList),
        assign_digits(Ds, NewList).
Sergii Dymchenko
  • 6,890
  • 1
  • 21
  • 46
user2318083
  • 567
  • 1
  • 8
  • 27
  • 1
    State clearly what is your question. Also check if this is not something you're looking for: http://stackoverflow.com/questions/23575795/cryptarithmetic-prolog-test-fails-recursion-idea – Grzegorz Adam Kowalski May 16 '14 at 14:11
  • I put it up there, but I just want to know how to solve the problem on paper at least. I don't know how to do that and an addition problem is easy. That link you posted is an addition problem and I understand those. – user2318083 May 16 '14 at 14:26

2 Answers2

6

Trivially to do with constraint logic programming. For example, in ECLiPSe Prolog:

:- lib(ic).
puzzle(Vars) :-
    [T,W,O,S,I,X,E,L,V] = Vars,
    Vars :: 0..9,
    alldifferent(Vars),
    T #> 0, S #> 0,
    (100*T + 10*W + O) * (100*S + 10*I + X) #= 
      100000*T + 10000*W + 1000*E + 100*L + 10*V + E,
    labeling(Vars).

First solution:

[eclipse]: puzzle([T,W,O,S,I,X,E,L,V]).
T = 1
W = 6
O = 5
S = 9
I = 7
X = 2
E = 0
L = 3
V = 8
Yes (0.01s cpu, solution 1, maybe more) ? 

There are 3 different solutions:

[eclipse]: puzzle([T,W,O,S,I,X,E,L,V]), writeln([T,W,O,S,I,X,E,L,V]), fail.
[1, 6, 5, 9, 7, 2, 0, 3, 8]
[2, 1, 8, 9, 6, 5, 0, 3, 7]
[3, 4, 5, 9, 8, 6, 0, 1, 7]
No (0.02s cpu)

Update - translation to SWI Prolog:

:- use_module(library(clpfd)).
puzzle(Vars) :-
    [T,W,O,S,I,X,E,L,V] = Vars,
    Vars ins 0..9,
    all_different(Vars),
    T #> 0, S #> 0,
    (100*T + 10*W + O) * (100*S + 10*I + X) #= 
      100000*T + 10000*W + 1000*E + 100*L + 10*V + E,
    label(Vars).
Sergii Dymchenko
  • 6,890
  • 1
  • 21
  • 46
  • Not too familiar with eclipse prolog and I'm doing it in swi prolog. Can you explain what is `labeling(Vars).`, `all different(Vars).` and `lib(ic)`? – user2318083 May 16 '14 at 19:30
  • 2
    @user2318083 I've added translation to SWI-Prolog. See my answer updated. Please see clpfd documentation http://www.swi-prolog.org/pldoc/man?section=clpfd for more info. – Sergii Dymchenko May 16 '14 at 19:51
  • Thanks that really helped. I was approaching it a different way with a generate and test I edited it with my code but it always returns false. I'm not sure why, would it be possible if you can look over my code there? – user2318083 May 16 '14 at 20:45
  • 1
    @user2318083 your code has two problems that are easy to fix. First problem, in `X = [T,W,O,S,I,X,E,L,V],` you chose name `X` for the list of all variables, but you already have a variable with name `X`! The second problem is the lack of parenthesis in `100*T + 10*W + O * 100*S + 10*I + X`. After fixing these bugs your code works OK. – Sergii Dymchenko May 16 '14 at 22:03
3

More general and no-CLP solution:

number_to_digits(Number,List) :-
        length(List,Len),
        ntb(0,Len,Number,List).

ntb(N,_,N,[]).
ntb(C,E,N,[D|L]) :-
        NE is E-1,
        V is C + D*10^NE,
        ntb(V,NE,N,L).

crypto(In1, In2, Out) :-
    term_variables([In1, In2, Out], Vars),
    permutation([0,1,2,3,4,5,6,7,8,9], Perm),
    append(_, Vars, Perm),
        number_to_digits(N1, In1),
        number_to_digits(N2, In2),
        number_to_digits(N3, Out),
        N3 is N1 * N2.

It is quite inefficient and definetly this problem should be solved using CLP like @Sergey did, but maybe someone will be interested in possible solutions without CLP.

Input and output:

?- crypto([T,W,O], [S,I,X], [T,W,E,L,V,E]).
T = 0,
W = 5,
O = 7,
S = 9,
I = 6,
X = 2,
E = 4,
L = 8,
V = 3;
(...)

(57 * 962 = 54834).

Grzegorz Adam Kowalski
  • 5,243
  • 3
  • 29
  • 40