This commit is contained in:
mehbark 2025-03-14 10:44:03 -04:00
commit cf374d4361
2 changed files with 28 additions and 0 deletions

7
pi.scm Normal file
View file

@ -0,0 +1,7 @@
(import (pi))
(spigot
(lambda (n)
(write n)
;; feels nicer to flush after every digit (it gets real slow)
;; chez let's us omit the output port but not r6rs sad
(flush-output-port (current-output-port))))

21
pi.sls Normal file
View file

@ -0,0 +1,21 @@
;; https://www.gavalas.dev/blog/spigot-algorithms-for-pi-in-python/
(library (pi)
(export spigot)
(import (rnrs (6)))
(define (spigot yield)
(let loop ([q 1] [r 0] [t 1] [k 1] [n 3] [l 3])
(if (< (- (+ (* 4 q) r) t) (* n t))
(begin
(yield n)
(loop (* 10 q)
(* 10 (- r (* n t)))
t
k
(- (div (* 10 (+ (* 3 q) r)) t) (* 10 n))
l))
(loop (* q k)
(* (+ (* 2 q) r) l)
(* t l)
(+ k 1)
(div (+ (* q (+ (* 7 k) 2)) (* r l)) (* t l))
(+ l 2))))))