repat
typewriter
Clauses
repeat.
repat:- repeat.
typewriter:- repeat, readchar(C), write(C),
char_int(C, 13).
( readchar) ( write) , enter. (.. enter) repeat.
. , , . , , . , , , , . .
. n! (n ) (n)*(n-1)*(n-2)*... *(1). 0 1. ,
n! = (n)*(n-1)!.
. .
procedure Factorial(N:integer; var Y:integer);
Var
NewN, NewY: integer;
Begin
if N = 0 then Y:= 1
Else
if N > 0 then
Begin
NewN:= N 1;
Factorial(NewN, NewY);
Y:= NewY * N
End
end;
, . .
factorial(0, 1).
factorial(N, Y):- N>0,
NewN = N 1,
factorial(NewN, NewY),
Y = NewY * N.
?- factorial(3, Y)
Y=6
factorial(3, Y) :
?- factorial(3, Y)
...
?- factorial(2, NewY)
...
?- factorial(1, NewY)
...
?- factorial(0, NewY) (0!)= 1, NewY= 1
, , , . , . , (0!) = 1 , , 0!.
, - , , , , : , . .. . , , . .
|
|
procedure factorial(N:integer; var Y:integer);
Var
I, P: integer;
Begin
I:=0; P:=1;
while I < N do
Begin
I:= I+1;
P:= P*I
end;
Y:=P;
end;
, , , , , , . ( , ), . , , , , . , :
1. - .
2. .
.
factorial(N, Y):-factorial(0, 1, N, Y).
factorial(N, Y, N, Y):-!.
factorial(I, P, N, Y):-I<N,
NewI=I+1, NewP=P*NewI,
factorial(NewI, NewP, N, Y).
?- factorial(3, Y)
Y=6
1! 0!, 2! 1! . . . , , , . , . , . , . New. . . P. I N, P Y . factorial(3, Y) :
?- factorial(3, Y)
?- factorial(0, 1, 3, Y)
...
?- factorial(1, 1, 3, Y)
...
?- factorial(2, 2, 3, Y)
...
?- factorial(3, 6, 3, Y)
Y:=6