Happy Pi Day!
12-12-2021, 11:04 PM
Post: #11
 Albert Chan Senior Member Posts: 1,799 Joined: Jul 2018
RE: Happy Pi Day!
Hi, DM48

A better PI spigot is with continued fraction:

PI = 4/(1+1/(3+2^2/(5+3^2/(7+4^2/(9+5^2/(11 ...

This keep integers *much* smaller ⇒ code run faster.
see ABC Programmer's Handbook, Pi example (*)

Below Python code produce 7656 pi digits, in 6 sec on my laptop.
To produce same output, your code (adjusted to range(33080)) take 31 sec.
(It take about 2 sec to print digits, speed ratio ≈ 29/4 ≈ 7)

Code:
p, q = 1, 3 a, b, a1, b1 = 4, 1, 12, 4 for i in range(10000):     p, q = p+q, q+2        # next convergent     a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1     while 1:         d = a//b         r = a1 - d*b1      # break if a//b != a1//b1         if r < 0 or r >= b1: break         print d,           # common digits         a, a1 = 10*(a-d*b), 10*r

(*) Python is descendant of ABC, both developed by Guido van Rossum
 « Next Oldest | Next Newest »

 Messages In This Thread Happy Pi Day! - Gerson W. Barbosa - 03-14-2020, 02:08 AM RE: Happy Pi Day! - pinkman - 03-14-2020, 06:19 AM RE: Happy Pi Day! - Gerson W. Barbosa - 03-14-2020, 02:53 PM RE: Happy Pi Day! - rprosperi - 03-14-2020, 05:04 PM RE: Happy Pi Day! - Jim Horn - 03-14-2020, 05:15 PM RE: Happy Pi Day! - Gerson W. Barbosa - 03-14-2020, 12:17 PM RE: Happy Pi Day! - Dave Britten - 03-14-2020, 12:47 PM RE: Happy Pi Day! - BruceH - 03-15-2020, 11:32 AM RE: Happy Pi Day! - Eddie W. Shore - 03-15-2020, 07:29 PM RE: Happy Pi Day! - DM48 - 12-12-2021, 06:44 PM RE: Happy Pi Day! - Albert Chan - 12-12-2021 11:04 PM RE: Happy Pi Day! - Gerson W. Barbosa - 12-13-2021, 12:41 AM RE: Happy Pi Day! - DM48 - 12-13-2021, 01:54 AM RE: Happy Pi Day! - Gil - 12-13-2021, 01:21 AM RE: Happy Pi Day! - Gerson W. Barbosa - 12-13-2021, 03:57 AM

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