Predicates
factorial(integer, real)
%(i,o),(i,i)
factorial(integer,integer,real,real)
%(i,i,i,0),(i,i,i,i)
%GOAL
%factorial(3,X),
%write(X).
Clauses
%%%%% 1:
/*factorial(N,Rez):-
N=0,Rez=1,!;
N1=N-1,factorial(N1,Rez1),Rez=N*Rez1.
*/
%%%%% 2:
%factorial(0,1):-!.
%factorial(N,Rez):-N1=N-1,factorial(N1,Rez1),Rez=Rez1*N.
%%%%% 3:
/*factorial(N,Rez):-factorial(N,1,1,Rez).
factorial(N,Ctr,Ri,Rez):-
Ctr<=N-1,!,Ctr1=Ctr+1,Ri1=Ri*Ctr1,factorial(N,Ctr1,Ri1,Rez);
%factorial(_,_,Ri,Ri).
factorial(N,N,Ri,Ri).
*/
%%%%% 4:
/*factorial(N,Rez):-factorial(N,1,1,Rez).
factorial(N,N,ReI,ReI).
factorial(N,Cont,ReI,Rez):-write("_ Cont:=",Cont," \n"),
Cont2=Cont+1,write("|_ Cont2:=",Cont2,"\n"),
Cont2<=N,write("|_ ",Cont2,"<=",N,"::TRUE\n"),
ReI2=ReI*Cont2,
factorial(N,Cont2,ReI2,Rez).
*/
%%%%% 5:
/*factorial(N,Rez):-factorial(N,1,1,Rez).
factorial(N,N,ReI,ReI):-!.
factorial(N,Cont,ReI,Rez):-write("_ Cont:=",Cont," \n"),
Cont2=Cont+1,write("|_ Cont2:=",Cont2,"\n"),
Cont2<=N,write("|_ ",Cont2,"<=",N,"::TRUE\n"),
ReI2=ReI*Cont2,
factorial(N,Cont2,ReI2,Rez).
*/
%%%%% 6:
/*factorial(N,Rez):-factorial(N,1,1,Rez).
factorial(N,N,ReI,ReI):-!.
factorial(N,Cont,ReI,Rez):-write("_ Cont:=",Cont," \n"),
Cont2=Cont+1,write("|_ Cont2:=",Cont2,"\n"),
write("|_ ",Cont2,"<=",N,"::TRUE\n"),
ReI2=ReI*Cont2,
factorial(N,Cont2,ReI2,Rez).
*/
%%%%% 7:
/*factorial(N,Rez):-factorial(N,1,1,Rez).
factorial(N,N,ReI,ReI).
factorial(N,Cont,ReI,Rez):-write("_ Cont:=",Cont," \n"),
Cont2=Cont+1,write("|_ Cont2:=",Cont2,"\n"),
write("|_ ",Cont2,"<=",N,"::TRUE?\n"),Cont2<=N,
ReI2=ReI*Cont2,
factorial(N,Cont2,ReI2,Rez).
*/
%%%%% 8:
/*factorial(N,Rez):-factorial(N,1,1,Rez).
factorial(N,N,ReI,ReI):-write("apelat fapt1\n").
factorial(N,N,ReI,ReI):-write("apelat fapt2\n").
factorial(N,Cont,ReI,Rez):-write("_ Cont:=",Cont," \n"),
Cont2=Cont+1,write("|_ Cont2:=",Cont2,"\n"),
write("|_ ",Cont2,"<=",N,"::TRUE?\n"),Cont2<=N,
ReI2=ReI*Cont2,
factorial(N,Cont2,ReI2,Rez).
*/
%%%%% 8.1:
/*factorial(N,Rez):-factorial(N,1,1,Rez).
factorial(N,N,ReI,ReI):-!,write("apelat fapt1\n").
factorial(N,N,ReI,ReI):-write("apelat fapt2\n").
factorial(N,Cont,ReI,Rez):-write("_ Cont:=",Cont," \n"),
Cont2=Cont+1,write("|_ Cont2:=",Cont2,"\n"),
write("|_ ",Cont2,"<=",N,"::TRUE?\n"),Cont2<=N,
ReI2=ReI*Cont2,
factorial(N,Cont2,ReI2,Rez).
*/
%%%%% 8.2:
/*factorial(N,Rez):-factorial(N,1,1,Rez).
factorial(N,N,ReI,ReI):-write("apel fapt1\n urmeaza FAIL \n"),fail,write("dupa FAIL\n").
factorial(N,N,ReI,ReI):-write("apel fapt2\n").
factorial(N,Cont,ReI,Rez):-write("_ Cont:=",Cont," \n"),
Cont2=Cont+1,write("|_ Cont2:=",Cont2,"\n"),
write("|_ ",Cont2,"<=",N,"::TRUE?\n"),Cont2<=N,
ReI2=ReI*Cont2,
factorial(N,Cont2,ReI2,Rez).
*/
%%%%% 5.1:
/*factorial(N,Rez):-factorial(N,1,1,Rez).
factorial(N,Cont,ReI,Rez):-
N=Cont,Rez=ReI;
write("_ Cont:=",Cont," \n"),
Cont2=Cont+1,write("|_ Cont2:=",Cont2,"\n"),
Cont2<=N,write("|_ ",Cont2,"<=",N,"::TRUE\n"),
ReI2=ReI*Cont2,
factorial(N,Cont2,ReI2,Rez).
*/
%%%%% 5.2:
/*factorial(N,Rez):-factorial(N,1,1,Rez).
factorial(N,Cont,ReI,Rez):-
N=Cont,Rez=ReI,!;
write("_ Cont:=",Cont," \n"),
Cont2=Cont+1,write("|_ Cont2:=",Cont2,"\n"),
Cont2<=N,write("|_ ",Cont2,"<=",N,"::TRUE\n"),
ReI2=ReI*Cont2,
factorial(N,Cont2,ReI2,Rez).
*/
%%%%% 5.3
factorial(N,Rez):-factorial(N,N,1,Rez).
factorial(N,1,ReI,ReI):-!,N>=1.
factorial(N,Cont,ReI,Rez):-write("_ Cont:=",Cont," \n"),
ReI2=ReI*Cont,write("|_ ReI2:=",ReI2,"\n"),
Cont2=Cont-1,write("|_ Cont2:=",Cont2,"\n"),
write("|_ ",Cont2,">=",1,"::TRUE\n"),
factorial(N,Cont2,ReI2,Rez).
Tuesday, 30 October 2007
Publicat de
pau
la
05:36
Subscribe to:
Post Comments (Atom)
4 comments:
Ce face chestia aia ? Sa nu-mi spui ca nu calculeaza decat factorialul unui numar...
ba da ... numai ca e facut in mai multe feluri ...
doamne apara si pazeste....
prea tarziu ...
Post a Comment