learning-sicp

My embarrassing half assed SICP run.
git clone https://kaka.farm/~git/learning-sicp
Log | Files | Refs

exercise-24.scm (1304B)


      1 
      2 ;; Exercise 1.24
      3 
      4 ;; I haven't got any idea what the hell I'm doing. XXX
      5 
      6 (run-delay-if-full-flag
      7  (delay
      8    (let ((port (open-output-file "primes-fast-prime.txt")))
      9 
     10      (define (runtime)
     11        (time-nanosecond (current-time time-process)))
     12 
     13      (define (display x)
     14        (format port "~a" x))
     15 
     16      (define (newline)
     17        (format port "\n"))
     18 
     19      (define (timed-prime-test n)
     20        ;;  (newline)
     21        ;;  (display n)
     22        (start-timed-test n (runtime)))
     23 
     24      (define (start-timed-test n start-time)
     25        (cond
     26         ((fast-prime? n (inexact->exact (ceiling (* n 0.01))))
     27          (newline)
     28          (display "[")
     29          (display n)
     30          (report-prime (- (runtime)
     31                           start-time)))
     32         (else  #f)))
     33 
     34      (define (report-prime elapsed-time)
     35        (display ", ")
     36        (display elapsed-time)
     37        (display "],")
     38        (newline)
     39        #t)
     40 
     41      (define (search-for-primes start-number end-number)
     42        (cond
     43         ((> start-number end-number) '())
     44         ((timed-prime-test start-number)
     45          (cons start-number
     46                (search-for-primes (+ start-number 2) end-number)))
     47         (else (search-for-primes (+ start-number 2) end-number))))
     48 
     49      (display "[")
     50 
     51      (search-for-primes 3 100001)
     52 
     53      (display "]")
     54      (newline))))