Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

1.23

  • Loading branch information...
commit 3c4db602f72957850036ff30cc149e6077f4b837 1 parent b7b1c48
@cosmin authored
Showing with 62 additions and 0 deletions.
  1. +62 −0 exercise-1-23.ss
View
62 exercise-1-23.ss
@@ -0,0 +1,62 @@
+(define runtime current-inexact-milliseconds)
+(define (square x) (* x x))
+(define (divides? a b) (= (remainder b a) 0))
+
+;; from 1.22
+
+(define (smallest-divisor n)
+ (find-divisor n 2))
+
+(define (prime? n)
+ (= n (smallest-divisor n)))
+
+(define (timed-prime-test n)
+ (display n)
+ (start-prime-test n (runtime)))
+
+(define (start-prime-test n start-time)
+ (if (prime? n)
+ (report-prime (- (runtime) start-time))
+ (newline)))
+
+(define (report-prime elapsed-time)
+ (display " *** ")
+ (display elapsed-time)
+ (newline))
+
+(define (even? n) (= 0 (remainder n 2)))
+
+(define (search-for-primes start end)
+ (if (even? start)
+ (search-for-primes (+ start 1) end) ; skip evens
+ (cond ((< start end) (timed-prime-test start)
+ (search-for-primes (+ start 2) end)))))
+
+;; new code for 1.23
+
+(define (next n)
+ (if (= n 2)
+ 3
+ (+ n 2)))
+
+(define (find-divisor n test-divisor)
+ (cond ((> (square test-divisor) n) n)
+ ((divides? test-divisor n) test-divisor)
+ (else (find-divisor n (next test-divisor)))))
+
+;; old results
+
+; > (search-for-primes 1000000 1000060)
+; 1000003 *** 0.140869140625
+; 1000033 *** 0.112060546875
+; 1000037 *** 0.09716796875
+
+;; new results
+
+; > (search-for-primes 1000000 1000060)
+; 1000003 *** 0.06103515625
+; 1000033 *** 0.06005859375
+; 1000037 *** 0.06103515625
+
+;; the new code is close to 2x faster. This might be due to invoking
+;; next being more expensive than directly invoking +
Please sign in to comment.
Something went wrong with that request. Please try again.