Guest User

Prolog Pyramid

a guest
Jul 9th, 2025
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. star :- write("*").
  2.  
  3. % print N stars
  4. star_iter(N) :-
  5. N > 0,
  6. star,
  7. N1 is N - 1,
  8. star_iter(N1).
  9. % base case = true
  10. star_iter(0).
  11.  
  12. pyramid(N) :- pyramid_helper(N, N).
  13.  
  14. % print a pyramid
  15. pyramid_helper(N, W) :-
  16. N > 0,
  17. star_iter(W - N + 1),
  18. write("\n"),
  19. N1 is N - 1,
  20. pyramid_helper(N1, W).
  21. % base case = true
  22. pyramid_helper(0, _).
  23.  
  24. read_pyramid :-
  25. read(X),
  26. pyramid(X).
  27.  
  28. :- initialization(read_pyramid).
  29.  
Advertisement
Add Comment
Please, Sign In to add comment