Guest User

Untitled

a guest
Oct 15th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. /* Este factorial es recursivo, pero operamos de alguna forma la
  2. * llamada recursiva (la multiplicamos por n).
  3. */
  4.  
  5. long factorial(long n) {
  6. if (n == 0) {
  7. return 1;
  8. }
  9. else {
  10. return n * factorial(n-1);
  11. }
  12. }
  13.  
  14. /* Este factorial no es recursivo, pero la función auxiliar
  15. * factorial_helper sí. Aquí, la llamada recursiva no se toca, sino
  16. * que se devuelve entera. *Eso* es recursividad final (tail call).
  17. */
  18.  
  19. long factorial_helper(long n, long ac) {
  20. if (n == 0 ) {
  21. return ac;
  22. }
  23. else {
  24. return factorial_helper(n -1, n * ac);
  25. }
  26. }
  27.  
  28. long factorial(long n) {
  29. return factorial_helper(n, 1);
  30. }
Add Comment
Please, Sign In to add comment