1) DrRacket
2) https://inst.eecs.berkeley.edu/~cs61a/sp15/assets/interpreter/scheme.html
Using both of the interpreters above for Hacker's version with arguments (expmod 11 17 17)
yields different answers. 11 for DrRacket (as the procedure should) and 0 for inst.eecs.berkeley.edu
Included is an example evaluation at the bottom. Substituting for all (< base m)
into both interpreters yields different answers when using inst.eecs.berkeley.edu and therefore makes the entire timed-prime-test fail for this interpreter.
My question: What is the underlying issue with this interpreter? Is this problem due to an error in interpretation? A different method for evaluation between the two?
(define (expmod base exp m)
(remainder (fast-expt base exp) m))
(define (fast-expt b n)
(cond ((= n 0) 1)
((even? n) (square (fast-expt b (/ n 2))))
(else (* b (fast-expt b (- n 1))))))
(define (even? n)
(= (remainder n 2) 0))
(define (square x)
(* x x))
(remainder (fast-expt 11 17) 17)
(remainder (* 11 (fast-expt 11 16)) 17)
(remainder (* 11 (square (fast-expt 11 8))) 17)
(remainder (* 11 (square (square (fast-expt 11 4)))) 17)
(remainder (* 11 (square (square (square (fast-expt 11 2))))) 17)
(remainder (* 11 (square (square (square (square (fast-expt 11 1)))))) 17)
(remainder (* 11 (square (square (square (square (* 11 (fast-expt 11 0))))))) 17)
(remainder (* 11 (square (square (square (square (* 11 1)))))) 17)
(remainder (* 11 (square (square (square (square 11))))) 17)
(remainder (* 11 (square (square (square 121)))) 17)
(remainder (* 11 (square (square 14641))) 17)
(remainder (* 11 (square 214358881)) 17)
(remainder (* 11 45949729863572161) 17)
(remainder 505447028499293771 17)
A link to online SICP
expmod
computes the powerp=base^exp
first, then finds the modulo. This means that the temporary resultp
can be very large. So large that it can't be represented exactly using floating point. Instead you must write a variation of yourfast-expt
that computes the modulo of each temporary result. In this way all numbers in the computation will be below the modulo - and you will get the correct result also in the Berkeley Scheme implementation. – soegaard