pi/pi.sls
2025-03-14 10:44:03 -04:00

22 lines
667 B
Scheme

;; 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))))))