PERL   19

Roman

Guest on 16th August 2022 01:06:44 AM

  1. % Prolog program to print out Roman numerals up to 3999.
  2. % S. Tanimoto, CSE 341, Univ. of Washington.
  3.  
  4. toroman(0).
  5. toroman(N) :- N < 4, put("I"), M is N - 1, toroman(M).
  6. toroman(N) :- N = 4, put("I"), put("V").
  7. toroman(N) :- N = 5, put("V").
  8. toroman(N) :- N < 9, put("V"), M is N - 5, toroman(M).
  9. toroman(N) :- N = 9, put("I"), put("X").
  10. toroman(N) :- N < 40, put("X"), M is N - 10, toroman(M).
  11. toroman(N) :- N < 50, put("X"), put("L"), M is N - 40, toroman(M).
  12. toroman(N) :- N < 90, put("L"), M is N - 50, toroman(M).
  13. toroman(N) :- N < 100, put("X"), put("C"), M is N - 90, toroman(M).
  14. toroman(N) :- N < 400, put("C"), M is N - 100, toroman(M).
  15. toroman(N) :- N < 500, put("C"), put("D"), M is N - 400, toroman(M).
  16. toroman(N) :- N < 900, put("D"), put("D"), M is N - 500, toroman(M).
  17. toroman(N) :- N < 1000, put("C"), put("M"), M is N - 900, toroman(M).
  18. toroman(N) :- N < 4000, put("M"), M is N - 1000, toroman(M).
  19.  
  20. % The procedure generate(N) prints out the Roman numerals for 1 through N.
  21. % genhelper keeps track of how many numbers are left to print.
  22.  
  23. genhelper(_,0).
  24. genhelper(N,NLeft) :- toroman(N), nl, !, M is N + 1,
  25.                      NewNLeft is NLeft - 1, genhelper(M,NewNLeft).
  26.  
  27. generate(N) :- genhelper(1, N).

Raw Paste


Login or Register to edit or fork this paste. It's free.