mod-arithmatic.lisp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. ;;;; modular-arithmatic-tables.lisp
  2. ;;;;
  3. ;;;; Copyright (c) 2016 Samuel W. Flint <swflint@flintfam.org>
  4. (defpackage #:mod-arithmatic
  5. (:use #:cl)
  6. (:export :print-tables-z_n
  7. :make-z_n-tables))
  8. (in-package #:mod-arithmatic)
  9. ;;; "mod-arithmatic" goes here.
  10. (defun euclid-gcd (a b)
  11. (check-type a integer)
  12. (check-type b integer)
  13. (cond
  14. ((zerop b)
  15. a)
  16. ((= a b)
  17. a)
  18. ((< a b)
  19. (euclid-gcd b a))
  20. (t
  21. (euclid-gcd b (mod a b)))))
  22. (defun z_n-divisors (n)
  23. (remove-if (lambda (x)
  24. (not (= 1 (euclid-gcd x n))))
  25. (loop for i from 1 to (1- n) collect i)))
  26. (defun z_n-addition-table (n)
  27. (loop for i from 0 to (1- n)
  28. collect (loop for j from 0 to (1- n)
  29. collect (mod (+ i j) n))))
  30. (defun z_n-multiplication-table (n)
  31. (loop for i from 0 to (1- n)
  32. collect (loop for j from 0 to (1- n)
  33. collect (mod (* i j) n))))
  34. (defun print-z_n-addition-table (n stream)
  35. (format stream "~{~{~4d~}~&~}" (z_n-addition-table n)))
  36. (defun print-z_n-multiplication-table (n stream)
  37. (format stream "~{~{~4d~}~&~}" (z_n-multiplication-table n)))
  38. (defun print-tables-z_n (n stream)
  39. (check-type n integer)
  40. (format stream "Addition Table (Z_~d):~%" n)
  41. (print-z_n-addition-table n stream)
  42. (format stream "~&~%Multiplication Table (Z_~d):~%" n)
  43. (print-z_n-multiplication-table n stream)
  44. (format stream "~&~%Divisors in Z_~d:~&" n)
  45. (format stream "~{~a~^, ~}" (z_n-divisors n)))
  46. (defun make-z_n-tables (n filename)
  47. (with-open-file (output filename
  48. :direction :output
  49. :if-exists :overwrite
  50. :if-does-not-exist :create)
  51. (print-tables-z_n n output)))
  52. ;;; End mod-arithmatic