From: Ramakrishnan Muthukrishnan Date: Sun, 25 Apr 2010 05:52:03 +0000 (+0530) Subject: Solution to exercise 1.28. Please look at the comments and see if my X-Git-Url: https://git.rkrishnan.org/uri?a=commitdiff_plain;h=434c658922cd0dc6bfb583765fbd902e4d527391;p=sicp.git Solution to exercise 1.28. Please look at the comments and see if my assumption is right. --- diff --git a/src/sicp/ch1_2.clj b/src/sicp/ch1_2.clj index 43cebbe..5e6375d 100644 --- a/src/sicp/ch1_2.clj +++ b/src/sicp/ch1_2.clj @@ -761,3 +761,59 @@ Average : 0.021450125000000004 ) ;; exercise 1.27 +(comment + "Some notes on Carmichael numbers: Carmichael numbers are those that fail + Fermat little test. That is, for any n in the Carmichael set, + (prime? n) => false + (fermat-test n) => true." + ) +(defn brute-force-fermat-test [n] + (try-all 2 n)) + +(defn try-all [a n] + (cond (= a n) true + (try-it a n) (try-all (inc a) n) + :else false)) +(comment + "all the given numbers pass the above test, i.e. for every a < n, + a^n mod n === a mod n" + user> (brute-force-fermat-test 561) + true + user> (brute-force-fermat-test 1105) + true + user> (brute-force-fermat-test 1729) + true + user> (brute-force-fermat-test 2465) + true + user> (brute-force-fermat-test 2821) + true + user> (brute-force-fermat-test 6601) + true + ) + +;;; exercise 1.28 +(defn expmod2 [base exp m] + (cond (= exp 0) 1 + (even? exp) (square-test (expmod2 base (/ exp 2) m) m) + :else (rem (* base (expmod2 base (dec exp) m)) + m))) + +(defn square-test [x m] + (if (and (not (or (= x 1) (= x (- m 1)))) + (= (rem (square x) m) 1)) + 0 + (rem (square x) m))) + +(defn miller-rabin-test [n] + (try-it (+ 2 (rand-int (- n 2))) + n)) + +(defn try-it [a n] + (= (expmod2 a (- n 1) n) 1)) + +(comment + "If the random number generated (a) is 1, then this returns false + positives. So generate random numbers between 2 and n-1. (is this + assumption correct?) " + + ) \ No newline at end of file