From 817415a43c977bfc90abec58c9bbec348d98b9a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sat, 13 Aug 2022 02:14:43 -0300 Subject: Initial Commit --- modulo-arithmetic.rkt | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 modulo-arithmetic.rkt (limited to 'modulo-arithmetic.rkt') diff --git a/modulo-arithmetic.rkt b/modulo-arithmetic.rkt new file mode 100644 index 0000000..5d80e6c --- /dev/null +++ b/modulo-arithmetic.rkt @@ -0,0 +1,38 @@ +#lang racket + +(define cardinality 487) ; must be prime + +(define (extended-euclid a b) + (define (euloop r s t) + (if (= 0 (second r)) + (list r s t) + (let ((q (quotient (first r) (second r)))) + (euloop (list (second r) (- (first r) (* q (second r)))) + (list (second s) (- (first s) (* q (second s)))) + (list (second t) (- (first t) (* q (second t)))))))) + (match (euloop (list a b) (list 1 0) (list 0 1)) + ((list r s t) + (list (first r) (first s) (first t))))) + +(define (inverse n) + (match (extended-euclid n cardinality) + ((list gdc x y) + (modulo x cardinality)))) + +(define (add . args) + (modulo (apply + args) cardinality)) + +(define (sub . args) + (modulo (apply - args) cardinality)) + +(define (mul . args) + (modulo (apply * args) cardinality)) + +(define (div a b) + (mul a (inverse b))) + +(provide cardinality + (rename-out (add +) + (sub -) + (mul *) + (div /))) \ No newline at end of file -- cgit v1.2.3