Skip to content
Browse files

1.24

  • Loading branch information...
1 parent 3c4db60 commit 130cc0cd171fec223dc9a3fc648e871bec584a0a @cosmin committed Feb 14, 2012
Showing with 42 additions and 0 deletions.
  1. +42 −0 exercise-1-24.ss
View
42 exercise-1-24.ss
@@ -0,0 +1,42 @@
+(define runtime current-inexact-milliseconds)
+(define (square x) (* x x))
+(define (even? n) (= 0 (remainder n 2)))
+
+(define (expmod base exp m)
+ (cond ((= exp 0) 1)
+ ((even? exp)
+ (remainder (square (expmod base (/ exp 2) m))
+ m))
+ (else
+ (remainder (* base (expmod base (- exp 1) m))
+ m))))
+
+(define (fermat-test n)
+ (define (try-it a)
+ (= (expmod a n n) a))
+ (try-it (+ 1 (random (- n 1)))))
+
+(define (fast-prime? n times)
+ (cond ((= times 0) true)
+ ((fermat-test n) (fast-prime? n (- times 1)))
+ (else false)))
+
+(define (timed-prime-test n)
+ (display n)
+ (start-prime-test n (runtime)))
+
+(define (start-prime-test n start-time)
+ (if (fast-prime? n 10)
+ (report-prime (- (runtime) start-time))
+ (newline)))
+
+(define (report-prime elapsed-time)
+ (display " *** ")
+ (display elapsed-time)
+ (newline))
+
+;; at the size of the numbers we are using the chosen number of
+;; iterations has a more signficant impact on the run-time then the
+;; size of the input. For a more thorough analysis check out
+;;
+;; http://www.billthelizard.com/2010/02/sicp-exercise-124-fermat-test.html

0 comments on commit 130cc0c

Please sign in to comment.
Something went wrong with that request. Please try again.