Post Reply 
Pi digits (again) - an unbounded spigot program
05-18-2024, 03:35 PM
Post: #19
RE: Pi digits (again) - an unbounded spigot program
(05-18-2024 03:19 PM)Guenter Schink Wrote:  How fast is PRIME?
(…)
time is between 5 and 6.5 seconds with several runs

Not sure if it helps, but I remembered Writing fast and efficient MicroPython and made the functions local to main:
Code:
def main(n, k):

    def stream(u, lfts):
        for x in lfts:
            v = cons(u, x)
            y = next(v)
            yield y
            u = prod(v, y)

    def next(z):
        [[q, r, s, t], i] = z
        x = 27 * i + 15
        return (q * x + 5 * r) // (s * x + 5 * t)

    def prod(s, n):
        z, i = s
        return comp((10, -10 * n, 0, 1), z), i

    def cons(s, x):
        z, i = s
        return comp(z, x), i + 1

    def comp(a, b):
        q, r, s, t = a
        u, v, w, x = b
        return q * u + r * w, q * v + r * x, s * u + t * w, s * v + t * x

    init = ((1, 0, 0, 1), 1)
    lfts = (
        (i * (2 * i - 1), j * (5 * i - 2), 0, j)
        for i, j in ((i, 3 * (3 * i + 1) * (3 * i + 2)) for i in range(1, n))
    )
    pi = stream(init, lfts)
    i = 0
    for d in pi:
        print(d, end="")
        if i % k == 0:
            print()
        i += 1


if __name__ == "__main__":
    n, k = 1002, 100
    main(n, k)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Pi digits (again) - an unbounded spigot program - Thomas Klemm - 05-18-2024 03:35 PM



User(s) browsing this thread: 1 Guest(s)