[ create a new paste ] login | about

Link: http://codepad.org/Dtz9yq59    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Mar 17:
; lucky numbers

(define (range . args)
  (case (length args)
    ((1) (range 0 (car args) (if (negative? (car args)) -1 1)))
    ((2) (range (car args) (cadr args) (if (< (car args) (cadr args)) 1 -1)))
    ((3) (let ((le? (if (negative? (caddr args)) >= <=)))
           (let loop ((x(car args)) (xs '()))
             (if (le? (cadr args) x)
                 (reverse xs)
                 (loop (+ x (caddr args)) (cons x xs))))))
    (else (error 'range "unrecognized arguments"))))

(define (del xs k)
  (let loop ((i 1) (xs xs) (zs (list)))
    (if (null? xs) (reverse zs)
      (if (zero? (modulo i k))
          (loop (+ i 1) (cdr xs) zs)
          (loop (+ i 1) (cdr xs) (cons (car xs) zs))))))

(define (lucky n)
  (let loop ((i 1) (sieve (range 1 n 2)))
    (let ((p (list-ref sieve i)))
      (if (< (length sieve) p) sieve
        (loop (+ i 1) (del sieve p))))))
 
(display (lucky 304))


Output:
1
(1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79 87 93 99 105 111 115 127 129 133 135 141 151 159 163 169 171 189 193 195 201 205 211 219 223 231 235 237 241 259 261 267 273 283 285 289 297 303)


Create a new paste based on this one


Comments: