Factorial 2

Description

Factorial 2 way

Source: Adventure in Prolog (on-line tutorial)

Download

Program source code: factorial_2.pl

Listing

factorial_1(1,1).
factorial_1(N,F):-
  N > 1,
  NN is N - 1,
  factorial_1(NN,FF),
  F is N * FF.
 
% ?- factorial_1(5,X).
% X = 120
 
factorial_2(1,F,F).
factorial_2(N,T,F):-
  N > 1,
  TT is N * T,
  NN is N - 1,
  factorial_2(NN,TT,F).
 
% ?- factorial_2(5,1,X).
% X = 120

Comments

pl/prolog/pllib/factorial_2.txt · ostatnio zmienione: 2019/06/27 15:50 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0