cl-rsm-mod/ 0002755 0001751 0001751 00000000000 10760276670 013301 5 ustar pvaneynd pvaneynd cl-rsm-mod/copying 0000644 0001751 0001751 00000002615 07724323106 014667 0 ustar pvaneynd pvaneynd Copyright (c) 2003 by R. Scott McIntire All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the Authors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cl-rsm-mod/package.lisp 0000644 0001751 0001751 00000002721 07745336073 015570 0 ustar pvaneynd pvaneynd ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*- ;;;; ************************************************************************* ;;;; FILE IDENTIFICATION ;;;; ;;;; Name: package.lisp ;;;; Purpose: Package definition for Modular arithmetic. ;;;; Author: R. Scott McIntire ;;;; Date Started: Aug 2003 ;;;; ;;;; $Id: package.lisp,v 1.4 2003/10/03 02:21:53 rscottmcintire Exp $ ;;;; ************************************************************************* (in-package #:cl-user) (defpackage rsm.mod (:use #:cl) (:shadow #:+ #:* #:^) (:documentation "This package supports modular arithmetic. Export Summary: +: Add numbers over Z mod n. *: Multiply numbers over Z mod n. ^: Exponentiate over Z mod n. euler-phi: Return the Euler phi function of a number. factors : Return the factors of a number. gcd-with-pair: Gets the gcd of two numbers a and b returning also the integer pair, (r s), such that r*a + s*b = gcd(a,b). has-inverse-p: Does a number have in inverse in Z mod n? inverse : Find the inverse (if it exists) in Z mod n. ppow : Exponentiate over Z mod p where p is prime. rational-approx: Returns a simple rational approximation within a given tolerance. solve-congruence-system: Solve for x: x = a_i mod m_i; i in [1,N] ") (:export #:+ #:* #:^ #:euler-phi #:factors #:gcd-with-pair #:has-inverse-p #:inverse #:ppow #:rational-approx #:solve-congruence-system)) cl-rsm-mod/mod-test.lisp 0000644 0001751 0001751 00000012725 07745336072 015735 0 ustar pvaneynd pvaneynd ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Package: rsm.mod.test -*- ;;;; ************************************************************************* ;;;; FILE IDENTIFICATION ;;;; ;;;; Name: mod-test.lisp ;;;; Purpose: Regression testing for modular arithmetic. ;;;; Author: R. Scott McIntire ;;;; Date Started: Aug 2003 ;;;; ;;;; $Id: mod-test.lisp,v 1.7 2003/10/21 20:59:44 rscottmcintire Exp $ ;;;; ************************************************************************* (in-package #:cl-user) (defpackage rsm.mod.test (:use #:cl #:ptester) (:documentation "Provides a test harness for modular arithmetic.") ) (in-package rsm.mod.test) ;;;; RUN THE TESTS. (defun run-mod-tests () (with-tests (:name "MOD TESTS") (test 2 (rsm.mod:+ 3 3 5) :fail-info "Test 1") (test 1 (rsm.mod:* 3 2 5) :fail-info "Test 2") (test 2 (rsm.mod:ppow 12 100 7) :fail-info "Test 3") (test 170 (rsm.mod:^ 213317 527131763 173) :fail-info "Test 4") (test '(2 5) (rsm.mod:factors 100) :test #'equal :fail-info "Test 5") (test '(2 2 5 5) (rsm.mod:factors 100 :no-dups nil) :test #'equal :fail-info "Test 6") (test 12 (rsm.mod:euler-phi 13) :fail-info "Test 7") (test 40 (rsm.mod:euler-phi 100) :fail-info "Test 8") (test 9 (rsm.mod:inverse 9 10) :fail-info "Test 9") (test 1529 (rsm.mod:inverse 2341 7919) :fail-info "Test 10") (test 15651 (rsm.mod:ppow 7919 7232937498729837429 104729) :fail-info "Test 11") (test 777/898 (rsm.mod:rational-approx (/ 2.71828 3.14159) 0.000001) :fail-info "Test 12") (test 22/7 (rsm.mod:rational-approx pi 0.002) :fail-info "Test 13") (test 355/113 (rsm.mod:rational-approx pi 0.001) :fail-info "Test 14") (test 152974058/176796123 (rsm.mod:rational-approx 27182845904523536/31415926535897932 0.0000000000000001) :fail-info "Test 15") (test '(12 (1 0)) (rsm.mod:gcd-with-pair 12 60) :test #'equal :multiple-values t :fail-info "Test 16") (test '(2 (1 -2)) (rsm.mod:gcd-with-pair 14 6) :test #'equal :multiple-values t :fail-info "Test 17") (test '(2 (-2 1)) (rsm.mod:gcd-with-pair 6 14) :test #'equal :multiple-values t :fail-info "Test 18") (test '(1 (-1035 676)) (rsm.mod:gcd-with-pair 1529 2341) :test #'equal :multiple-values t :fail-info "Test 19") (test '(2 (-502648 26455)) (rsm.mod:gcd-with-pair 123456 2345678) :test #'equal :multiple-values t) (test 15651 (rsm.mod:ppow 7919 7232937498729837429 104729) :fail-info "Test 21") (test 21762 (rsm.mod:ppow 7919 72329374987298374298 104729) :fail-info "Test 22") (test 43685 (rsm.mod:ppow 7919 723293749872983742983 104729) :fail-info "Test 23") (test 43685 (rsm.mod:^ 7919 723293749872983742983 104729 :e-phi 104728) :fail-info "Test 24") (test 43685 (rsm.mod:^ 7919 723293749872983742983 104729) :fail-info "Test 25") (test 56170 (rsm.mod:^ 79111 723293749872983742983 104727) :fail-info "Test 26") (test 355/113 (rsm.mod:rational-approx pi 0.0000003) :fail-info "Test 27") (test 12317 (rsm.mod:solve-congruence-system '(1 2 2 4 8 6) '(2 3 5 7 11 13)) :fail-info "Test 28") (test 29243 (rsm.mod:solve-congruence-system '(1 2 3 4 5 6) '(2 3 5 7 11 13)) :fail-info "Test 29") (test 54916118429448 (rsm.mod:solve-congruence-system '(1 2 3 4 5 6) '(7909 101 13 37 97 2003)) :fail-info "Test 30") (test 0 (rsm.mod:solve-congruence-system '(0 0 0) '(2 3 5)) :fail-info "Test 31") (test 23 (rsm.mod:solve-congruence-system '(1 2 3) '(2 3 5)) :fail-info "Test 32") (test t (rsm.mod:has-inverse-p 123 713) :fail-info "Test 33") (test nil (rsm.mod:has-inverse-p 123 717) :fail-info "Test 34") (test nil (rsm.mod:has-inverse-p 3 12) :fail-info "Test 35") (test t (rsm.mod:has-inverse-p 3 8) :fail-info "Test 36") (test 16041953 (rsm.mod:solve-congruence-system '(1 2 3 4 5) '(8 9 25 77 221)) :fail-info "Test 37") (test 0 (rsm.mod:inverse 8 10) :fail-info "Test 38") (test nil (rsm.mod:inverse 8 10 nil nil) :fail-info "Test 39") (test-error (rsm.mod:inverse 8 10 t) :fail-info "Test 40") (test -1 (rsm.mod:inverse 8 10 nil -1) :fail-info "Test 41") (test 3 (rsm.mod:inverse 7 10 nil -1) :fail-info "Test 42") (test 28 (rsm.mod:^ 7 2134145213317 33 :e-phi 20) :fail-info "Test 43") ) t ) cl-rsm-mod/rsm-mod.html 0000644 0001751 0001751 00000014611 07745336073 015551 0 ustar pvaneynd pvaneynd
Author : R. Scott McIntireVersion: 1.2Overview:This package supports modular arithmetic. Export Summary: +: Add numbers over Z mod n. *: Multiply numbers over Z mod n. ^: Exponentiate over Z mod n. euler-phi: Return the Euler phi function of a number. factors : Return the factors of a number. gcd-with-pair: Gets the gcd of two numbers a and b returning also the integer pair, (r s), such that r*a + s*b = gcd(a,b). has-inverse-p: Does a number have in inverse in Z mod n? inverse : Find the inverse (if it exists) in Z mod n. ppow : Exponentiate over Z mod p where p is prime. rational-approx: Returns a simple rational approximation within a given tolerance. solve-congruence-system: Solve for x: x = a_i mod m_i; i in [1,N] |
*  (mod &rest args)Multiply <args> in Mod <mod> arithmetic. +  (mod &rest args)Add <args> in Mod <mod> arithmetic. ^  (b n mod &key (e-phi 0))Raise <b> to the <n>th power mod <mod> by repeated squaring. If <e-phi> is non zero, use the generalization of Fermat's little theorem: b^phi(mod) = 1 mod mod, when the gcd of b and mod is 1. The theorem is used to replace b^n with b^r where r = mod(n, phi(mod)) and phi is the Euler Phi function. euler-phi  (n)Computes the Euler Phi function of <n>. factors  (n &key (no-dups t))Computes and returns a list of the primes factors of <n>. If <no-dups> is true, then no multiple entries of a factor are returned. gcd-with-pair  (n m)Returns two values: The gcd of <n> and <m>, and the list (r s) such that r * n + s * m = gcd(n,m). has-inverse-p  (a n)Does <a> have an inverse in Z mod <n>? inverse  (a n &optional (error nil) (not-invert-return 0))Finds the inverse of <a> in Z mod <n>. If <a> inverse does not exist, an error is thrown if <error> is non nil. If <error> is nil, then <not-invert-return> is returned. ppow  (b n p)Raise <b> to the <n>th power in the field Z mod <p>. Here <p> must be prime. rational-approx  (number &optional (epsilon nil))Find a simple rational approximation to <number> within <epsilon>. solve-congruence-system  (as ms)Use the Chinese remainder theorem to solve for x, the system of congruences: x = as_i mod ms_i. The moduli, <ms>, must all be pairwise relatively prime. x will be unique in Z mod (product of <ms>'s). |