发信人: daniel (小丹尼), 信区: AI
标 题: SP Puzzle (4)
发信站: 南大小百合信息交换站 (Mon Apr 27 20:44:58 1998), 转信
Turbo Prolog程序:
Predicates
puzzle(integer,integer,integer,integer)
boundd(integer,integer,integer)
unique(integer,integer,integer,integer)
find(integer,integer,integer)
cond1(integer,integer,integer,integer)
cond2(integer,integer,integer,integer)
cond3(integer,integer,integer,integer)
Goal
puzzle(2,99,X,Y),
write("The result is:"),nl,
write("X=",X),nl,
write("Y=",Y).
Clauses
puzzle(Lb,Hb,X,Y):- Hx=Hb/2, boundd(Lb,Hx,X),
boundd(X,Hb,Y), cond1(Lb,Hb,X,Y),
cond2(Lb,Hb,X,Y), cond3(Lb,Hb,X,Y).
boundd(L,H,_):- L>=H,!,fail.
boundd(L,_,L).
boundd(L,H,X):- L1=L+1, boundd(L1,H,X).
cond1(Lb,Hb,X,Y):- Z=X+Y, H=Z/2+1,
boundd(Lb,H,M1), N1=Z-M1,
unique(Lb,Hb,M1,N1),!,fail.
cond1(_,_,_,_).
unique(Lb,_,M,N):- P=M*N, find(Lb,P,U), not(U=M),
not(U=N), P mod U=0,!,fail.
unique(_,_,_,_).
find(Lb,Z1,_):- Q=Z1/Lb, Q<=Lb,!,fail.
find(Lb,_,Lb).
find(Lb,Z1,M2):- Lb1=Lb+1, find(Lb1,Z1,M2).
cond2(Lb,Hb,X,Y):- Z=X*Y,find(Lb,Z,M2), not(M2=X),
not(M2=Y), Z mod M2=0, N2=Z/M2,
N2<=Hb, cond1(Lb,Hb,M2,N2),!,fail.
cond2(_,_,_,_).
cond3(Lb,Hb,X,Y):- Z=X+Y, H=Z/2+1, boundd(Lb,H,M1),
not(M1=X), not(M1=Y), N1=Z-M1,
cond2(Lb,Hb,M1,N1),!,fail.
cond3(_,_,_,_).
--
*****************************************************************
归妹趋无妄,无妄趋同人,同人趋大有。甲转丙,丙转庚,庚转癸。
子丑之交,辰巳之交,午未之交。风雷是一变,山泽是一变,水火是一变。
乾坤相激,震兑相激,离巽相激。三增而成五,五增而成九。
*****************************************************************