math - Cryptarithmetic Multiplication Prolog -
i have grasp of idea of crypt arithmetic , addition cannot figure out how multiplication crypt arithmetic problem. it's two*six=twelve or along lines without middle additional part of multiplication problem given. couldn't find online , found constraints problem nothing leads me answers. not sure ask , thought best place.
i want know how solve multiplication crypt arithmetic problem.
i concluded:
t w o * s x _________________ t w e l v e t \= 0 means s \= 0 t 1-6 e (o*x) mod 10 o or x cannot 0 or 1 since e has different , 0 or 1 gives same value either o or x.
edit: using generate , 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).
trivially constraint logic programming. 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 = 7 x = 2 e = 0 l = 3 v = 8 yes (0.01s cpu, solution 1, maybe more) ?
there 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 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).
Comments
Post a Comment