Edit History


Edit history of sicp-ex-1.22

RevTimeChangesOperations
Log
72014-11-10 20:25:20+66 -0 line(s)[View this version, source] [Diff to previous]
Another implementation also easy to understand: {{{scheme ;basic operations (define (square x) (* x x)) (define (divides? a b) (= (remainder b a) 0)) (define (even? n) (= (remainder n 2) 0)) ;smallest divisor computation (define (smallest-divisor n) (define (find-divisor n test) (cond ((> (square test) n) n) ((divides? test n) test) (else (find-divisor n (+ test 1))))) (find-divisor n 2)) ;primality check (define (prime? n) (= n (smallest-divisor n))) ;check primality of consecutive odd integers in some range ;time&primality test ;drRacket has no (runtime) variable; had to substitute it with (current-milliseconds) which is basically same (define (runtime) (current-milliseconds)) (define (timed-prime-test n) (start-prime-test n (runtime))) (define (start-prime-test n start-time) (if (prime? n) (report-prime n (- (runtime) start-time)) #f)) (define (report-prime n elapsed-time) (display n) (display "***") (display elapsed-time) (newline)) ;search counter (define (search-for-primes n counter) (if (even? n) (s-f-p (+ n 1) counter) (s-f-p n counter))) ;it's important to pay attention to the fact that predicate of the first 'if' here calls (timed-prime-test n) which in case of #t computes into two procedures - (report-prime n (elapsed-time)) and 'then' case of the first 'if'. (define (s-f-p n counter) (if (> counter 0) (if (timed-prime-test n) (s-f-p (+ n 2) (- counter 1)) (s-f-p (+ n 2) counter)) "COMPUTATION COMPLETE")) }}} Output: {{{ (search-for-primes 1000 3) 1009***0 1013***0 1019***0 "COMPUTATION COMPLETE" (search-for-primes 1000000 3) 1000003***0 1000033***0 1000037***0 "COMPUTATION COMPLETE" (search-for-primes 1000000000000 3) 1000000000039***359 1000000000061***359 1000000000063***406 "COMPUTATION COMPLETE" }}} (bondaruk9405)
62010-03-25 23:13:04+1 -1 line(s)[View this version, source] [Diff to current|previous]
Correct grammar
52010-03-25 22:55:34+2 -0 line(s)[View this version, source] [Diff to current|previous]
Added navigation
42010-01-21 14:13:08+3 -2 line(s)[View this version, source] [Diff to current|previous]
actually use begin in the final example
32008-09-10 18:04:51+33 -0 line(s)[View this version, source] [Diff to current|previous]
Add sample output and comments
22008-09-10 17:54:05+4 -4 line(s)[View this version, source] [Diff to current|previous]
Highlight the code given in the note as Scheme code.
12008-09-10 17:52:03+69 -0 line(s)[View this version, source] [Diff to current|previous]
Create solution page for SICP exercise 1.22
[Diff from epoch]