about summary refs log tree commit diff
path: root/usth/ICT3.4/powmod.scm
diff options
context:
space:
mode:
Diffstat (limited to 'usth/ICT3.4/powmod.scm')
-rw-r--r--usth/ICT3.4/powmod.scm9
1 files changed, 9 insertions, 0 deletions
diff --git a/usth/ICT3.4/powmod.scm b/usth/ICT3.4/powmod.scm
new file mode 100644
index 0000000..b21e995
--- /dev/null
+++ b/usth/ICT3.4/powmod.scm
@@ -0,0 +1,9 @@
+(define (square x) (* x x))
+(display
+  (let powmod ((x (read)) (e (read)) (m (read)))
+    ; let's ignore negative e here
+    (cond ((= e 0) 1)
+          ((= e 1) (remainder x m))
+          ((even? e) (remainder (square (powmod x (/ e 2) m)) m))
+          (else (remainder (* (square (powmod x (quotient e 2) m)) x) m)))))
+(newline)