diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2017-02-20 00:05:31 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2017-02-20 00:05:31 -0800 |
commit | 5145dd3aa0c02c9fc496d1432fc4410674206e1d (patch) | |
tree | 540afc30c51da085f5bd8ec3f4c89f6496e7900d /modular.scm | |
parent | 8466d8cfa486fb30d1755c4261b781135083787b (diff) | |
download | slib-5145dd3aa0c02c9fc496d1432fc4410674206e1d.tar.gz slib-5145dd3aa0c02c9fc496d1432fc4410674206e1d.zip |
Import Upstream version 3a2upstream/3a2
Diffstat (limited to 'modular.scm')
-rw-r--r-- | modular.scm | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/modular.scm b/modular.scm index e836100..052bf92 100644 --- a/modular.scm +++ b/modular.scm @@ -245,20 +245,18 @@ ;;@args modulus n2 n3 ;;Returns (@var{n2} ^ @var{n3}) mod @var{modulus}. -(define modular:expt - (let ((integer-expt (and (provided? 'inexact) expt))) - (lambda (m n xpn) - (cond ((= n 1) 1) - ((= n (- m 1)) (if (odd? xpn) n 1)) - ((and (zero? m) integer-expt) (integer-expt n xpn)) - ((negative? xpn) - (modular:expt m (modular:invert m n) (- xpn))) - ((zero? n) 0) - (else - (do ((x n (modular:* m x x)) - (j xpn (quotient j 2)) - (acc 1 (if (even? j) acc (modular:* m x acc)))) - ((<= j 1) - (case j - ((0) acc) - ((1) (modular:* m x acc)))))))))) +(define (modular:expt m n xpn) + (cond ((= n 1) 1) + ((= n (- m 1)) (if (odd? xpn) n 1)) + ((zero? m) (expt n xpn)) + ((negative? xpn) + (modular:expt m (modular:invert m n) (- xpn))) + ((zero? n) 0) + (else + (do ((x n (modular:* m x x)) + (j xpn (quotient j 2)) + (acc 1 (if (even? j) acc (modular:* m x acc)))) + ((<= j 1) + (case j + ((0) acc) + ((1) (modular:* m x acc)))))))) |