From 82048ad9e3e4a96a38c8fa6a529798f40e33acb1 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sat, 14 Nov 2020 11:10:22 +0700 Subject: [usth/ICT3.4] Secure information --- usth/ICT3.4/powmod.scm | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 usth/ICT3.4/powmod.scm (limited to 'usth/ICT3.4/powmod.scm') 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) -- cgit 1.4.1