Sujet : Re: Totally OT: Colliding blocks that compute pi
De : ldo (at) *nospam* nz.invalid (Lawrence D'Oliveiro)
Groupes : comp.misc sci.electronics.designDate : 21. Mar 2025, 07:16:44
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vrj08c$ugqc$2@dont-email.me>
References : 1 2 3 4
User-Agent : Pan/0.162 (Pokrosvk)
On Thu, 20 Mar 2025 13:15:38 +0000, SH wrote:
How I wish I could calculate pi
Your computer can do it!
import decimal
Dec = decimal.Decimal
decctx = decimal.getcontext()
decctx.prec = 64
def decimal_pi():
with decimal.localcontext() as decctx :
decctx.prec += 2 # extra digits for intermediate steps
t = Dec(3) # substitute 3.0 for regular floats
lasts, s, n, na, d, da = 0, 3, 1, 0, 0, 24
nr_steps = 0
while s != lasts :
nr_steps += 1
lasts = s
n, na = n + na, na + 8
d, da = d + da, da + 32
t = t * n / d
s += t
#end while
#end with
print("nr_steps = %d" % nr_steps)
return +s # unary plus applies the new precision
#end decimal_pi
print(decimal_pi())
output:
nr_steps = 104
3.141592653589793238462643383279502884197169399375105820974944592
Taken from a presentation I did here
<
https://github.com/HamPUG/meetings/blob/master/2022/2022-11-14/ldo/Continued%20Fractions.ipynb>.
I tried continued fractions, but found them a waste of time.