Advertisement
mllm

exp(x) taylor

Mar 31st, 2017
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.54 KB | None | 0 0
  1. fact(0,1).
  2. fact(N,R):- N1 is N-1,
  3.             fact(N1,R1),
  4.             R is N*R1,!.
  5.  
  6. % Calculating the exponential (complex version)
  7. exp(_,0,1).
  8. exp(X,N,Res):- (N > 0,
  9.                fact(N,Fact),
  10.                Frac is X**N/Fact,              
  11.                N1 is N-1,
  12.                exp(X,N1,Res1),
  13.                Res is Res1 + Frac);
  14.                (N < 0, write("N must be positive")).
  15.  
  16. %% Simple version:
  17. exp2(_,0,1).
  18. exp2(X,N,Res):- fact(N,Fact),
  19.                Frac is X**N/Fact,              
  20.                N1 is N-1,
  21.                exp2(X,N1,Res1),
  22.                Res is Res1 + Frac.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement