about summary refs log tree commit diff
path: root/usth/ICT3.4/powmod.scm
blob: b21e995c5b44ba4c29fa9e7684df444ecef091e2 (plain) (blame)
1
2
3
4
5
6
7
8
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)