开发者

Cryptogram solver in Prolog

开发者 https://www.devze.com 2023-02-18 19:13 出处:网络
I have to solve a cryptogram which looks like this: ABC / DEF = 0.GHGHGH... where length of variables may be different. Also rep开发者_开发百科etition may be different (like 0.XYZXYZ...). I\'ve wri

I have to solve a cryptogram which looks like this:

ABC / DEF = 0.GHGHGH...

where length of variables may be different. Also rep开发者_开发百科etition may be different (like 0.XYZXYZ...). I've written a piece of code which I thought will work but it doesn't:

cryp(A,B,C) :-
  mn(A, X),
  mn(B, Y),
  mn(C, Z),
  Z = X/Y.

mn([], 0).
mn([H|T], W) :- D is 10, mn(T, W1), length(T, D), P is 10^D, W is W1 + H*P.

I execute it as crypt([A,B,C], [D,E,F], [G,H]). I thought it will at least solve ABC / DEF = GH just to have any part of working solution but it doesn't work.

I don't have any clue how to do it even for one example input. I don't know how to represent 0.GHGHGH....

EDIT: mn/2 is for converting list of digits to number ([1,2,3] -> 123).


Here is clp(fd)-way

 ?- use_module(library(clpfd)).
true.

 ?- Vars=[A,B,C,D,E,F,G,H], Vars ins 0..9, A#\=0, D#\=0, ((10*A + B)*10+C)*99 #= ((10*D+E)*10+F)*(10*G+H),all_distinct(Vars),forall(label(Vars),format('~w~n',[Vars])).
[1,0,8,2,9,7,3,6]
[1,0,8,3,9,6,2,7]
[1,3,0,2,8,6,4,5]
[1,3,0,4,9,5,2,6]
[1,3,8,2,9,7,4,6]
[1,3,8,5,0,6,2,7]
[1,6,0,4,9,5,3,2]
[1,8,0,3,9,6,4,5]
[1,8,0,4,9,5,3,6]
[2,0,4,3,9,6,5,1]
[2,5,9,4,0,7,6,3]
[2,8,4,3,9,6,7,1]
[2,8,7,4,5,1,6,3]
[2,8,7,6,9,3,4,1]
[2,9,0,6,3,8,4,5]
[3,1,0,4,9,5,6,2]
[3,1,0,6,8,2,4,5]
[3,6,0,4,9,5,7,2]
[3,6,0,7,9,2,4,5]
[3,8,0,4,9,5,7,6]
[4,0,8,5,6,1,7,2]
[4,0,8,7,9,2,5,1]
[4,9,3,5,6,1,8,7]
[5,0,4,6,9,3,7,2]
[5,0,4,7,9,2,6,3]
[5,1,8,6,9,3,7,4]
[5,7,4,6,9,3,8,2]
[5,7,4,9,0,2,6,3]
[5,9,4,7,2,6,8,1]
[6,8,0,9,3,5,7,2]
[7,5,6,9,2,4,8,1]


You might do the following identifications: let a=0.GHGHGHGH.... Then

a = 0.GHGH....
GH + a = GH.GHGHGHGH.... = 100 a
GH = 99 a

=> a = GH/99

So you can replace it by GH/99 in your formulas.

Similarly 0.XYZXYZXYZ = XYZ/999.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号