Jump to content

Arithmetic-geometric mean/Calculate Pi: Difference between revisions

Arithmetic-geometric mean/Calculate Pi in various dialects BASIC (BASIC256, FreeBASIC, True BASIC and Yabasic)
m (Fix typos in TI SR-56 solution)
(Arithmetic-geometric mean/Calculate Pi in various dialects BASIC (BASIC256, FreeBASIC, True BASIC and Yabasic))
Line 18:
 
The purpose of this task is to demonstrate how to use this approximation in order to compute a large number of decimals of <math>\pi</math>.
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic">digits = 500
an = 1.0
bn = sqr(0.5)
tn = 0.5 ^ 2
pn = 1.0
 
while pn <= digits
prevAn = an
an = (bn + an) / 2
bn = sqr(bn * prevAn)
prevAn -= an
tn -= (pn * prevAn ^ 2)
pn *= 2
end while
print ((an + bn) ^ 2) / (tn * 4)</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="vb">Dim As Short digits = 500
Dim As Double an = 1
Dim As Double bn = Sqr(0.5)
Dim As Double tn = 0.5^2
Dim As Double pn = 1
Dim As Double prevAn
 
While pn <= digits
prevAn = an
an = (bn + an) / 2
bn = Sqr(bn * prevAn)
prevAn -= an
tn -= (pn * prevAn^2)
pn *= 2
Wend
Dim As Double pi = ((an + bn)^2) / (tn * 4)
Print pi
 
Sleep</syntaxhighlight>
{{out}}
<pre>3.141592653589794</pre>
 
==={{header|True BASIC}}===
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">LET digits = 500
LET an = 1.0
LET bn = SQR(0.5)
LET tn = 0.5 ^ 2
LET pn = 1.0
 
DO WHILE pn <= digits
LET prevAn = an
LET an = (bn + an) / 2
LET bn = SQR(bn * prevAn)
LET prevAn = prevAn - an
LET tn = tn - (pn * prevAn ^ 2)
LET pn = pn + pn
LOOP
PRINT ((an + bn) ^ 2) / (tn * 4)
END</syntaxhighlight>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="basic">digits = 500
an = 1.0
bn = sqrt(0.5)
tn = 0.5 ^ 2
pn = 1.0
 
while pn <= digits
prevAn = an
an = (bn + an) / 2
bn = sqrt(bn * prevAn)
prevAn = prevAn - an
tn = tn - (pn * prevAn ^ 2)
pn = pn + pn
wend
print ((an + bn) ^ 2) / (tn * 4)</syntaxhighlight>
 
=={{header|C}}==
2,139

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.