Kimossab

[IA] - F3

Apr 18th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.67 KB | None | 0 0
  1. %somatorio
  2. somatorio(1,1).
  3. somatorio(N,S):- N1 is N-1,somatorio(N1,S1), S is S1+N.
  4. %factorial
  5. factorial(0,1).
  6. factorial(N,S):- N1 is N-1,factorial(N1,S1), S is S1*N.
  7. %comprimento
  8. comprimento([],0).
  9. comprimento([_|Cauda],S):-comprimento(Cauda,S1),S is S1+1.
  10.  
  11. n_elemento(1,[C|_],C).
  12. n_elemento(X,[_|Cauda],N):-X1 is X-1,n_elemento(X1,Cauda,N).
  13.  
  14. potencia(B,1,B).
  15. potencia(B,N,P):-N1 is N-1,potencia(B,N1,P1),P is P1*B.
  16.  
  17. %max(X,Y,Max):-X>Y -> Max is X ; Max is Y.
  18. %OU
  19. max(X,Y,X):-X>=Y,!.
  20. max(_,Y,Y).
  21.  
  22. %maxlista([C],C).
  23. %maxlista([C|Cauda],X):-maxlista(Cauda,X1),C>X1 -> X is C ; X is X1.
  24. %OU
  25. maxlista([C],C):-!.
  26. maxlista([C|Cauda],X):-maxlista(Cauda,X1),max(C,X1,X).
Add Comment
Please, Sign In to add comment