|
| 1 | +#lang racket |
| 2 | + |
| 3 | +(begin |
| 4 | + ;; sieve for computing primes, based on GT Benchmarks |
| 5 | + |
| 6 | + ;;-------------------------------------------------------------------------------------------------- |
| 7 | + ;; streams |
| 8 | + |
| 9 | + (define (stream hd thunk) |
| 10 | + (cons hd thunk)) |
| 11 | + |
| 12 | + (define (stream-first s) |
| 13 | + (car s)) |
| 14 | + |
| 15 | + (define (stream-rest s) |
| 16 | + (cdr s)) |
| 17 | + |
| 18 | + ;(: make-stream (-> Natural (-> stream) stream)) |
| 19 | + (define (make-stream hd thunk) |
| 20 | + (stream hd thunk)) |
| 21 | + |
| 22 | + ;; Destruct a stream into its first value and the new stream produced by de-thunking the tail |
| 23 | + ;(: stream-unfold (-> stream (cons Natural stream))) |
| 24 | + (define (stream-unfold st) |
| 25 | + (cons (stream-first st) ((stream-rest st)))) |
| 26 | + |
| 27 | + ;; [stream-get st i] Get the [i]-th element from the stream [st] |
| 28 | + ;(: stream-get (-> stream Natural Natural)) |
| 29 | + (define (stream-get st i) |
| 30 | + (match (stream-unfold st) |
| 31 | + [(cons hd tl) |
| 32 | + (cond [(= i 0) hd] |
| 33 | + [else (stream-get tl (sub1 i))])])) |
| 34 | + |
| 35 | + ;; [stream-take st n] Collect the first [n] elements of the stream [st]. |
| 36 | + ;(: stream-take (-> stream Natural (Listof Natural))) |
| 37 | + (define (stream-take st n) |
| 38 | + (cond [(= n 0) '()] |
| 39 | + [else (match (stream-unfold st) |
| 40 | + [(cons hd tl) |
| 41 | + (cons hd (stream-take tl (sub1 n)))])])) |
| 42 | + |
| 43 | + ;;-------------------------------------------------------------------------------------------------- |
| 44 | + ;; sieve |
| 45 | + |
| 46 | + ;; `count-from n` Build a stream of integers starting from `n` and iteratively adding 1 |
| 47 | + ;(: count-from (-> Natural stream)) |
| 48 | + (define (count-from n) |
| 49 | + (make-stream n (λ () (count-from (add1 n))))) |
| 50 | + |
| 51 | + ;; `sift n st` Filter all elements in `st` that are equal to `n`. |
| 52 | + ;; Return a new stream. |
| 53 | + ;(: sift (-> Natural stream stream)) |
| 54 | + (define (sift n st) |
| 55 | + (match (stream-unfold st) |
| 56 | + [(cons hd tl) |
| 57 | + (cond [(= 0 (modulo hd n)) (sift n tl)] |
| 58 | + [else (make-stream hd (λ () (sift n tl)))])])) |
| 59 | + |
| 60 | + ;; `sieve st` Sieve of Eratosthenes |
| 61 | + ;(: sieve (-> stream stream)) |
| 62 | + (define (sieve st) |
| 63 | + (match (stream-unfold st) |
| 64 | + [(cons hd tl) |
| 65 | + (make-stream hd (λ () (sieve (sift hd tl))))])) |
| 66 | + |
| 67 | + ;; stream of prime numbers |
| 68 | + ;(: primes (-> stream) |
| 69 | + (define (primes) (sieve (count-from 2))) |
| 70 | + |
| 71 | + ;;-------------------------------------------------------------------------------------------------- |
| 72 | + ;; reading numbers |
| 73 | + |
| 74 | + (define (* n m) |
| 75 | + (match n |
| 76 | + [0 0] |
| 77 | + [1 m] |
| 78 | + [n (+ m (* (sub1 n) m))])) |
| 79 | + |
| 80 | + (define (expt n m) |
| 81 | + (match m |
| 82 | + [0 1] |
| 83 | + [1 n] |
| 84 | + [m (* n (expt n (sub1 m)))])) |
| 85 | + |
| 86 | + (define (read-number) |
| 87 | + (digits->number (read-digits))) |
| 88 | + |
| 89 | + (define (read-digits) |
| 90 | + (let ((c (read-char))) |
| 91 | + (if (eof-object? c) |
| 92 | + '() |
| 93 | + (if (eq? #\newline c) |
| 94 | + '() |
| 95 | + (cons c (read-digits)))))) |
| 96 | + |
| 97 | + (define (digits->number ds) |
| 98 | + (match ds |
| 99 | + ['() 0] |
| 100 | + [(cons d ds) |
| 101 | + (+ (* (digit->number d) |
| 102 | + (expt 10 (length ds))) |
| 103 | + (digits->number ds))])) |
| 104 | + |
| 105 | + (define (digit->number d) |
| 106 | + (match d |
| 107 | + [#\0 0] |
| 108 | + [#\1 1] |
| 109 | + [#\2 2] |
| 110 | + [#\3 3] |
| 111 | + [#\4 4] |
| 112 | + [#\5 5] |
| 113 | + [#\6 6] |
| 114 | + [#\7 7] |
| 115 | + [#\8 8] |
| 116 | + [#\9 9])) |
| 117 | + |
| 118 | + ;;-------------------------------------------------------------------------------------------------- |
| 119 | + (define (modulo n m) |
| 120 | + (if (< n m) |
| 121 | + n |
| 122 | + (modulo (- n m) m))) |
| 123 | + |
| 124 | + ;;-------------------------------------------------------------------------------------------------- |
| 125 | + ;; computes nth prim |
| 126 | + |
| 127 | + (stream-get (primes) (sub1 (read-number)))) |
0 commit comments