Arithmetic-geometric mean/Calculate Pi: Difference between revisions

m (Added reference to "bignum" library.)
(43 intermediate revisions by 22 users not shown)
Line 1:
[[Category:Geometry]]
{{task}}
[http://www.maa.org/sites/default/files/pdf/upload_library/22/Ford/Almkvist-Berndt585-608.pdf Almkvist Berndt 1988] begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate <math>\pi</math>.
Line 17 ⟶ 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|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "PI.bas"
110 LET DIGITS=10
120 LET AN,PN=1
130 LET BN=SQR(.5)
140 LET TN=.5^2
150 DO WHILE PN<=DIGITS
160 LET PREVAN=AN
170 LET AN=(BN+AN)/2
180 LET BN=SQR(BN*PREVAN)
190 LET PREVAN=PREVAN-AN
200 LET TN=TN-(PN*PREVAN^2)
210 LET PN=PN+PN
220 LOOP
230 PRINT (AN+BN)^2/(TN*4)</syntaxhighlight>
 
==={{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}}==
Line 22 ⟶ 116:
<!-- Example by Nigel Galloway, Feb 8, 2012 -->
{{libheader|GMP}}
<langsyntaxhighlight lang="c">#include "gmp.h"
 
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
Line 63 ⟶ 157:
gmp_printf ("%.100000Ff\n", x0);
return 0;
}</langsyntaxhighlight>
i<7 produces:
<pre style="height:64ex;white-space: pre-wrap;">
Line 74 ⟶ 168:
 
=={{header|C sharp|C#}}==
{{libheader|System.Numerics}}Can specify the number of desired digits on the command line, default is 25000, which takes a few seconds (depending on your system's performance).<langsyntaxhighlight lang="csharp">using System;
using System.Numerics;
 
Line 131 ⟶ 225:
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
}
}</langsyntaxhighlight>
{{out}}
<pre style="height:64ex;white-space: pre-wrap;">
Line 140 ⟶ 234:
{{trans|C}}
{{libheader|GMP}}
<langsyntaxhighlight lang="cpp">#include <gmpxx.h>
#include <chrono>
 
Line 182 ⟶ 276:
printf("Took %f ms for output.\n", duration<double>(steady_clock::now() - st).count() * 1000.0);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre style="height:64ex;white-space: pre-wrap;">Took 60.726400 ms for computation.
Line 190 ⟶ 284:
=={{header|Clojure}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="lisp">(ns async-example.core
(:use [criterium.core])
(:gen-class))
Line 217 ⟶ 311:
(doseq [q (partition-all 200 (str (compute-pi 18)))]
(println (apply str q)))
</syntaxhighlight>
</lang>
{{Output}}
<pre style="height:64ex;overflow:scroll">
Line 266 ⟶ 360:
{{libheader|MMA}}
<p>This is an example that uses the Common Lisp Bigfloat Package (http://www.cs.berkeley.edu/~fateman/lisp/mma4max/more/bf.lisp)</p>
<langsyntaxhighlight lang="lisp">(load "bf.fasl")
 
;;(setf mma::bigfloat-bin-prec 1000)
Line 283 ⟶ 377:
(setf A X1)
(setf G X2)))
(mma:bigfloat-/ (mma:bigfloat-* A A) Z))</langsyntaxhighlight>
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141274</pre>
Line 289 ⟶ 383:
=={{header|D}}==
{{trans|C#}}
<langsyntaxhighlight lang="d">import std.bigint;
import std.conv;
import std.math;
Line 362 ⟶ 456:
string piStr = to!string(pi);
writeln(piStr[0], '.', piStr[1..$]);
}</langsyntaxhighlight>
{{out}}
<pre style="height:64ex;white-space: pre-wrap;">3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151557485724245415069595082953311686172785588907509838175463746493931925506040092770167113900984882401285836160356370766010471018194295559619894676783744944825537977472684710404753464620804668425906949129331367702898915210475216205696602405803815019351125338243003558764024749647326391419927260426992279678235478163600934172164121992458631503028618297455570674983850549458858692699569092721079750930295532116534498720275596023648066549911988183479775356636980742654252786255181841757467289097777279380008164706001614524919217321721477235014144197356854816136115735255213347574184946843852332390739414333454776241686251898356948556209921922218427255025425688767179049460165346680498862723279178608578438382796797668145410095388378636095068006422512520511739298489608412848862694560424196528502221066118630674427862203919494504712371378696095636437191728746776465757396241389086583264599581339047802759009946576407895126946839835259570982582262052248940772671947826848260147699090264013639443745530506820349625245174939965143142980919065925093722169646151570985838741059788595977297549893016175392846813826868386894277415599185592524595395943104997252468084598727364469584865383673622262609912460805124388439045124413654976278079771569143599770012961608944169486855584840635342207222582848864815845602850601684273945226746767889525213852254995466672782398645659611635488623057745649803559363456817432411251507606947945109659609402522887971089314566913686722874894056010150330861792868092087476091782493858900971490967598526136554978189312978482168299894872265880485756401427047755513237964145152374623436454285844479526586782105114135473573952311342716610213596953623144295248493718711014576540359027993440374200731057853906219838744780847848968332144571386875194350643021845319104848100537061468067491927819119793995206141966342875444064374512371819217999839101591956181467514269123974894090718649423196156794520809514655022523160388193014209376213785595663893778708303906979207734672218256259966150142150306803844773454920260541466592520149744285073251866600213243408819071048633173464965145390579626856100550810665879699816357473638405257145910289706414011097120628043903975951567715770042033786993600723055876317635942187312514712053292819182618612586732157919841484882916447060957527069572209175671167229109816909152801735067127485832228718352093539657251210835791513698820914442100675103346711031412671113699086585163983150197016515116851714376576183515565088490998985998238734552833163550764791853589322618548963213293308985706420467525907091548141654985946163718027098199430992448895757128289059232332609729971208443357326548938239119325974636673058360414281388303203824903758985243744170291327656180937734440307074692112019130203303801976211011004492932151608424448596376698389522868478312355265821314495768572624334418930396864262434107732269780280731891544110104468232527162010526522721116603966655730925471105578537634668206531098965269186205647693125705863566201855810072936065987648611791045334885034611365768675324944166803962657978771855608455296541266540853061434443185867697514566140680070023787765913440171274947042056223053899456131407112700040785473326993908145466464588079727082668306343285878569830523580893306575740679545716377525420211495576158140025012622859413021647155097925923099079654737612551765675135751782966645477917450112996148903046399471329621073404375189573596145890193897131117904297828564750320319869151402870808599048010941214722131794764777262241425485454033215718530614228813758504306332175182979866223717215916077166925474873898665494945011465406284336639379003976926567214638530673609657120918076383271664162748888007869256029022847210403172118608204190004229661711963779213375751149595015660496318629472654736425230817703675159067350235072835405670403867435136222247715891504953098444893330963408780769325993978054193414473774418426312986080998886874132604721569516239658645730216315981931951673538129741677294786724229246543668009806769282382806899640048243540370141631496589794092432378969070697794223625082216889573837986230015937764716512289357860158816175578297352334460428151262720373431465319777741603199066554187639792933441952154134189948544473456738316249934191318148092777710386387734317720754565453220777092120190516609628049092636019759882816133231666365286193266863360627356763035447762803504507772355471058595487027908143562401451718062464362679456127531813407833033625423278394497538243720583531147711992606381334677687969597030983391307710987040859133746414428227726346594704745878477872019277152807317679077071572134447306057007334924369311383504931631284042512192565179806941135280131470130478164378851852909285452011658393419656213491434159562586586557055269049652098580338507224264829397285847831630577775606888764462482468579260395352773480304802900587607582510474709164396136267604492562742042083208566119062545433721315359584506877246029016187667952406163425225771954291629919306455377991403734043287526288896399587947572917464263574552540790914513571113694109119393251910760208252026187985318877058429725916778131496990090192116971737278476847268608490033770242429165130050051683233643503895170298939223345172201381280696501178440874519601212285993716231301711444846409038906449544400619869075485160263275052983491874078668088183385102283345085048608250393021332197155184306354550076682829493041377655279397517546139539846833936383047461199665385815384205685338621867252334028308711232827892125077126294632295639898989358211674562701021835646220134967151881909730381198004973407239610368540664319395097901906996395524530054505806855019567302292191393391856803449039820595510022635353619204199474553859381023439554495977837790237421617271117236434354394782218185286240851400666044332588856986705431547069657474585503323233421073015459405165537906866273337995851156257843229882737231989875714159578111963583300594087306812160287649628674460477464915995054973742562690104903778198683593814657412680492564879855614537234786733039046883834363465537949864192705638729317487233208376011230299113679386270894387993620162951541337142489283072201269014754668476535761647737946752004907571555278196536213239264061601363581559074220202031872776052772190055614842555187925303435139844253223415762336106425063904975008656271095359194658975141310348227693062474353632569160781547818115284366795706110861533150445212747392454494542368288606134084148637767009612071512491404302725386076482363414334623518975766452164137679690314950191085759844239198629164219399490723623464684411739403265918404437805133389452574239950829659122850855582157250310712570126683024029295252201187267675622041542051618416348475651699981161410100299607838690929160302884002691041407928862150784245167090870006992821206604183718065355672525325675328612910424877618258297651579598470356222629348600341587229805349896502262917487882027342092222453398562647669149055628425039127577102840279980663658254889264880254566101729670266407655904290994568150652653053718294127033693137851786090407086671149655834343476933857817113864558736781230145876871266034891390956200993936103102916161528813843790990423174733639480457593149314052976347574811935670911013775172100803155902485309066920376719220332290943346768514221447737939375170344366199104033751117354719185504644902636551281622882446257591633303910722538374218214088350865739177150968288747826569959957449066175834413752239709683408005355984917541738188399944697486762655165827658483588453142775687900290951702835297163445621296404352311760066510124120065975585127617858382920419748442360800719304576189323492292796501987518721272675079812554709589045563579212210333466974992356302549478024901141952123828153091140790738602515227429958180724716259166854513331239480494707911915326734302824418604142636395480004480026704962482017928964766975831832713142517029692348896276684403232609275249603579964692565049368183609003238092934595889706953653494060340216654437558900456328822505452556405644824651518754711962184439658253375438856909411303150952617937800297412076651479394259029896959469955657612186561967337862362561252163208628692221032748892186543648022967807057656151446320469279068212073883778142335628236089632080682224680122482611771858963814091839036736722208883215137556003727983940041529700287830766709444745601345564172543709069793961225714298946715435784687886144458123145935719849225284716050492212424701412147805734551050080190869960330276347870810817545011930714122339086639383395294257869050764310063835198343893415961318543475464955697810382930971646514384070070736041123735998434522516105070270562352660127648483084076118301305279320542746286540360367453286510570658748822569815793678976697422057505968344086973502014102067235850200724522563265134105592401902742162484391403599895353945909440704691209140938700126456001623742880210927645793106579229552498872758461012648369998922569596881592056001016552563756785667227966198857827948488558343975187445455129656344348039664205579829368043522027709842942325330225763418070394769941597915945300697521482933665556615678736400536665641654732170439035213295435291694145990416087532018683793702348886894791510716378529023452924407736594956305100742108714261349745956151384987137570471017879573104229690666702144986374645952808243694457897723300487647652413390759204340196340391147320233807150952220106825634274716460243354400515212669324934196739770415956837535551667302739007497297363549645332888698440611964961627734495182736955882207573551766515898551909866653935494810688732068599075407923424023009259007017319603622547564789406475483466477604114632339056513433068449539790709030234604614709616968868850140834704054607429586991382966824681857103188790652870366508324319744047718556789348230894310682870272280973624809399627060747264553992539944280811373694338872940630792615959954626246297070625948455690347119729964090894180595343932512362355081349490043642785271383159125689892951964272875739469142725343669415323610045373048819855170659412173524625895487301676002988659257866285612496655235338294287854253404830833070165372285635591525347844598183134112900199920598135220511733658564078264849427644113763938669248031183644536985891754426473998822846218449008777697763127957226726555625962825427653183001340709223343657791601280931794017185985999338492354956400570995585611349802524990669842330173503580440811685526531170995708994273287092584878944364600504108922669178352587078595129834417295351953788553457374260859029081765155780390594640873506123226112009373108048548526357228257682034160504846627750450031262008007998049254853469414697751649327095049346393824322271885159740547021482897111777923761225788734771881968254629812686858170507402725502633290449762778944236216741191862694396506715157795867564823993917604260176338704549901761436412046921823707648878341968968611815581587360629386038101712158552726683008238340465647588040513808016336388742163714064354955618689641122821407533026551004241048967835285882902436709048871181909094945331442182876618103100735477054981596807720094746961343609286148494178501718077930681085469000944589952794243981392135055864221964834915126390128038320010977386806628779239718014613432445726400973742570073592100315415089367930081699805365202760072774967458400283624053460372634165542590276018348403068113818551059797056640075094260878857357960373245141467867036880988060971642584975951380693094494015154222219432913021739125383559150310033303251117491569691745027149433151558854039221640972291011290355218157628232831823425483261119128009282525619020526301639114772473314857391077758744253876117465786711694147764214411112635835538713610110232679877564102468240322648346417663698066378576813492045302240819727856471983963087815432211669122464159117767322532643356861461865452226812688726844596844241610785401676814208088502800541436131462308210259417375623899420757136275167457318918945628352570441335437585753426986994725470316566139919996826282472706413362221789239031760854289437339356188916512504244040089527198378738648058472689546243882343751788520143956005710481194988423906061369573423155907967034614914344788636041031823507365027785908975782727313050488939890099239135033732508559826558670892426124294736701939077271307068691709264625484232407485503660801360466895118400936686095463250021458529309500009071510582362672932645373821049387249966993394246855164832611341461106802674466373343753407642940266829738652209357016263846485285149036293201991996882851718395366913452224447080459239660281715655156566611135982311225062890585491450971575539002439315351909021071194573002438801766150352708626025378817975194780610137150044899172100222013350131060163915415895780371177927752259787428919179155224171895853616805947412341933984202187456492564434623925319531351033114763949119950728584306583619353693296992898379149419394060857248639688369032655643642166442576079147108699843157337496488352927693282207629472823815374099615455987982598910937171262182830258481123890119682214294576675807186538065064870261338928229949725745303328389638184394477077940228435988341003583854238973542439564755568409522484455413923941000162076936368467764130178196593799715574685419463348937484391297423914336593604100352343777065888677811394986164787471407932638587386247328896456435987746676384794665040741118256583788784548581489629612739984134427260860618724554523606431537101127468097787044640947582803487697589483282412392929605829486191966709189580898332012103184303401284951162035342801441276172858302435598300320420245120728725355811958401491809692533950757784000674655260314461670508276827722235341911026341631571474061238504258459884199076112872580591139356896014316682831763235673254170734208173322304629879928049085140947903688786878949305469557030726190095020764334933591060245450864536289354568629585313153371838682656178622736371697577418302398600659148161640494496501173213138957470620884748023653710311508984279927544268532779743113951435741722197597993596852522857452637962896126915723579866205734083757668738842664059909935050008133754324546359675048442352848747014435454195762584735642161981340734685411176688311865448937769795665172796623267148103386439137518659467300244345005449953997423723287124948347060440634716063258306498297955101095418362350303094530973358344628394763047756450150085075789495489313939448992161255255977014368589435858775263796255970816776438001254365023714127834679261019955852247172201777237004178084194239487254068015560359983905489857235467456423905858502167190313952629445543913166313453089390620467843877850542393905247313620129476918749751910114723152893267725339181466073000890277689631148109022097245207591672970078505807171863810549679731001678708506942070922329080703832634534520380278609905569001341371823683709919495164896007550493412678764367463849020639640197666855923356546391383631857456981471962108410809618846054560390384553437291414465134749407848844237721751543342603066988317683310011331086904219390310801437843341513709243530136776310849135161564226984750743032971674696406665315270353254671126675224605511995818319637637076179919192035795820075956053023462677579439363074630569010801149427141009391369138107258137813578940055995001835425118417213605572752210352680373572652792241737360575112788721819084490061780138897107708229310027976659358387589093956881485602632243937265624727760378908144588378550197028437793624078250527048758164703245812908783952324532378960298416692254896497156069811921865849267704039564812781021799132174163058105545988013004845629976511212415363745150056350701278159267142413421033015661653560247338078430286552572227530499988370153487930080626018096238151613669033411113865385109193673938352293458883225508870645075394739520439680790670868064450969865488016828743437861264538158342807530618454859037982179945996811544197425363443996029025100158882721647450068207041937615845471231834600726293395505482395571372568402322682130124767945226448209102356477527230820810635188991526928891084555711266039650343978962782500161101532351605196559042118449499077899920073294769058685778787209829013529566139788848605097860859570177312981553149516814671769597609942100361835591387778176984587581044662839988060061622984861693533738657877359833616133841338536842119789389001852956919678045544828584837011709672125353387586215823101331038776682721157269495181795897546939926421979155233857662316762754757035469941489290413018638611943919628388705436777432242768091323654494853667680000010652624854730558615989991401707698385483188750142938908995068545307651168033373222651756622075269517914422528081651716677667279303548515420402381746089232839170327542575086765511785939500279338959205766827896776445318404041855401043513483895312013263783692835808271937831265496174599705674507183320650345566440344904536275600112501843356073612227659492783937064784264567633881880756561216896050416113903906396016202215368494109260538768871483798955999911209916464644119185682770045742434340216722764455893301277815868695250694993646101756850601671453543158148010545886056455013320375864548584032402987170934809105562116715468484778039447569798042631809917564228098739987669732376957370158080682290459921236616890259627304306793165311494017647376938735140933618332161428021497633991898354848756252987524238730775595559554651963944018218409984124898262367377146722606163364329640633572810707887581640438148501884114318859882769449011932129682715888413386943468285900666408063140777577257056307294004929403024204984165654797367054855804458657202276378404668233798528271057843197535417950113472736257740802134768260450228515797957976474670228409995616015691089038458245026792659420555039587922981852648007068376504183656209455543461351341525700659748819163413595567196496540321872716026485930490397874895890661272507948282769389535217536218507962977851461884327192232238101587444505286652380225328438913752738458923844225354726530981715784478342158223270206902872323300538621634798850946954720047952311201504329322662827276321779088400878614802214753765781058197022263097174950721272484794781695729614236585957820908307332335603484653187302930266596450137183754288975579714499246540386817992138934692447419850973346267933210726868707680626399193619650440995421676278409146698569257150743157407938053239252394775574415918458215625181921552337096074833292349210345146264374498055961033079941453477845746999921285999993996122816152193148887693880222810830019860165494165426169685867883726095877456761825072759929508931805218729246108676399589161458550583972742098090978172932393010676638682404011130402470073508578287246271349463685318154696904669686939254725194139929146524238577625500474852954768147954670070503479995888676950161249722820403039954632788306959762493615101024365553522306906129493885990157346610237122354789112925476961760050479749280607212680392269110277722610254414922157650450812067717357120271802429681062037765788371669091094180744878140490755178203856539099104775941413215432844062503018027571696508209642734841469572639788425600845312140659358090412711359200419759851362547961606322887361813673732445060792441176399759746193835845749159880976674470930065463424234606342374746660804317012600520559284936959414340814685298150539471789004518357551541252235905906872648786357525419112888773717663748602766063496035367947026923229718683277173932361920077745221262475186983349515101986426988784717193966497690708252174233656627259284406204302141137199227852699846988477023238238400556555178890876613601304770984386116870523105531491625172837327286760072481729876375698163354150746088386636406934704372066886512756882661497307886570156850169186474885416791545965072342877306998537139043002665307839877638503238182155355973235306860430106757608389086270498418885951380910304235957824951439885901131858358406674723702971497850841458530857813391562707603563907639473114554958322669457024941398316343323789759556808568362972538679132750555425244919435891284050452269538121791319145135009938463117740179715122837854601160359554028644059024964669307077690554810288502080858008781157738171917417760173307385547580060560143377432990127286772530431825197579167929699650414607066457125888346979796429316229655201687973000356463045793088403274807718115553309098870255052076804630346086581653948769519600440848206596737947316808641564565053004988161649057883115434548505266006982309315777650037807046612647060214575057932709620478256152471459189652236083966456241051955105223572397395128818164059785914279148165426328920042816091369377737222999833270820829699557377273756676155271139225880552018988762011416800546873655806334716037342917039079863965229613128017826797172898229360702880690877686605932527463784053976918480820410219447197138692560841624511239806201131845412447820501107987607171556831540788654390412108730324020106853419472304766667217498698685470767812051247367924791931508564447753798537997322344561227858432968466475133365736923872014647236794278700425032555899268843495928761240075587569464137056251400117971331662071537154360068764773186755871487839890810742953094106059694431584775397009439883949144323536685392099468796450665339857388878661476294434140104988899316005120767810358861166020296119363968213496075011164983278563531614516845769568710900299976984126326650234771672865737857908574664607722834154031144152941880478254387617707904300015669867767957609099669360755949651527363498118964130433116627747123388174060373174397054067031096767657486953587896700319258662594105105335843846560233917967492678447637084749783336555790073841914731988627135259546251816043422537299628632674968240580602964211463864368642247248872834341704415734824818333016405669596688667695634914163284264149745333499994800026699875888159350735781519588990053951208535103572613736403436753471410483601754648830040784641674521673719048310967671134434948192626811107399482506073949507350316901973185211955263563258433909982249862406703107683184466072912487475403161796994113973877658998685541703188477886759290260700432126661791922352093822787888098863359911608192353555704646349113208591897961327913197564909760001399623444553501434642686046449586247690943470482932941404111465409239883444351591332010773944111840741076849810663472410482393582740194493566516108846312567852977697346843030614624180358529331597345830384554103370109167677637427621021370135485445092630719011473184857492331816720721372793556795284439254815609137281284063330393735624200160456645574145881660521666087387480472433912129558777639069690370788285277538940524607584962315743691711317613478388271941686066257210368513215664780014767523103935786068961112599602818393095487090590738613519145918195102973278755710497290114871718971800469616977700179139196137914171627070189584692143436967629274591099400600849835684252019155937037010110497473394938778859894174330317853487076032219829705797511914405109942358830345463534923498268836240433272674155403016195056806541809394099820206099941402168909007082133072308966211977553066591881411915778362729274615618571037217247100952142369648308641025928874579993223749551912219519034244523075351338068568073544649951272031744871954039761073080602699062580760202927314552520780799141842906388443734996814582733720726639176702011830046481900024130835088465841521489912761065137415394356572113903285749187690944137020905170314877734616528798482353382972601361109845148418238081205409961252745808810994869722161285248974255555160763716750548961730168096138038119143611439921063800508321409876045993093248510251682944672606661381517457125597549535802399831469822036133808284993567055755247129027453977621404931820146580080215665360677655087838043041343105918046068008345911366408348874080057412725867047922583191274157390809143831384564241509408491339180968402511639919368532255573389669537490266209232613188558915808324555719484538756287861288590041060060737465014026278240273469625282171749415823317492396835301361786536737606421667781377399510065895288</pre>
Line 372 ⟶ 466:
Thanks for Velthuis BigIntegers library[https://github.com/rvelthuis/DelphiBigNumbers].
{{Trans|C#}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Calculate_Pi;
 
Line 482 ⟶ 576:
readln;
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 488 ⟶ 582:
3.141592653589793238...81377399510065895288
</pre>
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang=easylang>
an = 1
bn = sqrt 0.5
tn = 0.25
pn = 1
while pn <= 5
prevAn = an
an = (bn + an) / 2
bn = sqrt (bn * prevAn)
prevAn -= an
tn -= (pn * prevAn * prevAn)
pn *= 2
.
mypi = (an + bn) * (an + bn) / (tn * 4)
numfmt 15 0
print mypi
</syntaxhighlight>
 
=={{header|Erlang}}==
{{trans|python}}
<langsyntaxhighlight lang="erlang">
-module(pi).
-export([agmPi/1, agmPiBody/5]).
Line 512 ⟶ 626:
Step_difference = A_n_plus_one - A,
agmPiBody(Loops-1, Running_divisor-(math:pow(Step_difference, 2)*J), A_n_plus_one, B_n_plus_one, J+J).
</syntaxhighlight>
</lang>
{{out}}
<pre>
3.141592653589794
</pre>
 
=={{header|Fortran}}==
{{works with|Fortran|2008 and later}}
{{libheader|iso_fortran_env}}
{{trans|Julia}}
<syntaxhighlight lang="fortran">program CalcPi
! Use real128 numbers: (append '_rf')
use iso_fortran_env, only: rf => real128
implicit none
real(rf) :: a,g,s,old_pi,new_pi
real(rf) :: a1,g1,s1
integer :: k,k1,i
 
old_pi = 0.0_rf;
a = 1.0_rf; g = 1.0_rf/sqrt(2.0_rf); s = 0.0_rf; k = 0
 
do i=1,100
call approx_pi_step(a,g,s,k,a1,g1,s1,k1)
new_pi = 4.0_rf * (a1**2.0_rf) / (1.0_rf - s1)
if (abs(new_pi - old_pi).lt.(2.0_rf*epsilon(new_pi))) then
! If the difference between the newly and previously
! calculated pi is negligible, stop the calculations
exit
end if
write(*,*) 'Iteration:',k1,' Diff:',abs(new_pi - old_pi),' Pi:',new_pi
old_pi = new_pi
a = a1; g = g1; s = s1; k = k1
end do
 
contains
 
subroutine approx_pi_step(x,y,z,n,a,g,s,k)
real(rf), intent(in) :: x,y,z
integer, intent(in) :: n
real(rf), intent(out) :: a,g,s
integer, intent(out) :: k
 
a = 0.5_rf*(x+y)
g = sqrt(x*y)
k = n + 1
s = z + (2.0_rf)**(real(k)+1.0_rf) * (a**(2.0_rf) - g**(2.0_rf))
end subroutine
end program CalcPi
</syntaxhighlight>
{{out}}
<pre>
Iteration: 1 Diff: 3.18767264271210862720192997052536885 Pi: 3.18767264271210862720192997052536885
Iteration: 2 Diff: 4.59923494144553332838595459653619370E-0002 Pi: 3.14168029329765329391807042456000691
Iteration: 3 Diff: 8.76394022067979151556657419559658324E-0005 Pi: 3.14159265389544649600291475881805095
Iteration: 4 Diff: 3.05653257536554156111405386493810661E-0010 Pi: 3.14159265358979323846636060270664556
Iteration: 5 Diff: 3.71721942712928151094186846648146566E-0021 Pi: 3.14159265358979323846264338327951628
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
 
double a, c, g, t, p
double apprpi
short i
 
// Initial values
a = 1
g = sqr(0.5)
p = 1
t = 0.25
 
//Iterate just 3 times
for i = 1 to 3
c = a
a = ( a + g ) / 2
g = sqr( c * g )
c -= a
t -= ( p * c^2 )
p *= 2
apprpi = (( a + g )^2) / ( t * 4 )
print "Iteration "i": ", apprpi
next
 
print "Actual value:",pi
 
handleevents
 
</syntaxhighlight>
{{output}}
<pre>
Iteration 1: 3.140579250522169
Iteration 2: 3.141592646213543
Iteration 3: 3.141592653589794
Actual value: 3.141592653589793
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 557 ⟶ 761:
pi := t.Quo(t, u.Sub(one, sum))
fmt.Println(pi)
}</langsyntaxhighlight>
 
{{out}}
Line 566 ⟶ 770:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">import java.math.MathContext
 
class CalculatePi {
Line 599 ⟶ 803:
System.out.println(pi)
}
}</langsyntaxhighlight>
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201065485862824</pre>
Line 606 ⟶ 810:
{{libheader|MPFR}}
{{libheader|hmpfr}}
<langsyntaxhighlight Haskelllang="haskell">import Prelude hiding (pi)
import Data.Number.MPFR hiding (sqrt, pi, div)
import Data.Number.MPFR.Instances.Near ()
Line 638 ⟶ 842:
main = do
-- The last decimal is rounded.
putStrLn $ toString 1000 $ pi 1000</langsyntaxhighlight>
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420199</pre>
Line 644 ⟶ 848:
=={{header|J}}==
Relevant J essays:
[httphttps://wwwcode.jsoftware.com/jwikiwiki/Essays/Extended%20Precision%20Functions| Extended precision functions] and [httphttps://wwwcode.jsoftware.com/jwikiwiki/Essays/Chudnovsky%20Algorithm|Chudnovsky_Algorithm Pi].
Translated from python:
<langsyntaxhighlight Jlang="j">DP=: 100
 
round=: DP&$: : (4 : 0)
Line 667 ⟶ 871:
'A G' =. X
PI =: A * A % Z
smoutputecho (0j100":PI) , 4 ": I
end.
PI
)</langsyntaxhighlight>
In this run the result is a rational approximation to pi. Only part of the numerator shows. The algorithm produces 100 decimal digits by the eighth iteration.
<pre> pi''
Line 692 ⟶ 896:
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170680 17
1583455951826865080542496790424338362837978447536228171662934224565463064033895909488933268392567279887495006936541219489670405121434573776487989539520749180843985094860051126840117004097133550161882511486508109869673199973040182062140382647367514024790194...</pre>
 
That said, note that J offers a more direct approach here:<syntaxhighlight lang="j"> 102j100":<.@o.&.(*&(10^100x))1
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679</syntaxhighlight>
 
However, a limitation of this approach is that the floor function -- which we use here to signal the desire for an exact approximation of pi -- does not round. But we can ask for extra digits, for example:<syntaxhighlight lang="j"> 113j111":<.@o.&.(*&(10^111x))1
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513</syntaxhighlight>
 
(Another issue is that we must be careful in formatting the number to see those extra digits -- we're generating a rational number which is by default not displayed as a decimal fraction. That's what the <code>102j100":</code> bit was about -- in that example we wanted to represent the number as a decimal fraction using 102 character positions with 100 digits after the decimal point.)
 
=={{header|Java}}==
{{trans|Kotlin}}
Used features of Java 8
<langsyntaxhighlight Javalang="java">import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Objects;
Line 731 ⟶ 943:
System.out.println(pi);
}
}</langsyntaxhighlight>
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201065485862824</pre>
 
{{works with|Julia|1.2}}
 
=={{header|jq}}==
'''Works with gojq, the Go implementation of jq'''
 
This entry presupposes a rational arithmetic module with a fast
`rsqrt` function
for taking square roots of rationals; such a module can be found at [[Arithmetic/Rational]].
<syntaxhighlight lang="jq"># include "rational"; # a reminder
 
def pi(precision):
(precision | (. + log) | ceil) as $digits
| def sq: . as $in | rmult($in; $in) | rround($digits);
{an: r(1;1),
bn: (r(1;2) | rsqrt($digits)),
tn: r(1;4),
pn: 1 }
| until (.pn > $digits;
.an as $prevAn
| .an = (rmult(radd(.bn; .an); r(1;2)) | rround($digits) )
| .bn = ([.bn, $prevAn] | rmult | rsqrt($digits) )
| .tn = rminus(.tn; rmult(rminus($prevAn; .an)|sq; .pn))
| .pn *= 2
)
| rdiv( radd(.an; .bn)|sq; rmult(.tn; 4))
| r_to_decimal(precision);
 
pi(500)</syntaxhighlight>
{{out}}
<pre>
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912
</pre>
 
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Printf
 
agm1step(x, y) = (x + y) / 2, sqrt(x * y)
Line 768 ⟶ 1,012:
 
testmakepi()
</langsyntaxhighlight>{{out}}
<pre>
Approximating π using 512-bit floats.
Line 787 ⟶ 1,031:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">import java.math.BigDecimal
import java.math.MathContext
 
Line 819 ⟶ 1,063:
val pi = (bigFour * a * a).divide(BigDecimal.ONE - sum, con1024)
println(pi)
}</langsyntaxhighlight>
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201065485862824</pre>
 
=={{header|MathematicaMaple}}==
 
<lang mathematica>pi[n_, prec_] :=
With 16 steps we have 89415 correct digits:
 
<syntaxhighlight lang="maple">agm:=proc(n)
local a:=1,g:=evalf(sqrt(1/2)),s:=0,p:=4,i;
for i to n do
a,g:=(a+g)/2,sqrt(a*g);
s+=p*(a*a-g*g);
p+=p
od;
4*a*a/(1-s)
end:
 
Digits:=100000:
d:=agm(16)-evalf(Pi):
evalf[10](d);
# 4.280696926e-89415</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">pi[n_, prec_] :=
Module[{a = 1, g = N[1/Sqrt[2], prec], k, s = 0, p = 4},
For[k = 1, k < n, k++,
Line 835 ⟶ 1,098:
 
pi[7, 100]
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628046852228654</langsyntaxhighlight>
 
=={{header|MATLAB}}==
{{trans|Julia}}
<syntaxhighlight lang="MATLAB">
 
clear all;close all;clc;
testMakePi();
 
 
function [a, g] = agm1step(x, y)
a = (x + y) / 2;
g = sqrt(x * y);
end
 
function [a, g, s, k] = approxPiStep(x, y, z, n)
[a, g] = agm1step(x, y);
k = n + 1;
s = z + 2^(k + 1) * (a^2 - g^2);
end
 
function pi_approx = approxPi(a, g, s)
pi_approx = 4 * a^2 / (1 - s);
end
 
function testMakePi()
digits(512); % Set the precision for variable-precision arithmetic
a = vpa(1.0);
g = 1 / sqrt(vpa(2.0));
s = vpa(0.0);
k = 0;
oldPi = vpa(0.0);
% Define a small value as a threshold for convergence
convergence_threshold = vpa(10)^(-digits);
 
fprintf(' k Error Result\n');
for i = 1:100
[a, g, s, k] = approxPiStep(a, g, s, k);
estPi = approxPi(a, g, s);
if abs(estPi - oldPi) < convergence_threshold
break;
end
oldPi = estPi;
err = abs(vpa(pi) - estPi);
fprintf('%4d%10.1e', i, double(err));
fprintf('%70.60f\n', double(estPi));
end
end
</syntaxhighlight>
{{out}}
<pre>
k Error Result
1 4.6e-02 3.187672642712108483920019352808594703674316406250000000000000
2 8.8e-05 3.141680293297653303596916884998790919780731201171875000000000
3 3.1e-10 3.141592653895446396461466065375134348869323730468750000000000
4 3.7e-21 3.141592653589793115997963468544185161590576171875000000000000
5 5.5e-43 3.141592653589793115997963468544185161590576171875000000000000
6 1.2e-86 3.141592653589793115997963468544185161590576171875000000000000
7 5.8e-174 3.141592653589793115997963468544185161590576171875000000000000
8 0.0e+00 3.141592653589793115997963468544185161590576171875000000000000
9 0.0e+00 3.141592653589793115997963468544185161590576171875000000000000
</pre>
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">3 П0 1 П1 П4 2 КвКор 1/x П2 1
{{Output?}}
<lang>3 П0 1 П1 П4 2 КвКор 1/x П2 1
^ 4 / П3 ИП3 ИП1 ИП2 + 2 /
П5 ИП1 - x^2 ИП4 * - П3 ИП1 ИП2
* КвКор П2 ИП5 П1 КИП4 L0 14 ИП1 x^2
ИП3 / С/П</langsyntaxhighlight>
 
{{out}}
<pre>3.1415927</pre>
 
=={{header|Nim}}==
{{libheader|bignum}}
{{Trans|Delphi}}
<lang Nim>from math import sqrt
<syntaxhighlight lang="nim">from math import sqrt
import times
 
Line 937 ⟶ 1,264:
echo "Computed ", d, " digits in ", timeStr
 
echo "π = ", compress(piStr, 20), "..."</langsyntaxhighlight>
 
{{out}}
Line 945 ⟶ 1,272:
=={{header|OCaml}}==
program for calculating digits of pi
<langsyntaxhighlight OCamllang="ocaml">let limit = 10000 and n = 2800
let x = Array.make (n+1) 2000
 
Line 963 ⟶ 1,290:
f n 0;
print_newline()
</syntaxhighlight>
</lang>
{{out}}
<pre>31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185</pre>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">pi(n)=my(a=1,g=2^-.5);(1-2*sum(k=1,n,[a,g]=[(a+g)/2,sqrt(a*g)];(a^2-g^2)<<k))^-1*4*a^2
pi(6)</langsyntaxhighlight>
{{out}}
<pre>%1 = 3.1415926535897932384626433832795028841971693993751058209749445923078164062878</pre>
 
=={{header|Pascal}}==
{{works with|FPC}}
{{libheader|GMP}}
The program expects as an input parameter the required number of decimal digits of '''''π''''' (32 ≤ N ≤ 1000000), default is 256 digits.
<syntaxhighlight lang="pascal">
program AgmForPi;
{$mode objfpc}{$h+}{$b-}{$warn 5091 off}
uses
SysUtils, Math, GMP;
 
const
MIN_DIGITS = 32;
MAX_DIGITS = 1000000;
 
var
Digits: Cardinal = 256;
 
procedure ReadInput;
var
UserDigits: Cardinal;
begin
if (ParamCount > 0) and TryStrToDWord(ParamStr(1), UserDigits) then
Digits := Min(MAX_DIGITS, Max(UserDigits, MIN_DIGITS));
f_set_default_prec(Ceil((Digits + 1)/LOG_10_2));
end;
 
function Sqrt(a: MpFloat): MpFloat;
begin
Result := f_sqrt(a);
end;
 
function Sqr(a: MpFloat): MpFloat;
begin
Result := a * a;
end;
 
function PiDigits: string;
var
a0, b0, an, bn, tn: MpFloat;
n: Cardinal;
begin
n := 1;
an := 1;
bn := Sqrt(MpFloat(0.5));
tn := 0.25;
while n < Digits do begin
a0 := an;
b0 := bn;
an := (a0 + b0)/2;
bn := Sqrt(a0 * b0);
tn := tn - Sqr(an - a0) * n;
n := n + n;
end;
Result := Sqr(an + bn)/(tn * 4);
SetLength(Result, Succ(Digits));
end;
 
begin
ReadInput;
WriteLn(PiDigits);
end.
</syntaxhighlight>
{{out}}
<pre>
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648
</pre>
 
=={{header|Perl}}==
Line 978 ⟶ 1,372:
The number of steps used is based on the desired accuracy rather than being hard coded, as this is intended to work for 1M digits as well as for 100.
 
<langsyntaxhighlight lang="perl">use Math::BigFloat try => "GMP,Pari";
 
my $digits = shift || 100; # Get number of digits from command line
Line 1,000 ⟶ 1,394:
$an->bmul($an,$acc)->bdiv(4*$tn, $digits);
return $an;
}</langsyntaxhighlight>
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068</pre>
The following is a translation, almost line-for-line, of the Ruby code. It is slower than the above and the last digit or two may not be correct.
{{trans|Ruby}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use Math::BigFloat;
Line 1,021 ⟶ 1,415:
($a, $g) = @$x;
}
print $a * $a / $z, "\n";</langsyntaxhighlight>
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067</pre>
Line 1,028 ⟶ 1,422:
{{libheader|Phix/mpfr}}
{{trans|Python}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>include mpfr.e
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.0"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (mpfr_set_default_prec[ision] has been renamed)</span>
mpfr_set_default_prec(-200) -- set precision to 200 decimal places
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
mpfr a = mpfr_init(1),
n = mpfr_init(1),
<span style="color: #7060A8;">mpfr_set_default_precision</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">200</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- set precision to 200 decimal places</span>
g = mpfr_init(1),
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
z = mpfr_init(0.25),
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
half = mpfr_init(0.5),
<span style="color: #000000;">g</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
x1 = mpfr_init(2),
<span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0.25</span><span style="color: #0000FF;">),</span>
x2 = mpfr_init(),
<span style="color: #000000;">half</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">),</span>
var = mpfr_init()
<span style="color: #000000;">x1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span>
mpfr_sqrt(x1,x1)
<span style="color: #000000;">x2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(),</span>
mpfr_div(g,g,x1) -- g:= 1/sqrt(2)
<span style="color: #000000;">v</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">()</span>
string prev, this, fmt = "%.200Rf\n"
<span style="color: #7060A8;">mpfr_sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">)</span>
for i=1 to 18 do
<span style="color: #7060A8;">mpfr_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- g:= 1/sqrt(2)</span>
mpfr_add(x1,a,g)
<span style="color: #004080;">string</span> <span style="color: #000000;">prev</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">curr</span>
mpfr_mul(x1,x1,half)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">18</span> <span style="color: #008080;">do</span>
mpfr_mul(x2,a,g)
<span style="color: #7060A8;">mpfr_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">g</span><span style="color: #0000FF;">)</span>
mpfr_sqrt(x2,x2)
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">half</span><span style="color: #0000FF;">)</span>
mpfr_sub(var,x1,a)
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">g</span><span style="color: #0000FF;">)</span>
mpfr_mul(var,var,var)
<span style="color: #7060A8;">mpfr_sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">)</span>
mpfr_mul(var,var,n)
<span style="color: #7060A8;">mpfr_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
mpfr_sub(z,z,var)
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
mpfr_add(n,n,n)
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
mpfr_set(a,x1)
<span style="color: #7060A8;">mpfr_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
mpfr_set(g,x2)
<span style="color: #7060A8;">mpfr_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
mpfr_mul(var,a,a)
<span style="color: #7060A8;">mpfr_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">)</span>
mpfr_div(var,var,z)
<span style="color: #7060A8;">mpfr_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">)</span>
this = mpfr_sprintf(fmt,var)
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
if i>1 then
<span style="color: #7060A8;">mpfr_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
if this=prev then exit end if
<span style="color: #000000;">curr</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">200</span><span style="color: #0000FF;">)</span>
for j=3 to length(this) do
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
if prev[j]!=this[j] then
<span style="color: #008080;">if</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">=</span><span style="color: #000000;">prev</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,"iteration %d matches previous to %d places\n",{i,j-3})
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">curr</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
exit
<span style="color: #008080;">if</span> <span style="color: #000000;">prev</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">curr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
end if
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"iteration %d matches previous to %d places\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">})</span>
end for
<span style="color: #008080;">exit</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
prev = this
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if this=prev then
<span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">curr</span>
printf(1,"identical result to last iteration:\n%s\n",{this})
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
else
<span style="color: #008080;">if</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">=</span><span style="color: #000000;">prev</span> <span style="color: #008080;">then</span>
printf(1,"insufficient iterations\n")
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"identical result to last iteration:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">curr</span><span style="color: #0000FF;">})</span>
end if</lang>
<span style="color: #008080;">else</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"insufficient iterations\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,088 ⟶ 1,486:
=={{header|PicoLisp}}==
{{trans|Python}}
<langsyntaxhighlight PicoLisplang="picolisp">(scl 40)
 
(de pi ()
Line 1,108 ⟶ 1,506:
(println (pi))
(bye)</langsyntaxhighlight>
{{out}}
<pre>"3.1415926535897932384626433832795028841841"</pre>
Line 1,114 ⟶ 1,512:
=={{header|Python}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="python">from decimal import *
 
D = Decimal
Line 1,126 ⟶ 1,524:
n += n
a, g = x
print(a * a / z)</langsyntaxhighlight>
 
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067</pre>
 
=={{header|R}}==
 
{{libheader|Rmpfr}}
 
<syntaxhighlight lang="rsplus">library(Rmpfr)
 
agm <- function(n, prec) {
s <- mpfr(0, prec)
a <- mpfr(1, prec)
g <- sqrt(mpfr("0.5", prec))
p <- as.bigz(4)
for (i in seq(n)) {
m <- (a + g) / 2L
g <- sqrt(a * g)
a <- m
s <- s + p * (a * a - g * g)
p <- p + p
}
4L * a * a / (1L - s)
}
 
1e6 * log(10) / log(2)
# [1] 3321928
 
# Compute pi to one million decimal digits:
p <- 3322000
x <- agm(20, p)
 
# Check
roundMpfr(x - Const("pi", p), 64)
# 1 'mpfr' number of precision 64 bits
# [1] 4.90382361286485830568e-1000016
 
# Save to disk
f <- file("pi.txt", "w")
writeLines(formatMpfr(x, 1e6), f)
close(f)</syntaxhighlight>
 
=={{header|Racket}}==
{{trans|Ruby}}
<langsyntaxhighlight Racketlang="racket">#lang racket
(require math/bigfloat)
 
Line 1,155 ⟶ 1,591:
(parameterize ([bf-precision 200])
(displayln (bigfloat->string (pi/a-g 6)))
(displayln (bigfloat->string pi.bf)))</langsyntaxhighlight>
{{Out}}
<pre>3.1415926535897932384626433832793
Line 1,173 ⟶ 1,609:
} = \frac{\sqrt{n 10^{2N} / d}}{10^N}</math>
 
so that what we need is one square root of a big number that we'll truncate to its integer part. We'll computeuse the squaremethod rootdescribed ofin this[[Integer bigroots]] integerto by usingcompute the convergencesquare root of thethis recursivebig integer. sequence:
 
<math>u_{n+1} = \frac{1}{2}(u_n + \frac{x}{u_n})</math>
 
It's not too hard to see that such a sequence converges towards <math>\sqrt x</math>.
 
Notice that we don't get the exact number of decimals required : the last two decimals or so can be wrong. This is because we don't need <math>a_n</math>, but rather <math>a_n^2</math>. Elevating to the square makes us lose a bit of precision. It could be compensated by choosing a slightly higher value of N (in a way that could be precisely calculated), but that would probably be overkill.
<syntaxhighlight lang="raku" perl6line>constant number-of-decimals = 100;
 
multi sqrt(Int $n) {
my $guess = (10**($n.chars div 2);, { ($_ + $n div $_) div 2 } ... * == *).tail
my $iterator = { ( $^x + $n div ($^x) ) div 2 };
my $endpoint = { $^x == $^y|$^z };
return min (+$guess, $iterator … $endpoint)[*-1, *-2];
}
 
multi sqrt(FatRat $r --> FatRat) {
return FatRat.new:
sqrt($r.nude[0]numerator * 10**(number-of-decimals*2) div $r.nude[1]denominator),
10**number-of-decimals;
}
Line 1,198 ⟶ 1,627:
my FatRat $g = sqrt(1/2.FatRat);
my $z = .25;
 
for ^10 {
given [ ($a + $g)/2, sqrt($a * $g) ] {
$z -= (.[0] - $a)**2 * $n;
$n += $n;
($a, $g) = @$_;
say ($a ** 2 / $z).substr: 0, 2 + number-of-decimals;
}
}</langsyntaxhighlight>
{{out}}
<pre>3.1876726427121086272019299705253692326510535718593692264876339862751228325281223301147286106601617972
Line 1,222 ⟶ 1,651:
=={{header|REXX}}==
{{trans|Ruby}}
 
Programming note: &nbsp; the number of digits to be used in the calculations can be specified on the C.L. ('''c'''ommand '''l'''ine).
 
<br>Whatever number of digits used, the actual number of digits is five larger than specified, and then the result is rounded to the requested number of digits.
Whatever number of digits used, the actual number of digits is five larger than specified, and then the result is rounded to the requested number of digits.
===version 1===
<langsyntaxhighlight lang="rexx">/*REXX program calculates the value of pi using the AGM algorithm. */
parse arg d .; if d=='' | d=="," then d=500 500 /*D not specified? Then use default. */
numeric digits d+5 /*set the numeric decimal digits to D+5*/
z= 1/4; a= 1; g= sqrt(1/2) /*calculate some initial values. */
n= 1
do j=1 until a==old; old= a /*keep calculating until no more noise.*/
x= (a+g) * .5; g= sqrt(a*g) /*calculate the next set of terms. */
z= z - n*(x-a)**2; n= n+n; a= x /*Z is used in the final calculation. */
end /*j*/ /* [↑] stop if A equals OLD. */
 
pi= a**2 / z /*compute the finished value of pi. */
numeric digits d /*set the numeric decimal digits to D.*/
say pi / 1 /*display the computed value of pi. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); numeric digits; h=d+6
Line 1,244 ⟶ 1,675:
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
numeric digits d; return g/1</langsyntaxhighlight>
Programming note: &nbsp; the &nbsp; '''sqrt''' &nbsp; subroutine (above) is optimized for larger ''digits''.
 
'''{{out|output''' |text=&nbsp; when using the default number of digits: &nbsp; &nbsp; <tt> 500 </tt>}}
<pre>
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491
Line 1,255 ⟶ 1,686:
===version 2===
This REXX version shows the accurate (correct) number of digits in each iteration of the calculation of pi.
<lang rexx>/*REXX program calculates value of pi using the AGM algorithm (with running digits).*/
parse arg d .; if d=='' | d=="," then d=500 /*D not specified? Then use default. */
numeric digits d+5 /*set the numeric decimal digits to D+5*/
z=1/4; a=1; g=sqrt(1/2) /*calculate some initial values. */
n=1
 
For 1,005 decimal digits, &nbsp; it is over &nbsp; '''68''' &nbsp; times faster than version 3.
do j=1 until a==old; old=a /*keep calculating until no more noise.*/
<syntaxhighlight lang="rexx">/*REXX program calculates the AGM (arithmetic─geometric mean) of two (real) numbers. */
x=(a+g)*.5; g=sqrt(a*g) /*calculate the next set of terms. */
parse arg a b digs . z=z-n*(x-a)**2; n=n+n; a=x /*Zobtain optional isnumbers used infrom the final calculationC.L. */
if digs=='' | manydigs==compare(a",old) " then digs= 100 /*howNo manyDIGS accuratespecified? digits computed? Then use default.*/
numeric digits digs if many==0 then many=d /*adjustREXX forwill theuse verylots lastof timedecimal digits. */
if a=='' | a=="," say right('iteration' j, 20)then a=1 right(many, 9) "digits" /*showNo A specified? Then use digitsdefault.*/
if b=='' | b=="," end /*j*/then b=1 / sqrt(2) /*No B specified? " " " /* [↑] stop if A equals OLD. */
say call AGM a,b /*displayinvoke aAGM blank line& for adon't show separatorA,B,result.*/
pi=a**2exit 0 / z /*computestick thea finishedfork in valueit, of we're pi.all done. */
numeric digits d /*set the numeric decimal digits to D.*/
say pi / 1 /*display the computed value of pi. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrtagm: procedure;: parse arg x,y; if x=0y then return 0;x d=digits(); numeric digits; h=d+6/*is it an equality case? */
if y=0 then return 0 /*is value of Y zero? */
numeric form; m.=9; parse value format(x,2,1,,0) 'E0' with g "E" _ .; g=g *.5'e'_ %2
do j=0 while h>9; m.j=h; if x=0 then return y / h=h%2+1; /* " " end " X " /*j*/
d= digits(); numeric digits do k=jd+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.add 5; more digs to ensure end /*kconvergence*/
numerictiny= '1e-' || (digits() - 1) d; return g/1<*construct a pretty tiny REXX number. */lang>
ox= x + 1
'''output''' &nbsp; using the default number of digits: &nbsp; <tt> 500 </tt>
do #=1 while ox\=x & abs(ox)>tiny; ox= x; oy= y
x= (ox+oy)/2; y= sqrt(ox*oy)
end /*#*/
numeric digits d /*restore numeric digits to original.*/
/*this is the only output displayed ►─┐*/
say 'digits='right(d, 7)", iterations=" right(#, 3) /* ◄───────────────┘*/
return x/1 /*normalize X to the new digits. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_ % 2
do j=0 while h>9; m.j=h; h=h % 2 + 1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/; return g</syntaxhighlight>
{{out|output|text=&nbsp; when using the default number of digits: &nbsp; &nbsp; <tt> 500 </tt>}}
<pre>
iteration 1 1 digits
Line 1,294 ⟶ 1,730:
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491
</pre>
'''{{out|output''' |text=&nbsp; when using the number of digits: &nbsp; &nbsp; <tt> 100000 </tt>}}
<pre>
iteration 1 1 digits
Line 1,319 ⟶ 1,755:
 
===version 3===
<syntaxhighlight lang ="rexx">Do d=10 To 13
/*REXX*/
Do d=10 To 13
Say d pib(d)
End
Line 1,366 ⟶ 1,805:
End
Numeric Digits xprec
Return (r+0)</langsyntaxhighlight>
[{out}]
<pre>10 3.141592654
Line 1,378 ⟶ 1,817:
1004 3.141...201989381
1005 3.141...2019893810</pre>
 
=={{header|RPL}}==
{{trans|BASIC}}
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ → digits
≪ 0.5 SQ 1 1 0.5 √
'''WHILE''' 3 PICK digits ≤ '''REPEAT'''
OVER
ROT 3 PICK + 2 / ROT ROT
SWAP OVER * √ SWAP
3 PICK -
5 ROLL SWAP SQ 5 PICK * - 4 ROLLD
ROT DUP + ROT ROT
'''END'''
+ SQ ROT 4 * /
SWAP DROP
≫ ≫ ‘'''AGMPI'''’ STO
≪ { } 1 5 FOR d d '''AGMPI''' NEXT
≫ ‘'''TASK'''’ STO
|
'''AGMPI''' ''( digits -- pi )''
tn = 0.5 ^ 2 : pn = 1.0 : an = 1.0 : bn = sqrt(0.5)
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)
// clean stack
|}
{{out}}
<pre>
5: 2.91421356238
4: 3.14057925053
3: 3.14159264619
2: 3.14159264619
1: 3.14159265359
</pre>
 
=={{header|Ruby}}==
Using agm.
See [[Talk:Arithmetic-geometric mean]]
<langsyntaxhighlight lang="ruby"># Calculate Pi using the Arithmetic Geometric Mean of 1 and 1/sqrt(2)
#
#
Line 1,401 ⟶ 1,891:
g = x[1]
}
puts a * a / z</langsyntaxhighlight>
Produces:
<pre>
Line 1,438 ⟶ 1,928:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">/// calculate pi with algebraic/geometric mean
pub fn pi(n: usize) -> f64 {
let mut a : f64 = 1.0;
Line 1,459 ⟶ 1,949:
4.0 * a.powi(2) / (1.0-s)
}
</syntaxhighlight>
</lang>
Can be invoked like:
<langsyntaxhighlight lang="rust">
fn main() {
println!("pi(7): {}", pi(7));
}
</syntaxhighlight>
</lang>
Outputs:
<pre>pi(7): 3.1415926535901733</pre>
Line 1,473 ⟶ 1,963:
=={{header|Scala}}==
===Completely (tail) recursive===
<langsyntaxhighlight Scalalang="scala">import java.math.MathContext
 
import scala.annotation.tailrec
Line 1,508 ⟶ 1,998:
 
println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]")
}</langsyntaxhighlight>
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/z8KNd5c/2 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/lTZhfzz2Ry2W7kJT0Iyoww Scastie (JVM)]. Be patient, some heavy computing (~30 s) involved.
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func agm_pi(digits) {
var acc = (digits + 8);
 
Line 1,534 ⟶ 2,024:
}
 
say agm_pi(100);</langsyntaxhighlight>
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068</pre>
Line 1,541 ⟶ 2,031:
{{trans|Ruby}}
{{tcllib|math::bigfloat}}
<langsyntaxhighlight lang="tcl">package require math::bigfloat
namespace import math::bigfloat::*
 
Line 1,563 ⟶ 2,053:
}
 
puts [agm/π 17]</langsyntaxhighlight>
{{out}}
<small>(with added line breaks for clarity)</small>
Line 1,571 ⟶ 2,061:
{{trans|C#}}
{{Libheader|System.Numerics}}
<langsyntaxhighlight lang="vbnet">Imports System, System.Numerics
 
Module Program
Line 1,625 ⟶ 2,115:
End Sub
End Module
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:64ex;white-space: pre-wrap;">Computation time: 4.1539 seconds
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151557485724245415069595082953311686172785588907509838175463746493931925506040092770167113900984882401285836160356370766010471018194295559619894676783744944825537977472684710404753464620804668425906949129331367702898915210475216205696602405803815019351125338243003558764024749647326391419927260426992279678235478163600934172164121992458631503028618297455570674983850549458858692699569092721079750930295532116534498720275596023648066549911988183479775356636980742654252786255181841757467289097777279380008164706001614524919217321721477235014144197356854816136115735255213347574184946843852332390739414333454776241686251898356948556209921922218427255025425688767179049460165346680498862723279178608578438382796797668145410095388378636095068006422512520511739298489608412848862694560424196528502221066118630674427862203919494504712371378696095636437191728746776465757396241389086583264599581339047802759009946576407895126946839835259570982582262052248940772671947826848260147699090264013639443745530506820349625245174939965143142980919065925093722169646151570985838741059788595977297549893016175392846813826868386894277415599185592524595395943104997252468084598727364469584865383673622262609912460805124388439045124413654976278079771569143599770012961608944169486855584840635342207222582848864815845602850601684273945226746767889525213852254995466672782398645659611635488623057745649803559363456817432411251507606947945109659609402522887971089314566913686722874894056010150330861792868092087476091782493858900971490967598526136554978189312978482168299894872265880485756401427047755513237964145152374623436454285844479526586782105114135473573952311342716610213596953623144295248493718711014576540359027993440374200731057853906219838744780847848968332144571386875194350643021845319104848100537061468067491927819119793995206141966342875444064374512371819217999839101591956181467514269123974894090718649423196156794520809514655022523160388193014209376213785595663893778708303906979207734672218256259966150142150306803844773454920260541466592520149744285073251866600213243408819071048633173464965145390579626856100550810665879699816357473638405257145910289706414011097120628043903975951567715770042033786993600723055876317635942187312514712053292819182618612586732157919841484882916447060957527069572209175671167229109816909152801735067127485832228718352093539657251210835791513698820914442100675103346711031412671113699086585163983150197016515116851714376576183515565088490998985998238734552833163550764791853589322618548963213293308985706420467525907091548141654985946163718027098199430992448895757128289059232332609729971208443357326548938239119325974636673058360414281388303203824903758985243744170291327656180937734440307074692112019130203303801976211011004492932151608424448596376698389522868478312355265821314495768572624334418930396864262434107732269780280731891544110104468232527162010526522721116603966655730925471105578537634668206531098965269186205647693125705863566201855810072936065987648611791045334885034611365768675324944166803962657978771855608455296541266540853061434443185867697514566140680070023787765913440171274947042056223053899456131407112700040785473326993908145466464588079727082668306343285878569830523580893306575740679545716377525420211495576158140025012622859413021647155097925923099079654737612551765675135751782966645477917450112996148903046399471329621073404375189573596145890193897131117904297828564750320319869151402870808599048010941214722131794764777262241425485454033215718530614228813758504306332175182979866223717215916077166925474873898665494945011465406284336639379003976926567214638530673609657120918076383271664162748888007869256029022847210403172118608204190004229661711963779213375751149595015660496318629472654736425230817703675159067350235072835405670403867435136222247715891504953098444893330963408780769325993978054193414473774418426312986080998886874132604721569516239658645730216315981931951673538129741677294786724229246543668009806769282382806899640048243540370141631496589794092432378969070697794223625082216889573837986230015937764716512289357860158816175578297352334460428151262720373431465319777741603199066554187639792933441952154134189948544473456738316249934191318148092777710386387734317720754565453220777092120190516609628049092636019759882816133231666365286193266863360627356763035447762803504507772355471058595487027908143562401451718062464362679456127531813407833033625423278394497538243720583531147711992606381334677687969597030983391307710987040859133746414428227726346594704745878477872019277152807317679077071572134447306057007334924369311383504931631284042512192565179806941135280131470130478164378851852909285452011658393419656213491434159562586586557055269049652098580338507224264829397285847831630577775606888764462482468579260395352773480304802900587607582510474709164396136267604492562742042083208566119062545433721315359584506877246029016187667952406163425225771954291629919306455377991403734043287526288896399587947572917464263574552540790914513571113694109119393251910760208252026187985318877058429725916778131496990090192116971737278476847268608490033770242429165130050051683233643503895170298939223345172201381280696501178440874519601212285993716231301711444846409038906449544400619869075485160263275052983491874078668088183385102283345085048608250393021332197155184306354550076682829493041377655279397517546139539846833936383047461199665385815384205685338621867252334028308711232827892125077126294632295639898989358211674562701021835646220134967151881909730381198004973407239610368540664319395097901906996395524530054505806855019567302292191393391856803449039820595510022635353619204199474553859381023439554495977837790237421617271117236434354394782218185286240851400666044332588856986705431547069657474585503323233421073015459405165537906866273337995851156257843229882737231989875714159578111963583300594087306812160287649628674460477464915995054973742562690104903778198683593814657412680492564879855614537234786733039046883834363465537949864192705638729317487233208376011230299113679386270894387993620162951541337142489283072201269014754668476535761647737946752004907571555278196536213239264061601363581559074220202031872776052772190055614842555187925303435139844253223415762336106425063904975008656271095359194658975141310348227693062474353632569160781547818115284366795706110861533150445212747392454494542368288606134084148637767009612071512491404302725386076482363414334623518975766452164137679690314950191085759844239198629164219399490723623464684411739403265918404437805133389452574239950829659122850855582157250310712570126683024029295252201187267675622041542051618416348475651699981161410100299607838690929160302884002691041407928862150784245167090870006992821206604183718065355672525325675328612910424877618258297651579598470356222629348600341587229805349896502262917487882027342092222453398562647669149055628425039127577102840279980663658254889264880254566101729670266407655904290994568150652653053718294127033693137851786090407086671149655834343476933857817113864558736781230145876871266034891390956200993936103102916161528813843790990423174733639480457593149314052976347574811935670911013775172100803155902485309066920376719220332290943346768514221447737939375170344366199104033751117354719185504644902636551281622882446257591633303910722538374218214088350865739177150968288747826569959957449066175834413752239709683408005355984917541738188399944697486762655165827658483588453142775687900290951702835297163445621296404352311760066510124120065975585127617858382920419748442360800719304576189323492292796501987518721272675079812554709589045563579212210333466974992356302549478024901141952123828153091140790738602515227429958180724716259166854513331239480494707911915326734302824418604142636395480004480026704962482017928964766975831832713142517029692348896276684403232609275249603579964692565049368183609003238092934595889706953653494060340216654437558900456328822505452556405644824651518754711962184439658253375438856909411303150952617937800297412076651479394259029896959469955657612186561967337862362561252163208628692221032748892186543648022967807057656151446320469279068212073883778142335628236089632080682224680122482611771858963814091839036736722208883215137556003727983940041529700287830766709444745601345564172543709069793961225714298946715435784687886144458123145935719849225284716050492212424701412147805734551050080190869960330276347870810817545011930714122339086639383395294257869050764310063835198343893415961318543475464955697810382930971646514384070070736041123735998434522516105070270562352660127648483084076118301305279320542746286540360367453286510570658748822569815793678976697422057505968344086973502014102067235850200724522563265134105592401902742162484391403599895353945909440704691209140938700126456001623742880210927645793106579229552498872758461012648369998922569596881592056001016552563756785667227966198857827948488558343975187445455129656344348039664205579829368043522027709842942325330225763418070394769941597915945300697521482933665556615678736400536665641654732170439035213295435291694145990416087532018683793702348886894791510716378529023452924407736594956305100742108714261349745956151384987137570471017879573104229690666702144986374645952808243694457897723300487647652413390759204340196340391147320233807150952220106825634274716460243354400515212669324934196739770415956837535551667302739007497297363549645332888698440611964961627734495182736955882207573551766515898551909866653935494810688732068599075407923424023009259007017319603622547564789406475483466477604114632339056513433068449539790709030234604614709616968868850140834704054607429586991382966824681857103188790652870366508324319744047718556789348230894310682870272280973624809399627060747264553992539944280811373694338872940630792615959954626246297070625948455690347119729964090894180595343932512362355081349490043642785271383159125689892951964272875739469142725343669415323610045373048819855170659412173524625895487301676002988659257866285612496655235338294287854253404830833070165372285635591525347844598183134112900199920598135220511733658564078264849427644113763938669248031183644536985891754426473998822846218449008777697763127957226726555625962825427653183001340709223343657791601280931794017185985999338492354956400570995585611349802524990669842330173503580440811685526531170995708994273287092584878944364600504108922669178352587078595129834417295351953788553457374260859029081765155780390594640873506123226112009373108048548526357228257682034160504846627750450031262008007998049254853469414697751649327095049346393824322271885159740547021482897111777923761225788734771881968254629812686858170507402725502633290449762778944236216741191862694396506715157795867564823993917604260176338704549901761436412046921823707648878341968968611815581587360629386038101712158552726683008238340465647588040513808016336388742163714064354955618689641122821407533026551004241048967835285882902436709048871181909094945331442182876618103100735477054981596807720094746961343609286148494178501718077930681085469000944589952794243981392135055864221964834915126390128038320010977386806628779239718014613432445726400973742570073592100315415089367930081699805365202760072774967458400283624053460372634165542590276018348403068113818551059797056640075094260878857357960373245141467867036880988060971642584975951380693094494015154222219432913021739125383559150310033303251117491569691745027149433151558854039221640972291011290355218157628232831823425483261119128009282525619020526301639114772473314857391077758744253876117465786711694147764214411112635835538713610110232679877564102468240322648346417663698066378576813492045302240819727856471983963087815432211669122464159117767322532643356861461865452226812688726844596844241610785401676814208088502800541436131462308210259417375623899420757136275167457318918945628352570441335437585753426986994725470316566139919996826282472706413362221789239031760854289437339356188916512504244040089527198378738648058472689546243882343751788520143956005710481194988423906061369573423155907967034614914344788636041031823507365027785908975782727313050488939890099239135033732508559826558670892426124294736701939077271307068691709264625484232407485503660801360466895118400936686095463250021458529309500009071510582362672932645373821049387249966993394246855164832611341461106802674466373343753407642940266829738652209357016263846485285149036293201991996882851718395366913452224447080459239660281715655156566611135982311225062890585491450971575539002439315351909021071194573002438801766150352708626025378817975194780610137150044899172100222013350131060163915415895780371177927752259787428919179155224171895853616805947412341933984202187456492564434623925319531351033114763949119950728584306583619353693296992898379149419394060857248639688369032655643642166442576079147108699843157337496488352927693282207629472823815374099615455987982598910937171262182830258481123890119682214294576675807186538065064870261338928229949725745303328389638184394477077940228435988341003583854238973542439564755568409522484455413923941000162076936368467764130178196593799715574685419463348937484391297423914336593604100352343777065888677811394986164787471407932638587386247328896456435987746676384794665040741118256583788784548581489629612739984134427260860618724554523606431537101127468097787044640947582803487697589483282412392929605829486191966709189580898332012103184303401284951162035342801441276172858302435598300320420245120728725355811958401491809692533950757784000674655260314461670508276827722235341911026341631571474061238504258459884199076112872580591139356896014316682831763235673254170734208173322304629879928049085140947903688786878949305469557030726190095020764334933591060245450864536289354568629585313153371838682656178622736371697577418302398600659148161640494496501173213138957470620884748023653710311508984279927544268532779743113951435741722197597993596852522857452637962896126915723579866205734083757668738842664059909935050008133754324546359675048442352848747014435454195762584735642161981340734685411176688311865448937769795665172796623267148103386439137518659467300244345005449953997423723287124948347060440634716063258306498297955101095418362350303094530973358344628394763047756450150085075789495489313939448992161255255977014368589435858775263796255970816776438001254365023714127834679261019955852247172201777237004178084194239487254068015560359983905489857235467456423905858502167190313952629445543913166313453089390620467843877850542393905247313620129476918749751910114723152893267725339181466073000890277689631148109022097245207591672970078505807171863810549679731001678708506942070922329080703832634534520380278609905569001341371823683709919495164896007550493412678764367463849020639640197666855923356546391383631857456981471962108410809618846054560390384553437291414465134749407848844237721751543342603066988317683310011331086904219390310801437843341513709243530136776310849135161564226984750743032971674696406665315270353254671126675224605511995818319637637076179919192035795820075956053023462677579439363074630569010801149427141009391369138107258137813578940055995001835425118417213605572752210352680373572652792241737360575112788721819084490061780138897107708229310027976659358387589093956881485602632243937265624727760378908144588378550197028437793624078250527048758164703245812908783952324532378960298416692254896497156069811921865849267704039564812781021799132174163058105545988013004845629976511212415363745150056350701278159267142413421033015661653560247338078430286552572227530499988370153487930080626018096238151613669033411113865385109193673938352293458883225508870645075394739520439680790670868064450969865488016828743437861264538158342807530618454859037982179945996811544197425363443996029025100158882721647450068207041937615845471231834600726293395505482395571372568402322682130124767945226448209102356477527230820810635188991526928891084555711266039650343978962782500161101532351605196559042118449499077899920073294769058685778787209829013529566139788848605097860859570177312981553149516814671769597609942100361835591387778176984587581044662839988060061622984861693533738657877359833616133841338536842119789389001852956919678045544828584837011709672125353387586215823101331038776682721157269495181795897546939926421979155233857662316762754757035469941489290413018638611943919628388705436777432242768091323654494853667680000010652624854730558615989991401707698385483188750142938908995068545307651168033373222651756622075269517914422528081651716677667279303548515420402381746089232839170327542575086765511785939500279338959205766827896776445318404041855401043513483895312013263783692835808271937831265496174599705674507183320650345566440344904536275600112501843356073612227659492783937064784264567633881880756561216896050416113903906396016202215368494109260538768871483798955999911209916464644119185682770045742434340216722764455893301277815868695250694993646101756850601671453543158148010545886056455013320375864548584032402987170934809105562116715468484778039447569798042631809917564228098739987669732376957370158080682290459921236616890259627304306793165311494017647376938735140933618332161428021497633991898354848756252987524238730775595559554651963944018218409984124898262367377146722606163364329640633572810707887581640438148501884114318859882769449011932129682715888413386943468285900666408063140777577257056307294004929403024204984165654797367054855804458657202276378404668233798528271057843197535417950113472736257740802134768260450228515797957976474670228409995616015691089038458245026792659420555039587922981852648007068376504183656209455543461351341525700659748819163413595567196496540321872716026485930490397874895890661272507948282769389535217536218507962977851461884327192232238101587444505286652380225328438913752738458923844225354726530981715784478342158223270206902872323300538621634798850946954720047952311201504329322662827276321779088400878614802214753765781058197022263097174950721272484794781695729614236585957820908307332335603484653187302930266596450137183754288975579714499246540386817992138934692447419850973346267933210726868707680626399193619650440995421676278409146698569257150743157407938053239252394775574415918458215625181921552337096074833292349210345146264374498055961033079941453477845746999921285999993996122816152193148887693880222810830019860165494165426169685867883726095877456761825072759929508931805218729246108676399589161458550583972742098090978172932393010676638682404011130402470073508578287246271349463685318154696904669686939254725194139929146524238577625500474852954768147954670070503479995888676950161249722820403039954632788306959762493615101024365553522306906129493885990157346610237122354789112925476961760050479749280607212680392269110277722610254414922157650450812067717357120271802429681062037765788371669091094180744878140490755178203856539099104775941413215432844062503018027571696508209642734841469572639788425600845312140659358090412711359200419759851362547961606322887361813673732445060792441176399759746193835845749159880976674470930065463424234606342374746660804317012600520559284936959414340814685298150539471789004518357551541252235905906872648786357525419112888773717663748602766063496035367947026923229718683277173932361920077745221262475186983349515101986426988784717193966497690708252174233656627259284406204302141137199227852699846988477023238238400556555178890876613601304770984386116870523105531491625172837327286760072481729876375698163354150746088386636406934704372066886512756882661497307886570156850169186474885416791545965072342877306998537139043002665307839877638503238182155355973235306860430106757608389086270498418885951380910304235957824951439885901131858358406674723702971497850841458530857813391562707603563907639473114554958322669457024941398316343323789759556808568362972538679132750555425244919435891284050452269538121791319145135009938463117740179715122837854601160359554028644059024964669307077690554810288502080858008781157738171917417760173307385547580060560143377432990127286772530431825197579167929699650414607066457125888346979796429316229655201687973000356463045793088403274807718115553309098870255052076804630346086581653948769519600440848206596737947316808641564565053004988161649057883115434548505266006982309315777650037807046612647060214575057932709620478256152471459189652236083966456241051955105223572397395128818164059785914279148165426328920042816091369377737222999833270820829699557377273756676155271139225880552018988762011416800546873655806334716037342917039079863965229613128017826797172898229360702880690877686605932527463784053976918480820410219447197138692560841624511239806201131845412447820501107987607171556831540788654390412108730324020106853419472304766667217498698685470767812051247367924791931508564447753798537997322344561227858432968466475133365736923872014647236794278700425032555899268843495928761240075587569464137056251400117971331662071537154360068764773186755871487839890810742953094106059694431584775397009439883949144323536685392099468796450665339857388878661476294434140104988899316005120767810358861166020296119363968213496075011164983278563531614516845769568710900299976984126326650234771672865737857908574664607722834154031144152941880478254387617707904300015669867767957609099669360755949651527363498118964130433116627747123388174060373174397054067031096767657486953587896700319258662594105105335843846560233917967492678447637084749783336555790073841914731988627135259546251816043422537299628632674968240580602964211463864368642247248872834341704415734824818333016405669596688667695634914163284264149745333499994800026699875888159350735781519588990053951208535103572613736403436753471410483601754648830040784641674521673719048310967671134434948192626811107399482506073949507350316901973185211955263563258433909982249862406703107683184466072912487475403161796994113973877658998685541703188477886759290260700432126661791922352093822787888098863359911608192353555704646349113208591897961327913197564909760001399623444553501434642686046449586247690943470482932941404111465409239883444351591332010773944111840741076849810663472410482393582740194493566516108846312567852977697346843030614624180358529331597345830384554103370109167677637427621021370135485445092630719011473184857492331816720721372793556795284439254815609137281284063330393735624200160456645574145881660521666087387480472433912129558777639069690370788285277538940524607584962315743691711317613478388271941686066257210368513215664780014767523103935786068961112599602818393095487090590738613519145918195102973278755710497290114871718971800469616977700179139196137914171627070189584692143436967629274591099400600849835684252019155937037010110497473394938778859894174330317853487076032219829705797511914405109942358830345463534923498268836240433272674155403016195056806541809394099820206099941402168909007082133072308966211977553066591881411915778362729274615618571037217247100952142369648308641025928874579993223749551912219519034244523075351338068568073544649951272031744871954039761073080602699062580760202927314552520780799141842906388443734996814582733720726639176702011830046481900024130835088465841521489912761065137415394356572113903285749187690944137020905170314877734616528798482353382972601361109845148418238081205409961252745808810994869722161285248974255555160763716750548961730168096138038119143611439921063800508321409876045993093248510251682944672606661381517457125597549535802399831469822036133808284993567055755247129027453977621404931820146580080215665360677655087838043041343105918046068008345911366408348874080057412725867047922583191274157390809143831384564241509408491339180968402511639919368532255573389669537490266209232613188558915808324555719484538756287861288590041060060737465014026278240273469625282171749415823317492396835301361786536737606421667781377399510065895288</pre>
 
=={{header|TI SR-56}}==
{| class="wikitable"
|+ Texas Instruments SR-56 Program Listing for "Calculate Pi"
|-
! Display !! Key !! Display !! Key !! Display !! Key !! Display !! Key
|-
| 00 03 || 3 || 25 54 || / || 50 04 || 4 || 75 ||
|-
| 01 33 || STO || 26 02 || 2 || 51 34 || RCL || 76 ||
|-
| 02 00 || 0 || 27 94 || = || 52 01 || 1 || 77 ||
|-
| 03 01 || 1 || 28 32 || x><t|| 53 43 || x² || 78 ||
|-
| 04 33 || STO || 29 64 || * || 54 54 || / || 79 ||
|-
| 05 01 || 1 || 30 34 || RCL || 55 34 || RCL || 80 ||
|-
| 06 33 || STO || 31 02 || 2 || 56 04 || 4 || 81 ||
|-
| 07 03 || 3 || 32 94 || = || 57 94 || = || 82 ||
|-
| 08 02 || 2 || 33 48 || *√x || 58 59 || *pause || 83 ||
|-
| 09 48 || *√x || 34 33 || STO || 59 27 || *dsz|| 84 ||
|-
| 10 20 || *1/x || 35 02 || 2 || 60 01 || 1 || 85 ||
|-
| 11 33 || STO || 36 32 || x><t|| 61 08 || 8 || 86 ||
|-
| 12 02 || 2 || 37 74 || - || 62 41 || R/S || 87 ||
|-
| 13 92 || . || 38 39 || *EXC|| 63 || || 88 ||
|-
| 14 02 || 2 || 39 01 || 1 || 64 || || 89 ||
|-
| 15 05 || 5 || 40 94 || = || 65 || || 90 ||
|-
| 16 33 || STO || 41 43 || x² || 66 || || 91 ||
|-
| 17 04 || 4 || 42 64 || * || 67 || || 92 ||
|-
| 18 34 || RCL || 43 34 || RCL || 68 || || 93 ||
|-
| 19 01 || 1 || 44 03 || 3 || 69 || || 94 ||
|-
| 20 84 || + || 45 35 || SUM || 70 || || 95 ||
|-
| 21 32 || x><t || 46 03 || 3 || 71 || || 96 ||
|-
| 22 34 || RCL || 47 94 || = || 72 || || 97 ||
|-
| 23 02 || 2 || 48 12 || INV || 73 || || 98 ||
|-
| 24 94 || = || 49 35 || SUM || 74 || || 99 ||
|}
 
Asterisk denotes 2nd function key.
 
{| class="wikitable"
|+ Register allocation
|-
| 0: Loop count || 1: Arithmetic Term || 2: Geometric Term || 3: Power of Two || 4: Divisor Term
|-
| 5: Unused || 6: Unused || 7: Unused || 8: Unused || 9: Unused
|}
 
Annotated listing:
<syntaxhighlight lang="text">
3 STO 0 // r0 = 3 (loop count)
1 STO 1 STO 3 // r1 = a0, r3 = 1
2 √x 1/x STO 2 // r2 = g0
. 2 5 STO 4 // r4 = 0.25
RCL 1 + x><t RCL 2 = / 2 = // t = a0, x = a1
x><t * RCL 2 = √x STO 2 // t = a1, r2 = g1
x><t - EXC 1 = // x = (a1 - a0), r1 = a1
x² * RCL 3 SUM 3 = INV SUM 4 // r4 = r4-r3(a1-a0)^2, r3 = r3*2
RCL 1 x² / RCL 4 = pause
dsz 18
R/S
</syntaxhighlight>
 
'''Usage:'''
 
Press RST R/S.
 
{{out}}
 
Intermediate results flash on the screen, converging on the correct answer.
 
<pre>
3.187672643
</pre>
 
<pre>
3.141680293
</pre>
 
The third, final iteration yields:
 
<pre>
3.141592654
</pre>
 
=={{header|Wren}}==
{{trans|Sidef}}
{{libheader|Wren-big}}
<langsyntaxhighlight ecmascriptlang="wren">import "./big" for BigRat
 
var digits = 500
Line 1,649 ⟶ 2,243:
}
var pi = (an + bn).square / (tn * 4)
System.print(pi.toDecimal(digits, false))</langsyntaxhighlight>
 
{{out}}
Line 1,655 ⟶ 2,249:
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912
</pre>
 
[[Category:Geometry]]
337

edits