fork(1) download
  1. ; self-locating strings in pi
  2.  
  3.  
  4. (define (take n xs)
  5. (let loop ((n n) (xs xs) (ys '()))
  6. (if (or (zero? n) (null? xs))
  7. (reverse ys)
  8. (loop (- n 1) (cdr xs)
  9. (cons (car xs) ys)))))
  10.  
  11. (define (digits n . args)
  12. (let ((b (if (null? args) 10 (car args))))
  13. (let loop ((n n) (d '()))
  14. (if (zero? n) d
  15. (loop (quotient n b)
  16. (cons (modulo n b) d))))))
  17.  
  18. ; https://p...content-available-to-author-only...s.com/2009/02/20/the-digits-of-pi/
  19. (define (pi-spigot z)
  20. (let loop ((z z) (ds '()) (q 1) (r 0) (t 1) (k 1) (n 3) (m 3))
  21. (cond ((zero? z) (reverse ds))
  22. ((< (+ q q q q r (- t)) (* n t))
  23. (loop (- z 1) (cons n ds) (* 10 q) (* 10 (- r (* n t)))
  24. t k (- (quotient (* 10 (+ q q q r)) t) (* 10 n)) m))
  25. (else (loop z ds (* q k) (* (+ q q r) m) (* t m) (+ k 1)
  26. (quotient (+ (* q (+ k k k k k k k 2)) (* r m))
  27. (* t m)) (+ m 2))))))
  28.  
  29. (define pi (pi-spigot 100))
  30.  
  31. (do ((n 0 (+ n 1)) (ps (cdr pi) (cdr ps))) ((null? ps))
  32. (let* ((ds (digits n)) (len (length ds)))
  33. (when (equal? ds (take len ps)) (display n) (newline))))
Success #stdin #stdout 0.04s 51496KB
stdin
Standard input is empty
stdout
0
6
27