fork download
; searching an infinite array

(define rand #f)
(define randint #f)
(let ((two31 #x80000000) (a (make-vector 56 -1)) (fptr #f))
  (define (mod-diff x y) (modulo (- x y) two31)) ; generic version
  ; (define (mod-diff x y) (logand (- x y) #x7FFFFFFF)) ; fast version
  (define (flip-cycle)
    (do ((ii 1 (+ ii 1)) (jj 32 (+ jj 1))) ((< 55 jj))
      (vector-set! a ii (mod-diff (vector-ref a ii) (vector-ref a jj))))
    (do ((ii 25 (+ ii 1)) (jj 1 (+ jj 1))) ((< 55 ii))
      (vector-set! a ii (mod-diff (vector-ref a ii) (vector-ref a jj))))
    (set! fptr 54) (vector-ref a 55))
  (define (init-rand seed)
    (let* ((seed (mod-diff seed 0)) (prev seed) (next 1))
      (vector-set! a 55 prev)
      (do ((i 21 (modulo (+ i 21) 55))) ((zero? i))
        (vector-set! a i next) (set! next (mod-diff prev next))
        (set! seed (+ (quotient seed 2) (if (odd? seed) #x40000000 0)))
        (set! next (mod-diff next seed)) (set! prev (vector-ref a i)))
      (flip-cycle) (flip-cycle) (flip-cycle) (flip-cycle) (flip-cycle)))
  (define (next-rand)
    (if (negative? (vector-ref a fptr)) (flip-cycle)
      (let ((next (vector-ref a fptr))) (set! fptr (- fptr 1)) next)))
  (define (unif-rand m)
    (let ((t (- two31 (modulo two31 m))))
      (let loop ((r (next-rand)))
        (if (<= t r) (loop (next-rand)) (modulo r m)))))
  (init-rand 19380110) ; happy birthday donald e knuth
  (set! rand (lambda seed
    (cond ((null? seed) (/ (next-rand) two31))
          ((eq? (car seed) 'get) (cons fptr (vector->list a)))
          ((eq? (car seed) 'set) (set! fptr (caadr seed))
                                 (set! a (list->vector (cdadr seed))))
          (else (/ (init-rand (modulo (numerator
                  (inexact->exact (car seed))) two31)) two31)))))
  (set! randint (lambda args
    (cond ((null? (cdr args))
            (if (< (car args) two31) (unif-rand (car args))
              (floor (* (next-rand) (car args)))))
          ((< (car args) (cadr args))
            (let ((span (- (cadr args) (car args))))
              (+ (car args)
                 (if (< span two31) (unif-rand span)
                   (floor (* (next-rand) span))))))
          (else (let ((span (- (car args) (cadr args))))
                  (- (car args)
                     (if (< span two31) (unif-rand span)
                       (floor (* (next-rand) span))))))))))

(define gap 5)

(define (next n x)
  (let loop ((n (- n 1)) (xs (list (+ x (randint gap) 1))))
    (if (zero? n) (list->vector (reverse xs))
      (loop (- n 1) (cons (+ (car xs) (randint gap) 1) xs)))))

(define (bsearch lt? x xs)
  (let loop ((lo 0) (hi (- (vector-length xs) 1)))
    (let ((mid (quotient (+ lo hi) 2)))
      (cond ((< hi lo) #f)
            ((lt? x (vector-ref xs mid))
              (loop lo (- mid 1)))
            ((lt? (vector-ref xs mid) x)
              (loop (+ mid 1) hi))
            (else mid)))))

(define (search k)
  (let loop ((base 0) (two 2) (ary (next 1 0)))
    (display ary) (newline)
    (let ((x (vector-ref ary (- (vector-length ary) 1))))
      (if (< x k)
          (loop (+ base (/ two 2)) (* two 2) (next two x))
          (let ((idx (bsearch < k ary)))
            (if idx (+ base idx) #f))))))

(display (search 79)) (newline)
(display (search 79)) (newline)
(display (search 79)) (newline)
(display (search 79)) (newline)
(display (search 79)) (newline)
Success #stdin #stdout 0.02s 8952KB
stdin
Standard input is empty
stdout
#(1)
#(3 4)
#(9 12 17 21)
#(26 31 36 40 44 49 54 57)
#(61 66 67 71 72 76 80 85 90 91 95 96 97 101 102 107)
#f
#(2)
#(6 9)
#(14 15 16 20)
#(25 30 34 37 39 44 49 52)
#(57 59 63 66 69 74 75 78 80 82 85 88 89 94 95 98)
#f
#(4)
#(9 12)
#(16 17 21 26)
#(27 28 29 34 37 38 39 43)
#(47 48 51 56 60 65 67 70 72 74 75 76 80 84 86 87)
#f
#(1)
#(5 10)
#(15 17 21 25)
#(26 28 31 35 40 43 48 51)
#(53 55 59 60 65 69 70 72 75 79 81 84 85 87 92 97)
24
#(4)
#(6 10)
#(11 13 16 18)
#(22 25 29 33 36 40 41 46)
#(51 55 59 61 65 69 72 77 81 84 89 90 95 100 104 109)
#f