Arithmetic-geometric mean/Calculate Pi: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 22: Line 22:
<!-- Example by Nigel Galloway, Feb 8, 2012 -->
<!-- Example by Nigel Galloway, Feb 8, 2012 -->
{{libheader|GMP}}
{{libheader|GMP}}
<lang c>#include "gmp.h"
<syntaxhighlight lang=c>#include "gmp.h"


void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
Line 63: Line 63:
gmp_printf ("%.100000Ff\n", x0);
gmp_printf ("%.100000Ff\n", x0);
return 0;
return 0;
}</lang>
}</syntaxhighlight>
i<7 produces:
i<7 produces:
<pre style="height:64ex;white-space: pre-wrap;">
<pre style="height:64ex;white-space: pre-wrap;">
Line 74: Line 74:


=={{header|C sharp|C#}}==
=={{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).<lang csharp>using System;
{{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).<syntaxhighlight lang=csharp>using System;
using System.Numerics;
using System.Numerics;


Line 131: Line 131:
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:64ex;white-space: pre-wrap;">
<pre style="height:64ex;white-space: pre-wrap;">
Line 140: Line 140:
{{trans|C}}
{{trans|C}}
{{libheader|GMP}}
{{libheader|GMP}}
<lang cpp>#include <gmpxx.h>
<syntaxhighlight lang=cpp>#include <gmpxx.h>
#include <chrono>
#include <chrono>


Line 182: Line 182:
printf("Took %f ms for output.\n", duration<double>(steady_clock::now() - st).count() * 1000.0);
printf("Took %f ms for output.\n", duration<double>(steady_clock::now() - st).count() * 1000.0);
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:64ex;white-space: pre-wrap;">Took 60.726400 ms for computation.
<pre style="height:64ex;white-space: pre-wrap;">Took 60.726400 ms for computation.
Line 190: Line 190:
=={{header|Clojure}}==
=={{header|Clojure}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang lisp>(ns async-example.core
<syntaxhighlight lang=lisp>(ns async-example.core
(:use [criterium.core])
(:use [criterium.core])
(:gen-class))
(:gen-class))
Line 217: Line 217:
(doseq [q (partition-all 200 (str (compute-pi 18)))]
(doseq [q (partition-all 200 (str (compute-pi 18)))]
(println (apply str q)))
(println (apply str q)))
</syntaxhighlight>
</lang>
{{Output}}
{{Output}}
<pre style="height:64ex;overflow:scroll">
<pre style="height:64ex;overflow:scroll">
Line 266: Line 266:
{{libheader|MMA}}
{{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>
<p>This is an example that uses the Common Lisp Bigfloat Package (http://www.cs.berkeley.edu/~fateman/lisp/mma4max/more/bf.lisp)</p>
<lang lisp>(load "bf.fasl")
<syntaxhighlight lang=lisp>(load "bf.fasl")


;;(setf mma::bigfloat-bin-prec 1000)
;;(setf mma::bigfloat-bin-prec 1000)
Line 283: Line 283:
(setf A X1)
(setf A X1)
(setf G X2)))
(setf G X2)))
(mma:bigfloat-/ (mma:bigfloat-* A A) Z))</lang>
(mma:bigfloat-/ (mma:bigfloat-* A A) Z))</syntaxhighlight>
{{out}}
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141274</pre>
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141274</pre>
Line 289: Line 289:
=={{header|D}}==
=={{header|D}}==
{{trans|C#}}
{{trans|C#}}
<lang d>import std.bigint;
<syntaxhighlight lang=d>import std.bigint;
import std.conv;
import std.conv;
import std.math;
import std.math;
Line 362: Line 362:
string piStr = to!string(pi);
string piStr = to!string(pi);
writeln(piStr[0], '.', piStr[1..$]);
writeln(piStr[0], '.', piStr[1..$]);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:64ex;white-space: pre-wrap;">3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151557485724245415069595082953311686172785588907509838175463746493931925506040092770167113900984882401285836160356370766010471018194295559619894676783744944825537977472684710404753464620804668425906949129331367702898915210475216205696602405803815019351125338243003558764024749647326391419927260426992279678235478163600934172164121992458631503028618297455570674983850549458858692699569092721079750930295532116534498720275596023648066549911988183479775356636980742654252786255181841757467289097777279380008164706001614524919217321721477235014144197356854816136115735255213347574184946843852332390739414333454776241686251898356948556209921922218427255025425688767179049460165346680498862723279178608578438382796797668145410095388378636095068006422512520511739298489608412848862694560424196528502221066118630674427862203919494504712371378696095636437191728746776465757396241389086583264599581339047802759009946576407895126946839835259570982582262052248940772671947826848260147699090264013639443745530506820349625245174939965143142980919065925093722169646151570985838741059788595977297549893016175392846813826868386894277415599185592524595395943104997252468084598727364469584865383673622262609912460805124388439045124413654976278079771569143599770012961608944169486855584840635342207222582848864815845602850601684273945226746767889525213852254995466672782398645659611635488623057745649803559363456817432411251507606947945109659609402522887971089314566913686722874894056010150330861792868092087476091782493858900971490967598526136554978189312978482168299894872265880485756401427047755513237964145152374623436454285844479526586782105114135473573952311342716610213596953623144295248493718711014576540359027993440374200731057853906219838744780847848968332144571386875194350643021845319104848100537061468067491927819119793995206141966342875444064374512371819217999839101591956181467514269123974894090718649423196156794520809514655022523160388193014209376213785595663893778708303906979207734672218256259966150142150306803844773454920260541466592520149744285073251866600213243408819071048633173464965145390579626856100550810665879699816357473638405257145910289706414011097120628043903975951567715770042033786993600723055876317635942187312514712053292819182618612586732157919841484882916447060957527069572209175671167229109816909152801735067127485832228718352093539657251210835791513698820914442100675103346711031412671113699086585163983150197016515116851714376576183515565088490998985998238734552833163550764791853589322618548963213293308985706420467525907091548141654985946163718027098199430992448895757128289059232332609729971208443357326548938239119325974636673058360414281388303203824903758985243744170291327656180937734440307074692112019130203303801976211011004492932151608424448596376698389522868478312355265821314495768572624334418930396864262434107732269780280731891544110104468232527162010526522721116603966655730925471105578537634668206531098965269186205647693125705863566201855810072936065987648611791045334885034611365768675324944166803962657978771855608455296541266540853061434443185867697514566140680070023787765913440171274947042056223053899456131407112700040785473326993908145466464588079727082668306343285878569830523580893306575740679545716377525420211495576158140025012622859413021647155097925923099079654737612551765675135751782966645477917450112996148903046399471329621073404375189573596145890193897131117904297828564750320319869151402870808599048010941214722131794764777262241425485454033215718530614228813758504306332175182979866223717215916077166925474873898665494945011465406284336639379003976926567214638530673609657120918076383271664162748888007869256029022847210403172118608204190004229661711963779213375751149595015660496318629472654736425230817703675159067350235072835405670403867435136222247715891504953098444893330963408780769325993978054193414473774418426312986080998886874132604721569516239658645730216315981931951673538129741677294786724229246543668009806769282382806899640048243540370141631496589794092432378969070697794223625082216889573837986230015937764716512289357860158816175578297352334460428151262720373431465319777741603199066554187639792933441952154134189948544473456738316249934191318148092777710386387734317720754565453220777092120190516609628049092636019759882816133231666365286193266863360627356763035447762803504507772355471058595487027908143562401451718062464362679456127531813407833033625423278394497538243720583531147711992606381334677687969597030983391307710987040859133746414428227726346594704745878477872019277152807317679077071572134447306057007334924369311383504931631284042512192565179806941135280131470130478164378851852909285452011658393419656213491434159562586586557055269049652098580338507224264829397285847831630577775606888764462482468579260395352773480304802900587607582510474709164396136267604492562742042083208566119062545433721315359584506877246029016187667952406163425225771954291629919306455377991403734043287526288896399587947572917464263574552540790914513571113694109119393251910760208252026187985318877058429725916778131496990090192116971737278476847268608490033770242429165130050051683233643503895170298939223345172201381280696501178440874519601212285993716231301711444846409038906449544400619869075485160263275052983491874078668088183385102283345085048608250393021332197155184306354550076682829493041377655279397517546139539846833936383047461199665385815384205685338621867252334028308711232827892125077126294632295639898989358211674562701021835646220134967151881909730381198004973407239610368540664319395097901906996395524530054505806855019567302292191393391856803449039820595510022635353619204199474553859381023439554495977837790237421617271117236434354394782218185286240851400666044332588856986705431547069657474585503323233421073015459405165537906866273337995851156257843229882737231989875714159578111963583300594087306812160287649628674460477464915995054973742562690104903778198683593814657412680492564879855614537234786733039046883834363465537949864192705638729317487233208376011230299113679386270894387993620162951541337142489283072201269014754668476535761647737946752004907571555278196536213239264061601363581559074220202031872776052772190055614842555187925303435139844253223415762336106425063904975008656271095359194658975141310348227693062474353632569160781547818115284366795706110861533150445212747392454494542368288606134084148637767009612071512491404302725386076482363414334623518975766452164137679690314950191085759844239198629164219399490723623464684411739403265918404437805133389452574239950829659122850855582157250310712570126683024029295252201187267675622041542051618416348475651699981161410100299607838690929160302884002691041407928862150784245167090870006992821206604183718065355672525325675328612910424877618258297651579598470356222629348600341587229805349896502262917487882027342092222453398562647669149055628425039127577102840279980663658254889264880254566101729670266407655904290994568150652653053718294127033693137851786090407086671149655834343476933857817113864558736781230145876871266034891390956200993936103102916161528813843790990423174733639480457593149314052976347574811935670911013775172100803155902485309066920376719220332290943346768514221447737939375170344366199104033751117354719185504644902636551281622882446257591633303910722538374218214088350865739177150968288747826569959957449066175834413752239709683408005355984917541738188399944697486762655165827658483588453142775687900290951702835297163445621296404352311760066510124120065975585127617858382920419748442360800719304576189323492292796501987518721272675079812554709589045563579212210333466974992356302549478024901141952123828153091140790738602515227429958180724716259166854513331239480494707911915326734302824418604142636395480004480026704962482017928964766975831832713142517029692348896276684403232609275249603579964692565049368183609003238092934595889706953653494060340216654437558900456328822505452556405644824651518754711962184439658253375438856909411303150952617937800297412076651479394259029896959469955657612186561967337862362561252163208628692221032748892186543648022967807057656151446320469279068212073883778142335628236089632080682224680122482611771858963814091839036736722208883215137556003727983940041529700287830766709444745601345564172543709069793961225714298946715435784687886144458123145935719849225284716050492212424701412147805734551050080190869960330276347870810817545011930714122339086639383395294257869050764310063835198343893415961318543475464955697810382930971646514384070070736041123735998434522516105070270562352660127648483084076118301305279320542746286540360367453286510570658748822569815793678976697422057505968344086973502014102067235850200724522563265134105592401902742162484391403599895353945909440704691209140938700126456001623742880210927645793106579229552498872758461012648369998922569596881592056001016552563756785667227966198857827948488558343975187445455129656344348039664205579829368043522027709842942325330225763418070394769941597915945300697521482933665556615678736400536665641654732170439035213295435291694145990416087532018683793702348886894791510716378529023452924407736594956305100742108714261349745956151384987137570471017879573104229690666702144986374645952808243694457897723300487647652413390759204340196340391147320233807150952220106825634274716460243354400515212669324934196739770415956837535551667302739007497297363549645332888698440611964961627734495182736955882207573551766515898551909866653935494810688732068599075407923424023009259007017319603622547564789406475483466477604114632339056513433068449539790709030234604614709616968868850140834704054607429586991382966824681857103188790652870366508324319744047718556789348230894310682870272280973624809399627060747264553992539944280811373694338872940630792615959954626246297070625948455690347119729964090894180595343932512362355081349490043642785271383159125689892951964272875739469142725343669415323610045373048819855170659412173524625895487301676002988659257866285612496655235338294287854253404830833070165372285635591525347844598183134112900199920598135220511733658564078264849427644113763938669248031183644536985891754426473998822846218449008777697763127957226726555625962825427653183001340709223343657791601280931794017185985999338492354956400570995585611349802524990669842330173503580440811685526531170995708994273287092584878944364600504108922669178352587078595129834417295351953788553457374260859029081765155780390594640873506123226112009373108048548526357228257682034160504846627750450031262008007998049254853469414697751649327095049346393824322271885159740547021482897111777923761225788734771881968254629812686858170507402725502633290449762778944236216741191862694396506715157795867564823993917604260176338704549901761436412046921823707648878341968968611815581587360629386038101712158552726683008238340465647588040513808016336388742163714064354955618689641122821407533026551004241048967835285882902436709048871181909094945331442182876618103100735477054981596807720094746961343609286148494178501718077930681085469000944589952794243981392135055864221964834915126390128038320010977386806628779239718014613432445726400973742570073592100315415089367930081699805365202760072774967458400283624053460372634165542590276018348403068113818551059797056640075094260878857357960373245141467867036880988060971642584975951380693094494015154222219432913021739125383559150310033303251117491569691745027149433151558854039221640972291011290355218157628232831823425483261119128009282525619020526301639114772473314857391077758744253876117465786711694147764214411112635835538713610110232679877564102468240322648346417663698066378576813492045302240819727856471983963087815432211669122464159117767322532643356861461865452226812688726844596844241610785401676814208088502800541436131462308210259417375623899420757136275167457318918945628352570441335437585753426986994725470316566139919996826282472706413362221789239031760854289437339356188916512504244040089527198378738648058472689546243882343751788520143956005710481194988423906061369573423155907967034614914344788636041031823507365027785908975782727313050488939890099239135033732508559826558670892426124294736701939077271307068691709264625484232407485503660801360466895118400936686095463250021458529309500009071510582362672932645373821049387249966993394246855164832611341461106802674466373343753407642940266829738652209357016263846485285149036293201991996882851718395366913452224447080459239660281715655156566611135982311225062890585491450971575539002439315351909021071194573002438801766150352708626025378817975194780610137150044899172100222013350131060163915415895780371177927752259787428919179155224171895853616805947412341933984202187456492564434623925319531351033114763949119950728584306583619353693296992898379149419394060857248639688369032655643642166442576079147108699843157337496488352927693282207629472823815374099615455987982598910937171262182830258481123890119682214294576675807186538065064870261338928229949725745303328389638184394477077940228435988341003583854238973542439564755568409522484455413923941000162076936368467764130178196593799715574685419463348937484391297423914336593604100352343777065888677811394986164787471407932638587386247328896456435987746676384794665040741118256583788784548581489629612739984134427260860618724554523606431537101127468097787044640947582803487697589483282412392929605829486191966709189580898332012103184303401284951162035342801441276172858302435598300320420245120728725355811958401491809692533950757784000674655260314461670508276827722235341911026341631571474061238504258459884199076112872580591139356896014316682831763235673254170734208173322304629879928049085140947903688786878949305469557030726190095020764334933591060245450864536289354568629585313153371838682656178622736371697577418302398600659148161640494496501173213138957470620884748023653710311508984279927544268532779743113951435741722197597993596852522857452637962896126915723579866205734083757668738842664059909935050008133754324546359675048442352848747014435454195762584735642161981340734685411176688311865448937769795665172796623267148103386439137518659467300244345005449953997423723287124948347060440634716063258306498297955101095418362350303094530973358344628394763047756450150085075789495489313939448992161255255977014368589435858775263796255970816776438001254365023714127834679261019955852247172201777237004178084194239487254068015560359983905489857235467456423905858502167190313952629445543913166313453089390620467843877850542393905247313620129476918749751910114723152893267725339181466073000890277689631148109022097245207591672970078505807171863810549679731001678708506942070922329080703832634534520380278609905569001341371823683709919495164896007550493412678764367463849020639640197666855923356546391383631857456981471962108410809618846054560390384553437291414465134749407848844237721751543342603066988317683310011331086904219390310801437843341513709243530136776310849135161564226984750743032971674696406665315270353254671126675224605511995818319637637076179919192035795820075956053023462677579439363074630569010801149427141009391369138107258137813578940055995001835425118417213605572752210352680373572652792241737360575112788721819084490061780138897107708229310027976659358387589093956881485602632243937265624727760378908144588378550197028437793624078250527048758164703245812908783952324532378960298416692254896497156069811921865849267704039564812781021799132174163058105545988013004845629976511212415363745150056350701278159267142413421033015661653560247338078430286552572227530499988370153487930080626018096238151613669033411113865385109193673938352293458883225508870645075394739520439680790670868064450969865488016828743437861264538158342807530618454859037982179945996811544197425363443996029025100158882721647450068207041937615845471231834600726293395505482395571372568402322682130124767945226448209102356477527230820810635188991526928891084555711266039650343978962782500161101532351605196559042118449499077899920073294769058685778787209829013529566139788848605097860859570177312981553149516814671769597609942100361835591387778176984587581044662839988060061622984861693533738657877359833616133841338536842119789389001852956919678045544828584837011709672125353387586215823101331038776682721157269495181795897546939926421979155233857662316762754757035469941489290413018638611943919628388705436777432242768091323654494853667680000010652624854730558615989991401707698385483188750142938908995068545307651168033373222651756622075269517914422528081651716677667279303548515420402381746089232839170327542575086765511785939500279338959205766827896776445318404041855401043513483895312013263783692835808271937831265496174599705674507183320650345566440344904536275600112501843356073612227659492783937064784264567633881880756561216896050416113903906396016202215368494109260538768871483798955999911209916464644119185682770045742434340216722764455893301277815868695250694993646101756850601671453543158148010545886056455013320375864548584032402987170934809105562116715468484778039447569798042631809917564228098739987669732376957370158080682290459921236616890259627304306793165311494017647376938735140933618332161428021497633991898354848756252987524238730775595559554651963944018218409984124898262367377146722606163364329640633572810707887581640438148501884114318859882769449011932129682715888413386943468285900666408063140777577257056307294004929403024204984165654797367054855804458657202276378404668233798528271057843197535417950113472736257740802134768260450228515797957976474670228409995616015691089038458245026792659420555039587922981852648007068376504183656209455543461351341525700659748819163413595567196496540321872716026485930490397874895890661272507948282769389535217536218507962977851461884327192232238101587444505286652380225328438913752738458923844225354726530981715784478342158223270206902872323300538621634798850946954720047952311201504329322662827276321779088400878614802214753765781058197022263097174950721272484794781695729614236585957820908307332335603484653187302930266596450137183754288975579714499246540386817992138934692447419850973346267933210726868707680626399193619650440995421676278409146698569257150743157407938053239252394775574415918458215625181921552337096074833292349210345146264374498055961033079941453477845746999921285999993996122816152193148887693880222810830019860165494165426169685867883726095877456761825072759929508931805218729246108676399589161458550583972742098090978172932393010676638682404011130402470073508578287246271349463685318154696904669686939254725194139929146524238577625500474852954768147954670070503479995888676950161249722820403039954632788306959762493615101024365553522306906129493885990157346610237122354789112925476961760050479749280607212680392269110277722610254414922157650450812067717357120271802429681062037765788371669091094180744878140490755178203856539099104775941413215432844062503018027571696508209642734841469572639788425600845312140659358090412711359200419759851362547961606322887361813673732445060792441176399759746193835845749159880976674470930065463424234606342374746660804317012600520559284936959414340814685298150539471789004518357551541252235905906872648786357525419112888773717663748602766063496035367947026923229718683277173932361920077745221262475186983349515101986426988784717193966497690708252174233656627259284406204302141137199227852699846988477023238238400556555178890876613601304770984386116870523105531491625172837327286760072481729876375698163354150746088386636406934704372066886512756882661497307886570156850169186474885416791545965072342877306998537139043002665307839877638503238182155355973235306860430106757608389086270498418885951380910304235957824951439885901131858358406674723702971497850841458530857813391562707603563907639473114554958322669457024941398316343323789759556808568362972538679132750555425244919435891284050452269538121791319145135009938463117740179715122837854601160359554028644059024964669307077690554810288502080858008781157738171917417760173307385547580060560143377432990127286772530431825197579167929699650414607066457125888346979796429316229655201687973000356463045793088403274807718115553309098870255052076804630346086581653948769519600440848206596737947316808641564565053004988161649057883115434548505266006982309315777650037807046612647060214575057932709620478256152471459189652236083966456241051955105223572397395128818164059785914279148165426328920042816091369377737222999833270820829699557377273756676155271139225880552018988762011416800546873655806334716037342917039079863965229613128017826797172898229360702880690877686605932527463784053976918480820410219447197138692560841624511239806201131845412447820501107987607171556831540788654390412108730324020106853419472304766667217498698685470767812051247367924791931508564447753798537997322344561227858432968466475133365736923872014647236794278700425032555899268843495928761240075587569464137056251400117971331662071537154360068764773186755871487839890810742953094106059694431584775397009439883949144323536685392099468796450665339857388878661476294434140104988899316005120767810358861166020296119363968213496075011164983278563531614516845769568710900299976984126326650234771672865737857908574664607722834154031144152941880478254387617707904300015669867767957609099669360755949651527363498118964130433116627747123388174060373174397054067031096767657486953587896700319258662594105105335843846560233917967492678447637084749783336555790073841914731988627135259546251816043422537299628632674968240580602964211463864368642247248872834341704415734824818333016405669596688667695634914163284264149745333499994800026699875888159350735781519588990053951208535103572613736403436753471410483601754648830040784641674521673719048310967671134434948192626811107399482506073949507350316901973185211955263563258433909982249862406703107683184466072912487475403161796994113973877658998685541703188477886759290260700432126661791922352093822787888098863359911608192353555704646349113208591897961327913197564909760001399623444553501434642686046449586247690943470482932941404111465409239883444351591332010773944111840741076849810663472410482393582740194493566516108846312567852977697346843030614624180358529331597345830384554103370109167677637427621021370135485445092630719011473184857492331816720721372793556795284439254815609137281284063330393735624200160456645574145881660521666087387480472433912129558777639069690370788285277538940524607584962315743691711317613478388271941686066257210368513215664780014767523103935786068961112599602818393095487090590738613519145918195102973278755710497290114871718971800469616977700179139196137914171627070189584692143436967629274591099400600849835684252019155937037010110497473394938778859894174330317853487076032219829705797511914405109942358830345463534923498268836240433272674155403016195056806541809394099820206099941402168909007082133072308966211977553066591881411915778362729274615618571037217247100952142369648308641025928874579993223749551912219519034244523075351338068568073544649951272031744871954039761073080602699062580760202927314552520780799141842906388443734996814582733720726639176702011830046481900024130835088465841521489912761065137415394356572113903285749187690944137020905170314877734616528798482353382972601361109845148418238081205409961252745808810994869722161285248974255555160763716750548961730168096138038119143611439921063800508321409876045993093248510251682944672606661381517457125597549535802399831469822036133808284993567055755247129027453977621404931820146580080215665360677655087838043041343105918046068008345911366408348874080057412725867047922583191274157390809143831384564241509408491339180968402511639919368532255573389669537490266209232613188558915808324555719484538756287861288590041060060737465014026278240273469625282171749415823317492396835301361786536737606421667781377399510065895288</pre>
<pre style="height:64ex;white-space: pre-wrap;">3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151557485724245415069595082953311686172785588907509838175463746493931925506040092770167113900984882401285836160356370766010471018194295559619894676783744944825537977472684710404753464620804668425906949129331367702898915210475216205696602405803815019351125338243003558764024749647326391419927260426992279678235478163600934172164121992458631503028618297455570674983850549458858692699569092721079750930295532116534498720275596023648066549911988183479775356636980742654252786255181841757467289097777279380008164706001614524919217321721477235014144197356854816136115735255213347574184946843852332390739414333454776241686251898356948556209921922218427255025425688767179049460165346680498862723279178608578438382796797668145410095388378636095068006422512520511739298489608412848862694560424196528502221066118630674427862203919494504712371378696095636437191728746776465757396241389086583264599581339047802759009946576407895126946839835259570982582262052248940772671947826848260147699090264013639443745530506820349625245174939965143142980919065925093722169646151570985838741059788595977297549893016175392846813826868386894277415599185592524595395943104997252468084598727364469584865383673622262609912460805124388439045124413654976278079771569143599770012961608944169486855584840635342207222582848864815845602850601684273945226746767889525213852254995466672782398645659611635488623057745649803559363456817432411251507606947945109659609402522887971089314566913686722874894056010150330861792868092087476091782493858900971490967598526136554978189312978482168299894872265880485756401427047755513237964145152374623436454285844479526586782105114135473573952311342716610213596953623144295248493718711014576540359027993440374200731057853906219838744780847848968332144571386875194350643021845319104848100537061468067491927819119793995206141966342875444064374512371819217999839101591956181467514269123974894090718649423196156794520809514655022523160388193014209376213785595663893778708303906979207734672218256259966150142150306803844773454920260541466592520149744285073251866600213243408819071048633173464965145390579626856100550810665879699816357473638405257145910289706414011097120628043903975951567715770042033786993600723055876317635942187312514712053292819182618612586732157919841484882916447060957527069572209175671167229109816909152801735067127485832228718352093539657251210835791513698820914442100675103346711031412671113699086585163983150197016515116851714376576183515565088490998985998238734552833163550764791853589322618548963213293308985706420467525907091548141654985946163718027098199430992448895757128289059232332609729971208443357326548938239119325974636673058360414281388303203824903758985243744170291327656180937734440307074692112019130203303801976211011004492932151608424448596376698389522868478312355265821314495768572624334418930396864262434107732269780280731891544110104468232527162010526522721116603966655730925471105578537634668206531098965269186205647693125705863566201855810072936065987648611791045334885034611365768675324944166803962657978771855608455296541266540853061434443185867697514566140680070023787765913440171274947042056223053899456131407112700040785473326993908145466464588079727082668306343285878569830523580893306575740679545716377525420211495576158140025012622859413021647155097925923099079654737612551765675135751782966645477917450112996148903046399471329621073404375189573596145890193897131117904297828564750320319869151402870808599048010941214722131794764777262241425485454033215718530614228813758504306332175182979866223717215916077166925474873898665494945011465406284336639379003976926567214638530673609657120918076383271664162748888007869256029022847210403172118608204190004229661711963779213375751149595015660496318629472654736425230817703675159067350235072835405670403867435136222247715891504953098444893330963408780769325993978054193414473774418426312986080998886874132604721569516239658645730216315981931951673538129741677294786724229246543668009806769282382806899640048243540370141631496589794092432378969070697794223625082216889573837986230015937764716512289357860158816175578297352334460428151262720373431465319777741603199066554187639792933441952154134189948544473456738316249934191318148092777710386387734317720754565453220777092120190516609628049092636019759882816133231666365286193266863360627356763035447762803504507772355471058595487027908143562401451718062464362679456127531813407833033625423278394497538243720583531147711992606381334677687969597030983391307710987040859133746414428227726346594704745878477872019277152807317679077071572134447306057007334924369311383504931631284042512192565179806941135280131470130478164378851852909285452011658393419656213491434159562586586557055269049652098580338507224264829397285847831630577775606888764462482468579260395352773480304802900587607582510474709164396136267604492562742042083208566119062545433721315359584506877246029016187667952406163425225771954291629919306455377991403734043287526288896399587947572917464263574552540790914513571113694109119393251910760208252026187985318877058429725916778131496990090192116971737278476847268608490033770242429165130050051683233643503895170298939223345172201381280696501178440874519601212285993716231301711444846409038906449544400619869075485160263275052983491874078668088183385102283345085048608250393021332197155184306354550076682829493041377655279397517546139539846833936383047461199665385815384205685338621867252334028308711232827892125077126294632295639898989358211674562701021835646220134967151881909730381198004973407239610368540664319395097901906996395524530054505806855019567302292191393391856803449039820595510022635353619204199474553859381023439554495977837790237421617271117236434354394782218185286240851400666044332588856986705431547069657474585503323233421073015459405165537906866273337995851156257843229882737231989875714159578111963583300594087306812160287649628674460477464915995054973742562690104903778198683593814657412680492564879855614537234786733039046883834363465537949864192705638729317487233208376011230299113679386270894387993620162951541337142489283072201269014754668476535761647737946752004907571555278196536213239264061601363581559074220202031872776052772190055614842555187925303435139844253223415762336106425063904975008656271095359194658975141310348227693062474353632569160781547818115284366795706110861533150445212747392454494542368288606134084148637767009612071512491404302725386076482363414334623518975766452164137679690314950191085759844239198629164219399490723623464684411739403265918404437805133389452574239950829659122850855582157250310712570126683024029295252201187267675622041542051618416348475651699981161410100299607838690929160302884002691041407928862150784245167090870006992821206604183718065355672525325675328612910424877618258297651579598470356222629348600341587229805349896502262917487882027342092222453398562647669149055628425039127577102840279980663658254889264880254566101729670266407655904290994568150652653053718294127033693137851786090407086671149655834343476933857817113864558736781230145876871266034891390956200993936103102916161528813843790990423174733639480457593149314052976347574811935670911013775172100803155902485309066920376719220332290943346768514221447737939375170344366199104033751117354719185504644902636551281622882446257591633303910722538374218214088350865739177150968288747826569959957449066175834413752239709683408005355984917541738188399944697486762655165827658483588453142775687900290951702835297163445621296404352311760066510124120065975585127617858382920419748442360800719304576189323492292796501987518721272675079812554709589045563579212210333466974992356302549478024901141952123828153091140790738602515227429958180724716259166854513331239480494707911915326734302824418604142636395480004480026704962482017928964766975831832713142517029692348896276684403232609275249603579964692565049368183609003238092934595889706953653494060340216654437558900456328822505452556405644824651518754711962184439658253375438856909411303150952617937800297412076651479394259029896959469955657612186561967337862362561252163208628692221032748892186543648022967807057656151446320469279068212073883778142335628236089632080682224680122482611771858963814091839036736722208883215137556003727983940041529700287830766709444745601345564172543709069793961225714298946715435784687886144458123145935719849225284716050492212424701412147805734551050080190869960330276347870810817545011930714122339086639383395294257869050764310063835198343893415961318543475464955697810382930971646514384070070736041123735998434522516105070270562352660127648483084076118301305279320542746286540360367453286510570658748822569815793678976697422057505968344086973502014102067235850200724522563265134105592401902742162484391403599895353945909440704691209140938700126456001623742880210927645793106579229552498872758461012648369998922569596881592056001016552563756785667227966198857827948488558343975187445455129656344348039664205579829368043522027709842942325330225763418070394769941597915945300697521482933665556615678736400536665641654732170439035213295435291694145990416087532018683793702348886894791510716378529023452924407736594956305100742108714261349745956151384987137570471017879573104229690666702144986374645952808243694457897723300487647652413390759204340196340391147320233807150952220106825634274716460243354400515212669324934196739770415956837535551667302739007497297363549645332888698440611964961627734495182736955882207573551766515898551909866653935494810688732068599075407923424023009259007017319603622547564789406475483466477604114632339056513433068449539790709030234604614709616968868850140834704054607429586991382966824681857103188790652870366508324319744047718556789348230894310682870272280973624809399627060747264553992539944280811373694338872940630792615959954626246297070625948455690347119729964090894180595343932512362355081349490043642785271383159125689892951964272875739469142725343669415323610045373048819855170659412173524625895487301676002988659257866285612496655235338294287854253404830833070165372285635591525347844598183134112900199920598135220511733658564078264849427644113763938669248031183644536985891754426473998822846218449008777697763127957226726555625962825427653183001340709223343657791601280931794017185985999338492354956400570995585611349802524990669842330173503580440811685526531170995708994273287092584878944364600504108922669178352587078595129834417295351953788553457374260859029081765155780390594640873506123226112009373108048548526357228257682034160504846627750450031262008007998049254853469414697751649327095049346393824322271885159740547021482897111777923761225788734771881968254629812686858170507402725502633290449762778944236216741191862694396506715157795867564823993917604260176338704549901761436412046921823707648878341968968611815581587360629386038101712158552726683008238340465647588040513808016336388742163714064354955618689641122821407533026551004241048967835285882902436709048871181909094945331442182876618103100735477054981596807720094746961343609286148494178501718077930681085469000944589952794243981392135055864221964834915126390128038320010977386806628779239718014613432445726400973742570073592100315415089367930081699805365202760072774967458400283624053460372634165542590276018348403068113818551059797056640075094260878857357960373245141467867036880988060971642584975951380693094494015154222219432913021739125383559150310033303251117491569691745027149433151558854039221640972291011290355218157628232831823425483261119128009282525619020526301639114772473314857391077758744253876117465786711694147764214411112635835538713610110232679877564102468240322648346417663698066378576813492045302240819727856471983963087815432211669122464159117767322532643356861461865452226812688726844596844241610785401676814208088502800541436131462308210259417375623899420757136275167457318918945628352570441335437585753426986994725470316566139919996826282472706413362221789239031760854289437339356188916512504244040089527198378738648058472689546243882343751788520143956005710481194988423906061369573423155907967034614914344788636041031823507365027785908975782727313050488939890099239135033732508559826558670892426124294736701939077271307068691709264625484232407485503660801360466895118400936686095463250021458529309500009071510582362672932645373821049387249966993394246855164832611341461106802674466373343753407642940266829738652209357016263846485285149036293201991996882851718395366913452224447080459239660281715655156566611135982311225062890585491450971575539002439315351909021071194573002438801766150352708626025378817975194780610137150044899172100222013350131060163915415895780371177927752259787428919179155224171895853616805947412341933984202187456492564434623925319531351033114763949119950728584306583619353693296992898379149419394060857248639688369032655643642166442576079147108699843157337496488352927693282207629472823815374099615455987982598910937171262182830258481123890119682214294576675807186538065064870261338928229949725745303328389638184394477077940228435988341003583854238973542439564755568409522484455413923941000162076936368467764130178196593799715574685419463348937484391297423914336593604100352343777065888677811394986164787471407932638587386247328896456435987746676384794665040741118256583788784548581489629612739984134427260860618724554523606431537101127468097787044640947582803487697589483282412392929605829486191966709189580898332012103184303401284951162035342801441276172858302435598300320420245120728725355811958401491809692533950757784000674655260314461670508276827722235341911026341631571474061238504258459884199076112872580591139356896014316682831763235673254170734208173322304629879928049085140947903688786878949305469557030726190095020764334933591060245450864536289354568629585313153371838682656178622736371697577418302398600659148161640494496501173213138957470620884748023653710311508984279927544268532779743113951435741722197597993596852522857452637962896126915723579866205734083757668738842664059909935050008133754324546359675048442352848747014435454195762584735642161981340734685411176688311865448937769795665172796623267148103386439137518659467300244345005449953997423723287124948347060440634716063258306498297955101095418362350303094530973358344628394763047756450150085075789495489313939448992161255255977014368589435858775263796255970816776438001254365023714127834679261019955852247172201777237004178084194239487254068015560359983905489857235467456423905858502167190313952629445543913166313453089390620467843877850542393905247313620129476918749751910114723152893267725339181466073000890277689631148109022097245207591672970078505807171863810549679731001678708506942070922329080703832634534520380278609905569001341371823683709919495164896007550493412678764367463849020639640197666855923356546391383631857456981471962108410809618846054560390384553437291414465134749407848844237721751543342603066988317683310011331086904219390310801437843341513709243530136776310849135161564226984750743032971674696406665315270353254671126675224605511995818319637637076179919192035795820075956053023462677579439363074630569010801149427141009391369138107258137813578940055995001835425118417213605572752210352680373572652792241737360575112788721819084490061780138897107708229310027976659358387589093956881485602632243937265624727760378908144588378550197028437793624078250527048758164703245812908783952324532378960298416692254896497156069811921865849267704039564812781021799132174163058105545988013004845629976511212415363745150056350701278159267142413421033015661653560247338078430286552572227530499988370153487930080626018096238151613669033411113865385109193673938352293458883225508870645075394739520439680790670868064450969865488016828743437861264538158342807530618454859037982179945996811544197425363443996029025100158882721647450068207041937615845471231834600726293395505482395571372568402322682130124767945226448209102356477527230820810635188991526928891084555711266039650343978962782500161101532351605196559042118449499077899920073294769058685778787209829013529566139788848605097860859570177312981553149516814671769597609942100361835591387778176984587581044662839988060061622984861693533738657877359833616133841338536842119789389001852956919678045544828584837011709672125353387586215823101331038776682721157269495181795897546939926421979155233857662316762754757035469941489290413018638611943919628388705436777432242768091323654494853667680000010652624854730558615989991401707698385483188750142938908995068545307651168033373222651756622075269517914422528081651716677667279303548515420402381746089232839170327542575086765511785939500279338959205766827896776445318404041855401043513483895312013263783692835808271937831265496174599705674507183320650345566440344904536275600112501843356073612227659492783937064784264567633881880756561216896050416113903906396016202215368494109260538768871483798955999911209916464644119185682770045742434340216722764455893301277815868695250694993646101756850601671453543158148010545886056455013320375864548584032402987170934809105562116715468484778039447569798042631809917564228098739987669732376957370158080682290459921236616890259627304306793165311494017647376938735140933618332161428021497633991898354848756252987524238730775595559554651963944018218409984124898262367377146722606163364329640633572810707887581640438148501884114318859882769449011932129682715888413386943468285900666408063140777577257056307294004929403024204984165654797367054855804458657202276378404668233798528271057843197535417950113472736257740802134768260450228515797957976474670228409995616015691089038458245026792659420555039587922981852648007068376504183656209455543461351341525700659748819163413595567196496540321872716026485930490397874895890661272507948282769389535217536218507962977851461884327192232238101587444505286652380225328438913752738458923844225354726530981715784478342158223270206902872323300538621634798850946954720047952311201504329322662827276321779088400878614802214753765781058197022263097174950721272484794781695729614236585957820908307332335603484653187302930266596450137183754288975579714499246540386817992138934692447419850973346267933210726868707680626399193619650440995421676278409146698569257150743157407938053239252394775574415918458215625181921552337096074833292349210345146264374498055961033079941453477845746999921285999993996122816152193148887693880222810830019860165494165426169685867883726095877456761825072759929508931805218729246108676399589161458550583972742098090978172932393010676638682404011130402470073508578287246271349463685318154696904669686939254725194139929146524238577625500474852954768147954670070503479995888676950161249722820403039954632788306959762493615101024365553522306906129493885990157346610237122354789112925476961760050479749280607212680392269110277722610254414922157650450812067717357120271802429681062037765788371669091094180744878140490755178203856539099104775941413215432844062503018027571696508209642734841469572639788425600845312140659358090412711359200419759851362547961606322887361813673732445060792441176399759746193835845749159880976674470930065463424234606342374746660804317012600520559284936959414340814685298150539471789004518357551541252235905906872648786357525419112888773717663748602766063496035367947026923229718683277173932361920077745221262475186983349515101986426988784717193966497690708252174233656627259284406204302141137199227852699846988477023238238400556555178890876613601304770984386116870523105531491625172837327286760072481729876375698163354150746088386636406934704372066886512756882661497307886570156850169186474885416791545965072342877306998537139043002665307839877638503238182155355973235306860430106757608389086270498418885951380910304235957824951439885901131858358406674723702971497850841458530857813391562707603563907639473114554958322669457024941398316343323789759556808568362972538679132750555425244919435891284050452269538121791319145135009938463117740179715122837854601160359554028644059024964669307077690554810288502080858008781157738171917417760173307385547580060560143377432990127286772530431825197579167929699650414607066457125888346979796429316229655201687973000356463045793088403274807718115553309098870255052076804630346086581653948769519600440848206596737947316808641564565053004988161649057883115434548505266006982309315777650037807046612647060214575057932709620478256152471459189652236083966456241051955105223572397395128818164059785914279148165426328920042816091369377737222999833270820829699557377273756676155271139225880552018988762011416800546873655806334716037342917039079863965229613128017826797172898229360702880690877686605932527463784053976918480820410219447197138692560841624511239806201131845412447820501107987607171556831540788654390412108730324020106853419472304766667217498698685470767812051247367924791931508564447753798537997322344561227858432968466475133365736923872014647236794278700425032555899268843495928761240075587569464137056251400117971331662071537154360068764773186755871487839890810742953094106059694431584775397009439883949144323536685392099468796450665339857388878661476294434140104988899316005120767810358861166020296119363968213496075011164983278563531614516845769568710900299976984126326650234771672865737857908574664607722834154031144152941880478254387617707904300015669867767957609099669360755949651527363498118964130433116627747123388174060373174397054067031096767657486953587896700319258662594105105335843846560233917967492678447637084749783336555790073841914731988627135259546251816043422537299628632674968240580602964211463864368642247248872834341704415734824818333016405669596688667695634914163284264149745333499994800026699875888159350735781519588990053951208535103572613736403436753471410483601754648830040784641674521673719048310967671134434948192626811107399482506073949507350316901973185211955263563258433909982249862406703107683184466072912487475403161796994113973877658998685541703188477886759290260700432126661791922352093822787888098863359911608192353555704646349113208591897961327913197564909760001399623444553501434642686046449586247690943470482932941404111465409239883444351591332010773944111840741076849810663472410482393582740194493566516108846312567852977697346843030614624180358529331597345830384554103370109167677637427621021370135485445092630719011473184857492331816720721372793556795284439254815609137281284063330393735624200160456645574145881660521666087387480472433912129558777639069690370788285277538940524607584962315743691711317613478388271941686066257210368513215664780014767523103935786068961112599602818393095487090590738613519145918195102973278755710497290114871718971800469616977700179139196137914171627070189584692143436967629274591099400600849835684252019155937037010110497473394938778859894174330317853487076032219829705797511914405109942358830345463534923498268836240433272674155403016195056806541809394099820206099941402168909007082133072308966211977553066591881411915778362729274615618571037217247100952142369648308641025928874579993223749551912219519034244523075351338068568073544649951272031744871954039761073080602699062580760202927314552520780799141842906388443734996814582733720726639176702011830046481900024130835088465841521489912761065137415394356572113903285749187690944137020905170314877734616528798482353382972601361109845148418238081205409961252745808810994869722161285248974255555160763716750548961730168096138038119143611439921063800508321409876045993093248510251682944672606661381517457125597549535802399831469822036133808284993567055755247129027453977621404931820146580080215665360677655087838043041343105918046068008345911366408348874080057412725867047922583191274157390809143831384564241509408491339180968402511639919368532255573389669537490266209232613188558915808324555719484538756287861288590041060060737465014026278240273469625282171749415823317492396835301361786536737606421667781377399510065895288</pre>
Line 372: Line 372:
Thanks for Velthuis BigIntegers library[https://github.com/rvelthuis/DelphiBigNumbers].
Thanks for Velthuis BigIntegers library[https://github.com/rvelthuis/DelphiBigNumbers].
{{Trans|C#}}
{{Trans|C#}}
<lang Delphi>
<syntaxhighlight lang=Delphi>
program Calculate_Pi;
program Calculate_Pi;


Line 482: Line 482:
readln;
readln;
end.
end.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 490: Line 490:
=={{header|Erlang}}==
=={{header|Erlang}}==
{{trans|python}}
{{trans|python}}
<lang erlang>
<syntaxhighlight lang=erlang>
-module(pi).
-module(pi).
-export([agmPi/1, agmPiBody/5]).
-export([agmPi/1, agmPiBody/5]).
Line 512: Line 512:
Step_difference = A_n_plus_one - A,
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).
agmPiBody(Loops-1, Running_divisor-(math:pow(Step_difference, 2)*J), A_n_plus_one, B_n_plus_one, J+J).
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 522: Line 522:
{{libheader|iso_fortran_env}}
{{libheader|iso_fortran_env}}
{{trans|Julia}}
{{trans|Julia}}
<lang fortran>program CalcPi
<syntaxhighlight lang=fortran>program CalcPi
! Use real128 numbers: (append '_rf')
! Use real128 numbers: (append '_rf')
use iso_fortran_env, only: rf => real128
use iso_fortran_env, only: rf => real128
Line 560: Line 560:
end subroutine
end subroutine
end program CalcPi
end program CalcPi
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 571: Line 571:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang=go>package main


import (
import (
Line 609: Line 609:
pi := t.Quo(t, u.Sub(one, sum))
pi := t.Quo(t, u.Sub(one, sum))
fmt.Println(pi)
fmt.Println(pi)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 618: Line 618:
=={{header|Groovy}}==
=={{header|Groovy}}==
{{trans|Java}}
{{trans|Java}}
<lang groovy>import java.math.MathContext
<syntaxhighlight lang=groovy>import java.math.MathContext


class CalculatePi {
class CalculatePi {
Line 651: Line 651:
System.out.println(pi)
System.out.println(pi)
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201065485862824</pre>
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201065485862824</pre>
Line 658: Line 658:
{{libheader|MPFR}}
{{libheader|MPFR}}
{{libheader|hmpfr}}
{{libheader|hmpfr}}
<lang Haskell>import Prelude hiding (pi)
<syntaxhighlight lang=Haskell>import Prelude hiding (pi)
import Data.Number.MPFR hiding (sqrt, pi, div)
import Data.Number.MPFR hiding (sqrt, pi, div)
import Data.Number.MPFR.Instances.Near ()
import Data.Number.MPFR.Instances.Near ()
Line 690: Line 690:
main = do
main = do
-- The last decimal is rounded.
-- The last decimal is rounded.
putStrLn $ toString 1000 $ pi 1000</lang>
putStrLn $ toString 1000 $ pi 1000</syntaxhighlight>
{{out}}
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420199</pre>
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420199</pre>
Line 698: Line 698:
[https://code.jsoftware.com/wiki/Essays/Extended%20Precision%20Functions Extended precision functions] and [https://code.jsoftware.com/wiki/Essays/Chudnovsky_Algorithm Pi].
[https://code.jsoftware.com/wiki/Essays/Extended%20Precision%20Functions Extended precision functions] and [https://code.jsoftware.com/wiki/Essays/Chudnovsky_Algorithm Pi].
Translated from python:
Translated from python:
<lang J>DP=: 100
<syntaxhighlight lang=J>DP=: 100


round=: DP&$: : (4 : 0)
round=: DP&$: : (4 : 0)
Line 722: Line 722:
end.
end.
PI
PI
)</lang>
)</syntaxhighlight>
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.
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''
<pre> pi''
Line 745: Line 745:
1583455951826865080542496790424338362837978447536228171662934224565463064033895909488933268392567279887495006936541219489670405121434573776487989539520749180843985094860051126840117004097133550161882511486508109869673199973040182062140382647367514024790194...</pre>
1583455951826865080542496790424338362837978447536228171662934224565463064033895909488933268392567279887495006936541219489670405121434573776487989539520749180843985094860051126840117004097133550161882511486508109869673199973040182062140382647367514024790194...</pre>


That said, note that J offers a more direct approach here:<lang J> 102j100":<.@o.&.(*&(10^100x))1
That said, note that J offers a more direct approach here:<syntaxhighlight lang=J> 102j100":<.@o.&.(*&(10^100x))1
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679</lang>
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:<lang J> 113j111":<.@o.&.(*&(10^111x))1
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</lang>
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.)
(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.)
Line 756: Line 756:
{{trans|Kotlin}}
{{trans|Kotlin}}
Used features of Java 8
Used features of Java 8
<lang Java>import java.math.BigDecimal;
<syntaxhighlight lang=Java>import java.math.BigDecimal;
import java.math.MathContext;
import java.math.MathContext;
import java.util.Objects;
import java.util.Objects;
Line 791: Line 791:
System.out.println(pi);
System.out.println(pi);
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201065485862824</pre>
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201065485862824</pre>
Line 803: Line 803:
`rsqrt` function
`rsqrt` function
for taking square roots of rationals; such a module can be found at [[Arithmetic/Rational]].
for taking square roots of rationals; such a module can be found at [[Arithmetic/Rational]].
<lang jq># include "rational"; # a reminder
<syntaxhighlight lang=jq># include "rational"; # a reminder


def pi(precision):
def pi(precision):
Line 822: Line 822:
| r_to_decimal(precision);
| r_to_decimal(precision);


pi(500)</lang>
pi(500)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 830: Line 830:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Printf
<syntaxhighlight lang=julia>using Printf


agm1step(x, y) = (x + y) / 2, sqrt(x * y)
agm1step(x, y) = (x + y) / 2, sqrt(x * y)
Line 860: Line 860:


testmakepi()
testmakepi()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Approximating π using 512-bit floats.
Approximating π using 512-bit floats.
Line 879: Line 879:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>import java.math.BigDecimal
<syntaxhighlight lang=scala>import java.math.BigDecimal
import java.math.MathContext
import java.math.MathContext


Line 911: Line 911:
val pi = (bigFour * a * a).divide(BigDecimal.ONE - sum, con1024)
val pi = (bigFour * a * a).divide(BigDecimal.ONE - sum, con1024)
println(pi)
println(pi)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201065485862824</pre>
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201065485862824</pre>
Line 919: Line 919:
With 16 steps we have 89415 correct digits:
With 16 steps we have 89415 correct digits:


<lang maple>agm:=proc(n)
<syntaxhighlight lang=maple>agm:=proc(n)
local a:=1,g:=evalf(sqrt(1/2)),s:=0,p:=4,i;
local a:=1,g:=evalf(sqrt(1/2)),s:=0,p:=4,i;
for i to n do
for i to n do
Line 932: Line 932:
d:=agm(16)-evalf(Pi):
d:=agm(16)-evalf(Pi):
evalf[10](d);
evalf[10](d);
# 4.280696926e-89415</lang>
# 4.280696926e-89415</syntaxhighlight>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang mathematica>pi[n_, prec_] :=
<syntaxhighlight lang=mathematica>pi[n_, prec_] :=
Module[{a = 1, g = N[1/Sqrt[2], prec], k, s = 0, p = 4},
Module[{a = 1, g = N[1/Sqrt[2], prec], k, s = 0, p = 4},
For[k = 1, k < n, k++,
For[k = 1, k < n, k++,
Line 946: Line 946:


pi[7, 100]
pi[7, 100]
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628046852228654</lang>
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628046852228654</syntaxhighlight>


=={{header|МК-61/52}}==
=={{header|МК-61/52}}==
Line 954: Line 954:
П5 ИП1 - x^2 ИП4 * - П3 ИП1 ИП2
П5 ИП1 - x^2 ИП4 * - П3 ИП1 ИП2
* КвКор П2 ИП5 П1 КИП4 L0 14 ИП1 x^2
* КвКор П2 ИП5 П1 КИП4 L0 14 ИП1 x^2
ИП3 / С/П</lang>
ИП3 / С/П</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
{{libheader|bignum}}
{{libheader|bignum}}
{{Trans|Delphy}}
{{Trans|Delphy}}
<lang Nim>from math import sqrt
<syntaxhighlight lang=Nim>from math import sqrt
import times
import times


Line 1,049: Line 1,049:
echo "Computed ", d, " digits in ", timeStr
echo "Computed ", d, " digits in ", timeStr


echo "π = ", compress(piStr, 20), "..."</lang>
echo "π = ", compress(piStr, 20), "..."</syntaxhighlight>


{{out}}
{{out}}
Line 1,057: Line 1,057:
=={{header|OCaml}}==
=={{header|OCaml}}==
program for calculating digits of pi
program for calculating digits of pi
<lang OCaml>let limit = 10000 and n = 2800
<syntaxhighlight lang=OCaml>let limit = 10000 and n = 2800
let x = Array.make (n+1) 2000
let x = Array.make (n+1) 2000


Line 1,075: Line 1,075:
f n 0;
f n 0;
print_newline()
print_newline()
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185</pre>
<pre>31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185</pre>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<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
<syntaxhighlight 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)</lang>
pi(6)</syntaxhighlight>
{{out}}
{{out}}
<pre>%1 = 3.1415926535897932384626433832795028841971693993751058209749445923078164062878</pre>
<pre>%1 = 3.1415926535897932384626433832795028841971693993751058209749445923078164062878</pre>
Line 1,090: Line 1,090:
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.
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.


<lang perl>use Math::BigFloat try => "GMP,Pari";
<syntaxhighlight lang=perl>use Math::BigFloat try => "GMP,Pari";


my $digits = shift || 100; # Get number of digits from command line
my $digits = shift || 100; # Get number of digits from command line
Line 1,112: Line 1,112:
$an->bmul($an,$acc)->bdiv(4*$tn, $digits);
$an->bmul($an,$acc)->bdiv(4*$tn, $digits);
return $an;
return $an;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068</pre>
<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.
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}}
{{trans|Ruby}}
<lang perl>use strict;
<syntaxhighlight lang=perl>use strict;
use warnings;
use warnings;
use Math::BigFloat;
use Math::BigFloat;
Line 1,133: Line 1,133:
($a, $g) = @$x;
($a, $g) = @$x;
}
}
print $a * $a / $z, "\n";</lang>
print $a * $a / $z, "\n";</syntaxhighlight>
{{out}}
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067</pre>
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067</pre>
Line 1,140: Line 1,140:
{{libheader|Phix/mpfr}}
{{libheader|Phix/mpfr}}
{{trans|Python}}
{{trans|Python}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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>
Line 1,188: Line 1,188:
<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: #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>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,204: Line 1,204:
=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
{{trans|Python}}
{{trans|Python}}
<lang PicoLisp>(scl 40)
<syntaxhighlight lang=PicoLisp>(scl 40)


(de pi ()
(de pi ()
Line 1,224: Line 1,224:
(println (pi))
(println (pi))
(bye)</lang>
(bye)</syntaxhighlight>
{{out}}
{{out}}
<pre>"3.1415926535897932384626433832795028841841"</pre>
<pre>"3.1415926535897932384626433832795028841841"</pre>
Line 1,230: Line 1,230:
=={{header|Python}}==
=={{header|Python}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang python>from decimal import *
<syntaxhighlight lang=python>from decimal import *


D = Decimal
D = Decimal
Line 1,242: Line 1,242:
n += n
n += n
a, g = x
a, g = x
print(a * a / z)</lang>
print(a * a / z)</syntaxhighlight>


{{out}}
{{out}}
Line 1,251: Line 1,251:
{{libheader|Rmpfr}}
{{libheader|Rmpfr}}


<lang rsplus>library(Rmpfr)
<syntaxhighlight lang=rsplus>library(Rmpfr)


agm <- function(n, prec) {
agm <- function(n, prec) {
Line 1,283: Line 1,283:
f <- file("pi.txt", "w")
f <- file("pi.txt", "w")
writeLines(formatMpfr(x, 1e6), f)
writeLines(formatMpfr(x, 1e6), f)
close(f)</lang>
close(f)</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang Racket>#lang racket
<syntaxhighlight lang=Racket>#lang racket
(require math/bigfloat)
(require math/bigfloat)


Line 1,309: Line 1,309:
(parameterize ([bf-precision 200])
(parameterize ([bf-precision 200])
(displayln (bigfloat->string (pi/a-g 6)))
(displayln (bigfloat->string (pi/a-g 6)))
(displayln (bigfloat->string pi.bf)))</lang>
(displayln (bigfloat->string pi.bf)))</syntaxhighlight>
{{Out}}
{{Out}}
<pre>3.1415926535897932384626433832793
<pre>3.1415926535897932384626433832793
Line 1,334: Line 1,334:


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.
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.
<lang perl6>constant number-of-decimals = 100;
<syntaxhighlight lang=perl6>constant number-of-decimals = 100;


multi sqrt(Int $n) {
multi sqrt(Int $n) {
Line 1,360: Line 1,360:
say ($a ** 2 / $z).substr: 0, 2 + number-of-decimals;
say ($a ** 2 / $z).substr: 0, 2 + number-of-decimals;
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>3.1876726427121086272019299705253692326510535718593692264876339862751228325281223301147286106601617972
<pre>3.1876726427121086272019299705253692326510535718593692264876339862751228325281223301147286106601617972
Line 1,381: Line 1,381:
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===
===version 1===
<lang rexx>/*REXX program calculates the value of pi using the AGM algorithm. */
<syntaxhighlight lang=rexx>/*REXX program calculates the value of pi using the AGM algorithm. */
parse arg d .; if d=='' | d=="," then d= 500 /*D not specified? Then use default. */
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*/
numeric digits d+5 /*set the numeric decimal digits to D+5*/
Line 1,400: Line 1,400:
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
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*/
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</lang>
numeric digits d; return g/1</syntaxhighlight>
Programming note: &nbsp; the &nbsp; '''sqrt''' &nbsp; subroutine (above) is optimized for larger ''digits''.
Programming note: &nbsp; the &nbsp; '''sqrt''' &nbsp; subroutine (above) is optimized for larger ''digits''.


Line 1,413: Line 1,413:


For 1,005 decimal digits, &nbsp; it is over &nbsp; '''68''' &nbsp; times faster than version 3.
For 1,005 decimal digits, &nbsp; it is over &nbsp; '''68''' &nbsp; times faster than version 3.
<lang rexx>/*REXX program calculates the AGM (arithmetic─geometric mean) of two (real) numbers. */
<syntaxhighlight lang=rexx>/*REXX program calculates the AGM (arithmetic─geometric mean) of two (real) numbers. */
parse arg a b digs . /*obtain optional numbers from the C.L.*/
parse arg a b digs . /*obtain optional numbers from the C.L.*/
if digs=='' | digs=="," then digs= 100 /*No DIGS specified? Then use default.*/
if digs=='' | digs=="," then digs= 100 /*No DIGS specified? Then use default.*/
Line 1,439: Line 1,439:
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_ % 2
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 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</lang>
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>}}
{{out|output|text=&nbsp; when using the default number of digits: &nbsp; &nbsp; <tt> 500 </tt>}}
<pre>
<pre>
Line 1,480: Line 1,480:


===version 3===
===version 3===
<lang rexx>
<syntaxhighlight lang=rexx>
/*REXX*/
/*REXX*/
Line 1,530: Line 1,530:
End
End
Numeric Digits xprec
Numeric Digits xprec
Return (r+0)</lang>
Return (r+0)</syntaxhighlight>
[{out}]
[{out}]
<pre>10 3.141592654
<pre>10 3.141592654
Line 1,546: Line 1,546:
Using agm.
Using agm.
See [[Talk:Arithmetic-geometric mean]]
See [[Talk:Arithmetic-geometric mean]]
<lang ruby># Calculate Pi using the Arithmetic Geometric Mean of 1 and 1/sqrt(2)
<syntaxhighlight lang=ruby># Calculate Pi using the Arithmetic Geometric Mean of 1 and 1/sqrt(2)
#
#
#
#
Line 1,565: Line 1,565:
g = x[1]
g = x[1]
}
}
puts a * a / z</lang>
puts a * a / z</syntaxhighlight>
Produces:
Produces:
<pre>
<pre>
Line 1,602: Line 1,602:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>/// calculate pi with algebraic/geometric mean
<syntaxhighlight lang=rust>/// calculate pi with algebraic/geometric mean
pub fn pi(n: usize) -> f64 {
pub fn pi(n: usize) -> f64 {
let mut a : f64 = 1.0;
let mut a : f64 = 1.0;
Line 1,623: Line 1,623:
4.0 * a.powi(2) / (1.0-s)
4.0 * a.powi(2) / (1.0-s)
}
}
</syntaxhighlight>
</lang>
Can be invoked like:
Can be invoked like:
<lang rust>
<syntaxhighlight lang=rust>
fn main() {
fn main() {
println!("pi(7): {}", pi(7));
println!("pi(7): {}", pi(7));
}
}
</syntaxhighlight>
</lang>
Outputs:
Outputs:
<pre>pi(7): 3.1415926535901733</pre>
<pre>pi(7): 3.1415926535901733</pre>
Line 1,637: Line 1,637:
=={{header|Scala}}==
=={{header|Scala}}==
===Completely (tail) recursive===
===Completely (tail) recursive===
<lang Scala>import java.math.MathContext
<syntaxhighlight lang=Scala>import java.math.MathContext


import scala.annotation.tailrec
import scala.annotation.tailrec
Line 1,672: Line 1,672:


println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]")
println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]")
}</lang>
}</syntaxhighlight>
{{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.
{{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}}==
=={{header|Sidef}}==
<lang ruby>func agm_pi(digits) {
<syntaxhighlight lang=ruby>func agm_pi(digits) {
var acc = (digits + 8);
var acc = (digits + 8);


Line 1,698: Line 1,698:
}
}


say agm_pi(100);</lang>
say agm_pi(100);</syntaxhighlight>
{{out}}
{{out}}
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068</pre>
<pre>3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068</pre>
Line 1,705: Line 1,705:
{{trans|Ruby}}
{{trans|Ruby}}
{{tcllib|math::bigfloat}}
{{tcllib|math::bigfloat}}
<lang tcl>package require math::bigfloat
<syntaxhighlight lang=tcl>package require math::bigfloat
namespace import math::bigfloat::*
namespace import math::bigfloat::*


Line 1,727: Line 1,727:
}
}


puts [agm/π 17]</lang>
puts [agm/π 17]</syntaxhighlight>
{{out}}
{{out}}
<small>(with added line breaks for clarity)</small>
<small>(with added line breaks for clarity)</small>
Line 1,735: Line 1,735:
{{trans|C#}}
{{trans|C#}}
{{Libheader|System.Numerics}}
{{Libheader|System.Numerics}}
<lang vbnet>Imports System, System.Numerics
<syntaxhighlight lang=vbnet>Imports System, System.Numerics


Module Program
Module Program
Line 1,789: Line 1,789:
End Sub
End Sub
End Module
End Module
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre style="height:64ex;white-space: pre-wrap;">Computation time: 4.1539 seconds
<pre style="height:64ex;white-space: pre-wrap;">Computation time: 4.1539 seconds
Line 1,797: Line 1,797:
{{trans|Sidef}}
{{trans|Sidef}}
{{libheader|Wren-big}}
{{libheader|Wren-big}}
<lang ecmascript>import "/big" for BigRat
<syntaxhighlight lang=ecmascript>import "/big" for BigRat


var digits = 500
var digits = 500
Line 1,813: Line 1,813:
}
}
var pi = (an + bn).square / (tn * 4)
var pi = (an + bn).square / (tn * 4)
System.print(pi.toDecimal(digits, false))</lang>
System.print(pi.toDecimal(digits, false))</syntaxhighlight>


{{out}}
{{out}}