我必须解决一个如下所示的密码:
ABC / DEF = 0.GHGHGH...
其中变量的长度可能不同。重复也可能不同(如0.XYZXYZ ......)。我写了一段代码,我认为它会起作用,但它没有:
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.
我执行它作为crypt([A,B,C],[D,E,F],[G,H])。我认为至少解决ABC / DEF = GH只是为了获得工作解决方案的任何部分,但它不起作用。
即使是一个示例输入,我也没有任何线索。我不知道如何代表0.GHGHGH ......
编辑: mn / 2用于将数字列表转换为数字([1,2,3] - > 123)。
答案 0 :(得分:3)
这是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]
答案 1 :(得分:1)
您可以进行以下识别:让a=0.GHGHGHGH...
。然后
a = 0.GHGH....
GH + a = GH.GHGHGHGH.... = 100 a
GH = 99 a
=> a = GH/99
因此,您可以在公式中用GH/99
替换它。
同样0.XYZXYZXYZ = XYZ/999
。