Question
IN SCHEME PROGRAMMING LANGUAGE (RACKET) (define +mod (lambda (a b n) (modulo (+ a b) n) )) (define -mod (lambda (a b n) (modulo (-
IN SCHEME PROGRAMMING LANGUAGE (RACKET)
(define +mod
(lambda (a b n)
(modulo (+ a b) n)
))
(define -mod
(lambda (a b n)
(modulo (- a b) n)
))
(define *mod
(lambda (a b n)
(modulo (* a b) n)
Write a procedure exptmod that computes a b (mod n) using repeated squaring. You should use the given modular arithmetic operations, particularly mod, in your solution. Do not use expt or slow-exptmod in your solution.
(define exptmod (lambda (a b n) YOUR-CODE-HERE))
Test your code for at least the following cases:
(exptmod 2 0 10) ; -> 1 (exptmod 2 3 10) ; -> 8 (exptmod 3 4 10) ; -> 1 (exptmod 2 15 100) ; -> 68 (exptmod -5 3 100) ; -> 75
(What I have so far is given below but it doesn't really work.)
(define exptmod
(lambda (a b n)
(cond ((= b 1) 1)
((= b 0) (*mod 1 1 n))
((even? b) (*mod (* a a) (exptmod a (/ b 2) n) n))
(else (*mod (exptmod (* a (- b 1) n) n)))))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started