Arithmetic-geometric mean: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 22:
=={{header|11l}}==
{{trans|Python}}
<langsyntaxhighlight lang=11l>F agm(a0, g0, tolerance = 1e-10)
V an = (a0 + g0) / 2.0
V gn = sqrt(a0 * g0)
Line 29:
R an
print(agm(1, 1 / sqrt(2)))</langsyntaxhighlight>
{{out}}
<pre>0.847213</pre>
Line 35:
=={{header|360 Assembly}}==
For maximum compatibility, this program uses only the basic instruction set.
<langsyntaxhighlight lang=360asm>AGM CSECT
USING AGM,R13
SAVEAREA B STM-SAVEAREA(R15)
Line 125:
LTORG
YREGS
END AGM</langsyntaxhighlight>
{{out}}
<pre>
Line 132:
 
=={{header|8th}}==
<langsyntaxhighlight lang=8th>: epsilon 1.0e-12 ;
 
with: n
Line 146:
;with
bye
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 155:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<langsyntaxhighlight lang=Action!>INCLUDE "H6:REALMATH.ACT"
 
PROC Agm(REAL POINTER a0,g0,result)
Line 191:
Print(",") PrintR(g)
Print(")=") PrintRE(res)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Arithmetic-geometric_mean.png Screenshot from Atari 8-bit computer]
Line 199:
 
=={{header|Ada}}==
<langsyntaxhighlight lang=Ada>with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions;
 
procedure Arith_Geom_Mean is
Line 224:
begin
N_IO.Put(AGM(1.0, 1.0/Math.Sqrt(2.0)), Fore => 1, Aft => 17, Exp => 0);
end Arith_Geom_Mean;</langsyntaxhighlight>
 
Output:<pre>0.84721308479397909</pre>
Line 232:
 
Printing out the difference between the means at each iteration nicely demonstrates the quadratic convergence.
<langsyntaxhighlight lang=algol68>
BEGIN
PROC agm = (LONG REAL x, y) LONG REAL :
Line 256:
printf (($l(-35,33)l$, agm (LONG 1.0, LONG 1.0 / long sqrt (LONG 2.0))))
END
</syntaxhighlight>
</lang>
Output:<pre>+1.707106781186547524400844362e +0 +2.928932188134524755991556379e -1
+2.928932188134524755991556379e -1 +1.265697533955921916929670477e -2
Line 269:
 
=={{header|APL}}==
<langsyntaxhighlight lang=APL>
agd←{(⍺-⍵)<10*¯8:⍺⋄((⍺+⍵)÷2)∇(⍺×⍵)*÷2}
1 agd ÷2*÷2
</syntaxhighlight>
</lang>
Output: <pre>0.8472130848</pre>
 
Line 278:
By functional composition:
 
<langsyntaxhighlight lang=AppleScript>-- ARITHMETIC GEOMETRIC MEAN -------------------------------------------------
 
property tolerance : 1.0E-5
Line 336:
end script
end if
end mReturn</langsyntaxhighlight>
{{Out}}
<pre>0.847213084835</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang=AHK>agm(a, g, tolerance=1.0e-15){
While abs(a-g) > tolerance
{
Line 351:
}
SetFormat, FloatFast, 0.15
MsgBox % agm(1, 1/sqrt(2))</langsyntaxhighlight>
Output:
<pre>0.847213084793979</pre>
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>#!/usr/bin/awk -f
BEGIN {
printf "%.16g\n", agm(1.0,sqrt(0.5))
Line 372:
return (x<0 ? -x : x)
}
</syntaxhighlight>
</lang>
Output
<pre>0.8472130847939792</pre>
Line 379:
==={{header|BASIC}}===
{{works with|QBasic}}
<langsyntaxhighlight lang=qbasic>PRINT AGM(1, 1 / SQR(2))
END
 
Line 390:
AGM = a
END FUNCTION</langsyntaxhighlight>
{{out}}
<pre>
Line 397:
 
==={{header|BASIC256}}===
<langsyntaxhighlight lang=BASIC256>print AGM(1, 1 / sqr(2))
end
 
Line 410:
 
return a
end function</langsyntaxhighlight>
{{out}}
<pre>
Line 417:
 
==={{header|Commodore BASIC}}===
<langsyntaxhighlight lang=commodorebasic>10 A = 1
20 G = 1/SQR(2)
30 GOSUB 100
Line 426:
120 G = SQR(TA*G)
130 IF A<TA THEN 100
140 RETURN</langsyntaxhighlight>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang=bbcbasic> *FLOAT 64
@% = &1010
PRINT FNagm(1, 1/SQR(2))
Line 443:
UNTIL a = ta
= a
</syntaxhighlight>
</lang>
Produces this output:
<pre>
Line 450:
 
==={{header|GW-BASIC}}===
<langsyntaxhighlight lang=gwbasic>10 A = 1
20 G = 1!/SQR(2!)
30 FOR I=1 TO 20 'twenty iterations is plenty
Line 457:
60 A = B
70 NEXT I
80 PRINT A</langsyntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight lang=IS-BASIC>100 PRINT AGM(1,1/SQR(2))
110 DEF AGM(A,G)
120 DO
Line 467:
150 LOOP UNTIL A=TA
160 LET AGM=A
170 END DEF</langsyntaxhighlight>
 
==={{header|True BASIC}}===
{{works with|QBasic}}
<langsyntaxhighlight lang=qbasic>FUNCTION AGM (a, g)
DO
LET ta = (a + g) / 2
Line 485:
 
PRINT AGM(1, 1 / SQR(2))
END</langsyntaxhighlight>
{{out}}
<pre>
Line 492:
 
=={{header|bc}}==
<langsyntaxhighlight lang=bc>/* Calculate the arithmethic-geometric mean of two positive
* numbers x and y.
* Result will have d digits after the decimal point.
Line 516:
 
scale = 20
m(1, 1 / sqrt(2), 20)</langsyntaxhighlight>
 
{{Out}}
Line 522:
 
=={{header|BQN}}==
<langsyntaxhighlight lang=bqn>AGM ← {
(|𝕨-𝕩) ≤ 1e¯15? 𝕨;
(0.5×𝕨+𝕩) 𝕊 √𝕨×𝕩
}
 
1 AGM 1÷√2</langsyntaxhighlight>
{{out}}
<pre>0.8472130847939792</pre>
Line 533:
=={{header|C}}==
===Basic===
<langsyntaxhighlight lang=c>#include<math.h>
#include<stdio.h>
#include<stdlib.h>
Line 565:
return 0;
}
</syntaxhighlight>
</lang>
 
Original output:
Line 579:
 
===GMP===
<langsyntaxhighlight lang=cpp>/*Arithmetic Geometric Mean of 1 and 1/sqrt(2)
 
Nigel_Galloway
Line 612:
 
return 0;
}</langsyntaxhighlight>
 
The first couple of iterations produces:
Line 628:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang=csharp>namespace RosettaCode.ArithmeticGeometricMean
{
using System;
Line 705:
}
}
}</langsyntaxhighlight>
Output:
<pre>0.847213084835193</pre>
Note that the last 5 digits are spurious, as ''maximumRelativeDifference'' was only specified to be 1e-5. Using 1e-11 instead will give the result 0.847213084793979, which is as far as ''double'' can take it.
===Using Decimal Type===
<langsyntaxhighlight lang=csharp>using System;
 
class Program {
Line 727:
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
}
}</langsyntaxhighlight>
{{Out}}
<pre>0.8472130847939790866064991235</pre>
Line 734:
{{Libheader|System.Numerics}}
Even though the System.Numerics library directly supports only '''BigInteger''' (and not big rationals or big floating point numbers), it can be coerced into making this calculation. One just has to keep track of the decimal place and multiply by a very large constant.
<langsyntaxhighlight lang=csharp>using static System.Math;
using static System.Console;
using BI = System.Numerics.BigInteger;
Line 763:
WriteLine("0.{0}", CalcByAGM(digits));
if (System.Diagnostics.Debugger.IsAttached) ReadKey(); }
}</langsyntaxhighlight>
{{out}}
<pre style="height:64ex; overflow:scroll; white-space:pre-wrap;">0.8472130847939790866064991234821916364814459103269421850605793726597340048341347597232002939946112299421222856252334109630979626658308710596997136359833842511763268142890603897067686016166500482811887218977133094117674620199443929629021672891944995072316778973468639476066710579805578521731403493983042004221192160398395535950981936412937163406460295999679705994343516020318426487569502421748638554059819545816017424178878541927588041627190120855876856483268341404312184008040358092045594943138778151209265222545743971242868207663409547336745996217926655353486256861185433086262872872875630108355631935706687147856390889821151088363521476969796126218329432284178681137684451700181460219136940270209459966835135963278808042743454817445873632200251539529362658066141983656164916262596074347237066169023530800173753128478525584306319074542749341526857906552694060031475910203327467196861247963255105546489028208552974396512499400966255286606758044873538921857014011677169765350140849524768489932573213370289846689391946658618737529663875622660459147770442046810892565844083803204091061900315370673411959410100747433105990550582052432600995169279241747821697678106168369771411073927334392155014302200708736736596227214925877619285105238036702689046390962190766364423553808590294523406519001334234510583834171218051425500392370111132541114461262890625413355052664365359582455215629339751825147065013464104705697935568130660632937334503871097709729487591717901581732028157828848714993134081549334236779704471278593761859508514667736455467920161593422399714298407078888227903265675159652843581779572728480835648996350440414073422611018338354697596266333042208499985230074270393027724347497971797326455254654301983169496846109869074390506801376611925291977093844129970701588949316666116199459226501131118396635250253056164643158720845452298877547517727274765672164898291823923889520720764283971088470596035692199292183190154814128076659269829446445714923966632997307581390495762243896242317520950731901842446244237098642728114951118082282605386248461767518014098312749725765198375649235690280021617490553142720815343954059556357637112728165705973733744297003905604015638866307222570038923015911237696012158008177907786335124086243107357158376592650454665278733787444483440631024475703968125545398226643035341641303561380163416557526558975294452116687345122019122746673319157124076375382110696814107692639007483317574339675231966033086497357138387419609898383220288269488219130281936694995442224069727616862136951165783888501219909616065545461154325314816424933269479700415949147632311292059351651899794335004597628821729262591808940550843146639378254833513955019065337087206206402407705607584879649984365159272826453442863661541914258577710675618501727803328717519518930503180550524542602233552290077141812879865435118791800635627959362476826778641224946033812608262825409889531252767753465624327921451122955551603181843313369296172304178385515712556740498341666592696958000895372457305769454227537216020968719147039887846636724326270619112707171659082464004167994112040565710364083000241929439855307399465653967781049270105541035951333943219992506667620207839469555376055179640100974921885631130101781388857879381317209594806253920130098365028791769582798590527994772194179799702494306215841946888532811549772157996019440962347768614408507573928429882375939682322367058033413477462311289762585932437663177897491107726190970448952220450963072551559009382490402136480779203476721504856844602255440999282616317431264228578762898338065072202301037175314926350463106018857377256700661838129058063895450812703131137104371613583348806583395543121790134839883321641305763524471251153947206667033010134871651632411382881763983962952612114126321979596509865678675525076076042409590751752302194610453256433324961490125353332922372386894812788502013596630537605584935892839163046940388785496002747148719780145765957904958580226006609952496736432496683346176010660815670697514238186650361083885220976165500251607311499216129477579019972924868963822060380876027628167237016681910663358577515465038133423672234764202655856558846416010210540489855618711473588497637840648642679818650448631907747038228671143515112300360708657429886477146674733750114345818852797006056211724692174847180694866251199472893444270378304620707354938052872720621560630718828685805645211106967080285699069825769177220998671959968507790681443494932804976811543680463259938693076235070999518295129581121235707245383354826190752395158273098248180549665897909168867984071707793705959045775840910473413109604194111357756620727337797833203797301137672658535747710279781409721309612142393854737462769615041307952837372882050658719152259765084027796991761175393006725492491229845082362975568722711065849435533850494532638736489804606655979954360169503092790092450057856477235876198848986034412195340795369002996411974549060741600978859537660722905160772428590070901156639138364299041220826769629797867649032356499981990765997439870548648769091024911927099968275697011368762244046402960383700066212734577664709711326374656811502985863032260337383421358423937896114617192083071953915643782093641496780334152464507396683173198363362743392555311712019454146844880895622417898031894341231284027858378289009624209541345002101072736323285272576209646851994468240550629391742053301706461917215178844296705314335503772310709716080285145314144106105023117310877779933248932087727229897821330120834074305604998159963202687793307156940302439156118926767517249511766526248547096041991473113657920697330996088897286789780735587578500623575157123771653042063631002703129296694025421967877168846655727580898306467662007014679585693082220620905330827782226503112520278733512519159918893900284319218166686548434879621972211763904959895793607330943697457628943200384117552941594754747183936381144125610351023459581080768558985657007445308909428669251190101718122826689349269528261052518556736045877702288147821446968500918347219741420546128072347950059811766364526150190788545471193803557145930744635656260752787518824386409506964649815131170591457990619376560858650175616864501924098327235724333688813080022186368700209641119724303603558649793773314916749593151188673535025505982303047060284740458456676849620934506396302909441632516408692889814507247877727673378033828929504978384342943766566737297587430575141036417476861639624198941904730996100228428079444920026904845254139188246001559089131943255610365769362364161784646693141456109984038312265504115251494445380042090428718182468431624610552637677520970104063944687837375017436089751693486887651283453677552786547090231542029453873076141196649767521919808902105772633472397958968722923357769041244458682297806209887089816018179521454920370956252850733023255060096611329479148443416687429872654204083552056456404421174124065041932362831296643126330768715450444950733554418200793669701331244638824360062439816712409346806322169771701563590417609841261977801052586956634654144702511135382841010278579543061802357275500930513955637771043922799597114118278203358118398952338720119626666828781215343331193353019800652511924103594315072427251589774226901431325149775220621148653209528291784172678852791825950189428306645453380829438548491390660090152646315666940813051689857738445716110134773528439558663918031477128997248977232695083095920860316390860179422146804892537147135669490647597566350405076105930300153453613446834614136284840473063909580064862482211399539962122107992774053203059756987131501429238941821989218445861496845306346078287058864262560349767113385390753047360747520569725532663517964059488138127648519130232826129551720747594498863925111049785977410104647258831744969489273332281068408949475978706769012216951869658194406136694310323411619613160554381608728305543504819071159752742665917363693001980988797627218662628543311906086034280619151845297823703639898449414417889008602782220998390227472837967411429578924346545640402855167478372538831386154780508035236893583332887355879794886804980971406868936719416711504307402575102269081707385928535837390976424975922421061832372517021428320986753744507133218963666908565634963306077455683011837149400258404997766113525532847665618870592978212729899729592794781820428719807102278646183807006401083138975677112754136221127444534535584959769252575758312999039536959893249951324106784265611556743660088737484274038234811784911002123537108015334407708175281579422928548731689863980071896268684985779061942582000173178473797975815609269087287850270024414741281953578873964745859459899535543412801653553049058528794674398220606230386688852700505218904927782197514115595435549125326115087432280435609563176116321811794164884206928474315699133677787956913705592704959893911100786224112449931719539890308215307126971807352814294437374058180589784287101566325873726600012296180403780429093175160473979931236882466314524590792512088916974765430245705320638670468411054034201437664442213212750799846299157010147106552946146746392249574530619682203425444816247545977269653430250686824205288099692448923652171403817749282935917315481284919621433304080904306867233682060716291289398517406255904282247558159509102324206160816363511440953267967974466214658121897383725705201831800678505181233270743236051760236565304605919728246762046497950757124332306210615236617229324468286251110577832854712371857906482302429199129753477340618812393224405123793229248698239302094605799468502209356458018864737205798950819968285087908120645175464792846657029993496146354533816989879012073959534299458051884682918835631136138879631316173442207506218212945047503433730640140356614106403320867621443183928438969994268286836082535591242751488383392264668222963323657488981599104902374571278077062853236895690028469742954774248422335523859049299225453318270693966088603518491166875108552006265340966412611220069290556369052744064893640087015171662929356529921474420793873710647399136453402185931518201576110059405556600166318190916348212818643068418256991194316266715898588673650488980580832972145195811525832974358064432698289209364284959616975339927502383832695801109608954786457256109785378297307074918168744735731189049849490781632210127110919398357638892753131749978321368280932894349330930087868884127092076359007648065118301317440813138170776478562086983456849957696333241556699085937149528437303782174166781012624737754844959408277598042857813775448446192929537153359741871355556678028606484917974827559022377376189703770332489774349235376523557139076431488967144133099539679871046284747721772185865851985971282165739148574494328320308464163956096301047370473988450307936956928683464113764226308568695688152053749196294562881085987015910764955019272667378276517237450013662421051146709184898952269727656206976263055094938932099216377529415335060027109430018977339221845390337351007942764665232509045377940478212355620488638969640291029182673024368888013982750049655688955540362739754118359277009094291839958396298535952123465573707751680432023872401008786292362558484920221296055948232317635214207117650427699747801290249150914873347204981208353486521246233538858471700470120592394582541522312967601307268280232044633644234100026474341568399123881048049819491200940244895720301881220640996997340843736095812449945913231793359333819197360248853375641030435643732302001328359990615298394916710687997693926699033522064083729586994304357670917169796698442332656830732550000321312902706719106342428311390049478179307304556219943912072209495471916547109605404919944186051724981471812994063119290173738101176617356976495636675620278895592099504686163440305250658681735840269428736633431167832903837475658050990783985384926064721246565130660487673608585790218386643241627198210378772796337736742692945663985470529377745854692207002046330357343505517537014050310355526578082729897049230547545589009275410944504014157125357682801074915174627928533783099570631952876838237806368177841661186334747789420166190186143388804514884174361681454810362321037643274595653364629397295294049952661691181657740018116146497654407589150912557599100855273107733703213603505619407350405223414533224306604743600257212590127202517146952605462439215815151732661454812243619860357386922465403688559787750083268386930674253759349376972691382532780570135683441862315010318955128705494038594760949278590520009881447715839714713971813720554960331191642239195313230213875992717401904622413925914800620171561815889352945121978193704745708538695427900233080410588007250947512318930796844637224171170594606197614751977323896101315556406372309310279476973938229476346893933755946893665094049910252612163538072005644241026471164639800490998535570282059396054554479255558624918709232180130454102936332893619326596350851413637207293142767763267817840066780089558654877782630822818446508158509625695020697797889664140551101421185533444015948880284701657904464926309216120238068566472631611326995533585414320547442896728173291714010643730593960222482733969720865809194288803963344344876467583385597351333330628439786357062196382217705500672607607570202305548328439335937369624085404957344415141889143812206076832329063384332685935928226648361622876815670931303789678327741487845287838232474038340893449427806045589018183673133602271167285304427194507315740913600066356089181219040305019319028163972135790696025211929562455952835850442627787993214468221041325612271290302469610374855134599106662606082143546126463790846952338680559237822828610361386416013753920426888371192602742087474507782730180882648297991489233434653363930327991816476995529468892904060335470265188317825821391915073117022336839564945335630414192442838503954209073337511117053790819768061378846157004292392264788138228486672543415580694421193506836000488465561599083339184724263183698928130695654949153165010313216361224018298711517222401523368101476246169896417259748838727189598765602350324828709741468793415378708814573190327920453219231685852735108372055942456601545647944675449566859142997988233179819059574125368681032194798082603876241044848730208905065871934264174092007936669883601462309762759844113071525758916288010581709353072588887654386253201848624931923638568216562603110434528313030704972291334873033240933736956347974889824930017415805659182123288343858101250171537305398462043432455721482088547523494730467761429282915391485852688505423074450548192619166975975031503447208211845313907683486006908772752077246485706597636740936173143436990399498908375710246545650814962015988805204483379491707040848303909417512426275869868668644293498242419667403627076032399201407183071270759837132000712447159523642782162488472933913713634046138974088894178399320090051543608421618891328957740354384456107645016010462709579098652495342014766016330458293537653454523438667413798731255017029554582809547897542497367109038598264606895622241257303208140890607025206140457815282368504505765710043804228592032720729190222134651835930255942940875306994701101153416476785623543575023993736414532895773499876167502240919794121893188059017977444329403624038551082491954751841177014150820554999148803286500065069030165028455616533514890711974194172310029663247936640825364542104897640445108081123906368188594908660418340025631562661211506365309297219580687177632051461355581309500814563826112416521487163593643553646268872746276680368630680088231249970572706496265335285424273723449757482776061300818063419639083097882249478922949525891665782610044424440110326748539620120023397129834624242363283711074267309902126029110038109050751840523266273905031934856015485510632624318778970878895198168073096354223096005536267735905099473408744371024816727970009494589707630185344952680106730984246828848883760016695887137355969244555238536396178788134209309376484848406842940499731494663578455826688245825356635393289729316700066238128368519670627697889769929009597838069557440769080950069594659578325366066060213000525012998145215099629307110700615796004759918829827472751877492472674770755413679265775060149528336859838085353420874215682758801259992855903410097963019943741001394975591822918846705741010634931594527954742032057295356596869586863097328488381174243827058441735659667485315202886191192125286398739560928127513223214119754229343092375569339614672740517569529376699061052365448344078610425576694541873486379356070861240473688356773437140126350120823765176390562050604076894729400293162079760342896846897639867830553941515230713725560502914671175123451932131962571791940911728951123948113598860588062424037835751996487088330150679210175429060531418836978611027896830689666851868410470182364780700615529883149883111601949965815038674390467105247175993726709203381051984777006122752302698038537619917731907133105816779008651480172440446403764720673784583395382889380902941273987910475254258486561698048543296782281040453997661165123290729161619992628751086519341731116513305659182981762584769428708454819029344222186027977405519291266188948708010515922860149238393490889782166965109499761673179583522105791358724355029782111425280584380959770472177893827382916471882671437865821461326011263516554280516418422188264141890686619186492751718984735037496602686033671961304915922609442146773092074476794711917820209913226872184947548378003848726148872742881265579174794634151444545105599464567614478293387968015412886418098284885525959617399177657635267081989985408930744564199296902459275405143647525648661932959903068323866757518479741015342911416508753572892479684280248440220211898390243430190746592470563991910024225814399068391457857458095344096826158489731615822039837691005171654390590093326827586419753439483771905973079465029210363641972615923872187876095687197681934481955852567024141433671590889694204781798936556351775101591005026585947279448642317311892727153525046034081896227383114600546852406398855471859684088277722162250586368419379964112646321070639818773794369650252104438622320671517228411475433482803041707675438555447584321271846396281391925884972509051040944134450429845346071848875654240709690138592611645519676563708429710676494635766201285381926791204110977805857352062737510466943591592074904378966129808716274322385039032007477854211063899544954185997641428116395197239708078986048758264126544825149923227286176571389697334537835963603962709038002668921324389159009375225033651171937770657226295341257068980907793198879997076783263303670667342657925395849950582363998610492878479976185891384024744790742355981796013254960652684988733518397287191251899388324341602608356164496670902390042273216221931567939944001215159910054381084520081133103207553492484487369268314444466610780275891777468369344585045949963237156043800258227618908603074550819931892899703285549507330240121766349515315827830897786432254556221744305752825143708087184314470811004510108612122699931396969361066523608721126359012344828262284427191281973187269761974740398071778378188160519801862257232970224762494767912932684020188061795236229174601398576604233579094407723017353015337974435643738584248250538061547193075224429309117207447677149522141919390974201716026970557825836923707297811545552570788004955666915477901830719591663516687057984336951611189153751912396714116378197000784953115386326766369269172016978409040396969804861828436417776804088449208439901095951205751340861060375353408155737087188313898337656322533650946010308686111901241541794900659835366926383515058402026098259570385429145865025692157987309807064597082326377138235585737704225628144262793497769429358804020882742028263786443615935817930817858306265712263479452174065216410798029333573961137404301928294367884626832432449078812684787281988676202931062510264948586549463964789154366240635570346688477784815271412470430646040615614277320107003575855033995279377529716156628381118518085523414187577256025217995103662771477552291036839539792329375184700131215428652464111526297830742328651189481978920924682746392250346179819781021313400022272303222234731521016033826145645816472110340883197207109422849637006090510260943044730126801795349152894613046101033061811314821366141874985466628809585678299308824993966655499624380015821082410781190328189506855057581990908848597095494573176672201417764187253816862426293852974092626551536758155537683368451820154793964862810533857810979434793077956125541240828563089647076354827276586047900779183041806574320855302776686899978897939486987950729652971448050889517660684386673056662911929857913206598752762097197279390208473846210277152094212386266930256260451209117402079233658157593274696841906354187366092529138116574357045728290417433832596884391356956442617823006949118156994294295529170211353842468704890572313005646106202029653246628477843902025194715815133791174898257040115532858624973690714844800747184719290671002133191274834310662201874141841328708920709275866745037664169280121112867057832132585948539987132879098472640550013972043153470930436509718084070853723316111111611632600262171748813737621046013600544051850633175245231989785291065646466038278748870331134307620041356514295482843502245454400571392386492526283423907951705366640483826875013469850263767974528926285288366544314868036628329638912254207094687335597669512007687507292940623176435604796651807847095408991068514998003358735387989422028901542800717906482276185298683079286137204396993726503610285463352157718364571843381950031926272352293654343387522809514152498052577486366048613580539162662183475105825647260311633442002377527140625112075332294909525522330744664115572260242435895269482927435844022622001466247093866533879048392320516224276433339282642640953964341822416705658461244760448817737705782669080880834418822622611342632727419248415651121035047131961583094994438779439078380664656207143187309895280874153167621657602227990850199615587578332393883365169478142077533262283694526612005465820771400826060398839255150948861553177333447506822679211849690448880479070102043288205874672361672971246062341973369704807867768609989464712379097525706498042381815865399434983035941162258347729020489356838477197804973214911448748749915616679253857438010864500220134843719609727912761136925035123155282535741655826107266099467657016111855684257826878422197833994329148734893923892153298966294232703135845615804723993624827409373966761563257981994036006655039613941881183164267144485664874468348587099434743710128859267552473831462181434321232124758618476925803128913233878664527525204324484796532776273320171351979849530142473805976430318655810403609897537469226336015596525652284888167037460054235043655813438329870872734142062859147847007274999414885129441657918212383876056572545671794085637289277002790218604788423519924573051811976377731594412994393860534559159658127123862955315918182841923881357245009246238507097741891437575676886206936433608263660374355173185026954239766173038826275043838965247160428689739548061640664606565379050539422795708801840829664956978192406737307076253014257542221763860230431809477056758905681723033326311408802886092880151777469082375063137750925275331638009836786645991949881018108222446858443984865972449621097999331605268587810061927125889694400669979755648800940895626242917531834388920035663113368763931463847812763130237825562198311791061780856687903309789539747505239545316630638169559777653347655949908779202359718666623572487055558216484036084925217803431104356647417600193631613474196113126657206064282217690428541246560204561459484317744683213906021267727411189443675804442911583757423572500214191467493342871160840582639470485636370375679604797073490813681083838562113841391587052553615073991983125473434527404596547926972539542447555990332809716643578039646945749813368621152410490288581779206318208255069166455507840899628333174744873951607229399258854694188637978240144635295264982572856632103053550891057171748674115218494774077589151115819489068851971959768129214023511454382738867557288320426608338030759515727545577639726238470674634011626346953231815229549718996906470438903536574430644436472716449550868519871817092814068746449470806856174570885105064766494332205391085097539987897980672278869943134632799032372604933150163386774039430519493297142505321117669011820293604482694166301309801111227443654953271242388534939973277749999335296667138307969441135719079969506099821923206878892624416110175909254904610286553512032488285673735148429324009831633211264460376172046209384270528903772251057643968938983722779640468452705694321085455273829462711022737243290606294601651732654594463569861350966095209962038508010899673666470073918705760679801337058347046567503369379598928154437380765511031719081985901371088639600700705631873099251480947989238619052479230983309717938226245725600119571130722386790431255742179135633111146646083268382596762356018472772209198013121983224179079476134977421748168833934278876403014334318798493417716613256506422668264638388429786875443810986754386459491846082078633346046469418429778813833857755519670005669840456587642130852057050148314568259387702428619224671173187370822224627538313365937868201435535126600146246249435880806572693573084485615073901842761167215162204840459913839674251648</pre>
 
=={{header|C++}}==
<langsyntaxhighlight lang=c++>
#include<bits/stdc++.h>
using namespace std;
Line 797:
return 0;
}
</syntaxhighlight>
</lang>
 
 
Line 806:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang=lisp>(ns agmcompute
(:gen-class))
 
Line 831:
 
(println (agm one isqrt2))
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 838:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang=cobol>IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
Line 870:
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).</langsyntaxhighlight>
{{out}}
<pre>0.8472130847939792</pre>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang=lisp>(defun agm (a0 g0 &optional (tolerance 1d-8))
(loop for a = a0 then (* (+ a g) 5d-1)
and g = g0 then (sqrt (* a g))
until (< (abs (- a g)) tolerance)
finally (return a)))
</syntaxhighlight>
</lang>
 
{{out}}
Line 891:
 
=={{header|D}}==
<langsyntaxhighlight lang=d>import std.stdio, std.math, std.meta, std.typecons;
 
real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe {
Line 903:
void main() @safe {
writefln("%0.19f", agm(1, 1 / sqrt(2.0)));
}</langsyntaxhighlight>
{{out}}
<pre>0.8472130847939790866</pre>
Line 910:
{{libheader| System.SysUtils}}
{{Trans|C#}}
<langsyntaxhighlight lang=Delphi>
program geometric_mean;
 
Line 954:
writeln(format('The arithmetic-geometric mean is %.6f', [agm(x, y)]));
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>Enter two numbers:1
Line 962:
=={{header|EchoLisp}}==
We use the '''(~= a b)''' operator which tests for |a - b| < ε = (math-precision).
<langsyntaxhighlight lang=scheme>
(lib 'math)
 
Line 977:
(agm 1 (/ 1 (sqrt 2)))
→ 0.8472130847939792
</syntaxhighlight>
</lang>
 
=={{header|Elixir}}==
 
<langsyntaxhighlight lang=Elixir>defmodule ArithhGeom do
def mean(a,g,tol) when abs(a-g) <= tol, do: a
def mean(a,g,tol) do
Line 988:
end
 
IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)</langsyntaxhighlight>
 
{{out}}
Line 996:
 
=={{header|Erlang}}==
<langsyntaxhighlight lang=Erlang>%% Arithmetic Geometric Mean of 1 and 1 / sqrt(2)
%% Author: Abhay Jain
 
Line 1,014:
A1 = (A+B) / 2,
B1 = math:pow(A*B, 0.5),
agm(A1, B1).</langsyntaxhighlight>
Output:
<langsyntaxhighlight lang=Erlang>AGM = 0.8472130848351929</langsyntaxhighlight>
 
=={{header|ERRE}}==
Line 1,041:
PRINT(A)
END PROGRAM
</syntaxhighlight>
</lang>
 
=={{header|F_Sharp|F#}}==
{{trans|OCaml}}
<langsyntaxhighlight lang=fsharp>let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
 
printfn "%g" (agm 1. (sqrt(0.5)) 1e-15)</langsyntaxhighlight>
Output
<pre>0.847213</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang=factor>USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
 
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
 
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .</langsyntaxhighlight>
{{out}}
<pre>
Line 1,066:
 
=={{header|Forth}}==
<langsyntaxhighlight lang=forth>: agm ( a g -- m )
begin
fover fover f+ 2e f/
Line 1,074:
fdrop ;
 
1e 2e -0.5e f** agm f. \ 0.847213084793979</langsyntaxhighlight>
 
=={{header|Fortran}}==
A '''Fortran 77''' implementation
<langsyntaxhighlight lang=fortran> function agm(a,b)
implicit none
double precision agm,a,b,eps,c
Line 1,092:
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
end</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang=freebasic>' version 16-09-2015
' compile with: fbc -s console
 
Line 1,120:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre> 0.8472130847939792</pre>
Line 1,127:
{{incorrect|Futhark|Futhark's syntax has changed, so this example will not compile}}
 
<langsyntaxhighlight lang=Futhark>
import "futlib/math"
 
Line 1,139:
fun main(x: f64, y: f64): f64 =
agm(x,y)
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
<langsyntaxhighlight lang=go>package main
 
import (
Line 1,160:
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,169:
{{trans|Java}}
Solution:
<langsyntaxhighlight lang=groovy>double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}</langsyntaxhighlight>
 
Test:
<langsyntaxhighlight lang=groovy>println "agm(1, 0.5**0.5) = agm(1, ${0.5**0.5}) = ${agm(1, 0.5**0.5)}"
assert (0.8472130847939792 - agm(1, 0.5**0.5)).abs() <= 10.0**-14</langsyntaxhighlight>
 
Output:
Line 1,183:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang=haskell>-- Return an approximation to the arithmetic-geometric mean of two numbers.
-- The result is considered accurate when two successive approximations are
-- sufficiently close, as determined by "eq".
Line 1,202:
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal</langsyntaxhighlight>
{{out}}
<pre>0.8472130847527654</pre>
Line 1,223:
}
return an
end</langsyntaxhighlight>
 
Output:
Line 1,239:
First, the basic approach (with display precision set to 16 digits, which slightly exceeds the accuracy of 64 bit IEEE floating point arithmetic):
 
<langsyntaxhighlight lang=j>mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791</langsyntaxhighlight>
 
This is the limit -- it stops when values are within a small epsilon of previous calculations. We can ask J for unique values (which also means -- unless we specify otherwise -- values within a small epsilon of each other, for floating point values):
 
<langsyntaxhighlight lang=j> ~.(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792</langsyntaxhighlight>
 
Another variation would be to show intermediate values, in the limit process:
 
<langsyntaxhighlight lang=j> (mean, */ %:~ #)^:a: 1,%%:2
1 0.7071067811865475
0.8535533905932737 0.8408964152537145
0.8472249029234942 0.8472012667468915
0.8472130848351929 0.8472130847527654
0.8472130847939792 0.8472130847939791</langsyntaxhighlight>
 
=== Arbitrary Precision ===
Line 1,263:
Borrowing routines from that page, but going with a default of approximately 100 digits of precision:
 
<langsyntaxhighlight lang=J>DP=:101
 
round=: DP&$: : (4 : 0)
Line 1,292:
n=. e (>i.1:) a (^%!@]) i.>.a^.e [ a=. |y-m*^.2
(2x^m) * 1++/*/\d%1+i.n
)</langsyntaxhighlight>
 
We are also going to want a routine to display numbers with this precision, and we are going to need to manage epsilon manually, and we are going to need an arbitrary root routine:
 
<langsyntaxhighlight lang=J>fmt=:[: ;:inv DP&$: : (4 :0)&.>
x{.deb (x*2j1)":y
)
Line 1,302:
root=: ln@] exp@% [
 
epsilon=: 1r9^DP</langsyntaxhighlight>
 
Some example uses:
 
<langsyntaxhighlight lang=J> fmt sqrt 2
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572
fmt *~sqrt 2
Line 1,313:
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000418
fmt 2 root 2
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572</langsyntaxhighlight>
 
Note that 2 root 2 is considerably slower than sqrt 2. The price of generality. So, while we could define geometric mean generally, a desire for good performance pushes us to use a routine specialized for two numbers:
 
<langsyntaxhighlight lang=J>geomean=: */ root~ #
geomean2=: [: sqrt */</langsyntaxhighlight>
 
A quick test to make sure these can be equivalent:
 
<langsyntaxhighlight lang=J> fmt geomean 3 5
3.872983346207416885179265399782399610832921705291590826587573766113483091936979033519287376858673517
fmt geomean2 3 5
3.872983346207416885179265399782399610832921705291590826587573766113483091936979033519287376858673517</langsyntaxhighlight>
 
Now for our task example:
 
<langsyntaxhighlight lang=J> fmt (mean, geomean2)^:(epsilon <&| -/)^:a: 1,%sqrt 2
1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0.707106781186547524400844362104849039284835937688474036588339868995366239231053519425193767163820786
0.853553390593273762200422181052424519642417968844237018294169934497683119615526759712596883581910393 0.840896415253714543031125476233214895040034262356784510813226085974924754953902239814324004199292536
Line 1,337:
0.847213084793979086606499123482191636481445984459557704232275241670533381126169243513557113565344075 0.847213084793979086606499123482191636481445836194326665888883503648934628542100275932846717790147361
0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723201915677745718 0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723198672311476741
0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723200293994611229 0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723200293994611229</langsyntaxhighlight>
 
We could of course extract out only a representative final value, but it's obvious enough, and showing how rapidly this converges is fun.
Line 1,343:
=={{header|Java}}==
 
<langsyntaxhighlight lang=Java>/*
* Arithmetic-Geometric Mean of 1 & 1/sqrt(2)
* Brendan Shaklovitz
Line 1,365:
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}</langsyntaxhighlight>
{{out}}
<pre>0.8472130847939792</pre>
Line 1,372:
 
===ES5===
<langsyntaxhighlight lang=JavaScript>function agm(a0, g0) {
var an = (a0 + g0) / 2,
gn = Math.sqrt(a0 * g0);
Line 1,381:
}
 
agm(1, 1 / Math.sqrt(2));</langsyntaxhighlight>
 
===ES6===
<langsyntaxhighlight lang=JavaScript>(() => {
'use strict';
 
Line 1,426:
return agm(1, 1 / Math.sqrt(2));
 
})();</langsyntaxhighlight>
 
{{Out}}
<syntaxhighlight lang =JavaScript>0.8472130848351929</langsyntaxhighlight>
 
=={{header|jq}}==
{{works with|jq|1.4}}
Naive version that assumes tolerance is appropriately specified:
<langsyntaxhighlight lang=jq>def naive_agm(a; g; tolerance):
def abs: if . < 0 then -. else . end;
def _agm:
Line 1,442:
else .
end;
[a, g] | _agm | .[0] ;</langsyntaxhighlight>
This version avoids an infinite loop if the requested tolerance is too small:
<langsyntaxhighlight lang=jq>def agm(a; g; tolerance):
def abs: if . < 0 then -. else . end;
def _agm:
Line 1,459:
# Example:
agm(1; 1/(2|sqrt); 1e-100)</langsyntaxhighlight>
{{Out}}
$ jq -n -f Arithmetic-geometric_mean.jq
Line 1,466:
=={{header|Julia}}==
{{works with|Julia|1.2}}
<langsyntaxhighlight lang=Julia>function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
Line 1,485:
println("# Using ", precision(BigFloat), "-bit float numbers:")
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)</langsyntaxhighlight>
The &epsilon; for this calculation is given as a positive integer multiple of the machine &epsilon; for <tt>x</tt>.
 
Line 1,498:
=={{header|Klingphix}}==
{{trans|Oforth}}
<langsyntaxhighlight lang=Klingphix>include ..\Utilitys.tlhy
 
:agm [ over over + 2 / rot rot * sqrt ] [ over over tostr swap tostr # ] while drop ;
Line 1,506:
pstack
 
" " input</langsyntaxhighlight>
{{trans|F#}}
<langsyntaxhighlight lang=Klingphix>include ..\Utilitys.tlhy
 
:agm %a %g %p !p !g !a
Line 1,517:
pstack
 
" " input</langsyntaxhighlight>
{{out}}
<pre>(0.847213)</pre>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang=scala>// version 1.0.5-2
 
fun agm(a: Double, g: Double): Double {
Line 1,540:
fun main(args: Array<String>) {
println(agm(1.0, 1.0 / Math.sqrt(2.0)))
}</langsyntaxhighlight>
 
{{out}}
Line 1,549:
=={{header|LFE}}==
 
<langsyntaxhighlight lang=lisp>
(defun agm (a g)
(agm a g 1.0e-15))
Line 1,565:
(defun next-g (a g)
(math:sqrt (* a g)))
</syntaxhighlight>
</lang>
 
Usage:
Line 1,575:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang=lb>
<lang lb>
print agm(1, 1/sqr(2))
print using("#.#################",agm(1, 1/sqr(2)))
Line 1,590:
end function
</syntaxhighlight>
</lang>
 
=={{header|LiveCode}}==
<langsyntaxhighlight lang=LiveCode>function agm aa,g
put abs(aa-g) into absdiff
put (aa+g)/2 into aan
Line 1,605:
end repeat
return aa
end agm</langsyntaxhighlight>
Example
<langsyntaxhighlight lang=LiveCode>put agm(1, 1/sqrt(2))
-- ouput
-- 0.847213</langsyntaxhighlight>
 
=={{header|LLVM}}==
<langsyntaxhighlight lang=llvm>; This is not strictly LLVM, as it uses the C library function "printf".
; LLVM does not provide a way to print values, so the alternative would be
; to just load the string into memory, and that would be boring.
Line 1,714:
attributes #2 = { nounwind readnone speculatable }
attributes #4 = { nounwind }
attributes #6 = { noreturn }</langsyntaxhighlight>
{{out}}
<pre>The arithmetic-geometric mean is 0.8472130847939791654</pre>
 
=={{header|Logo}}==
<langsyntaxhighlight lang=logo>to about :a :b
output and [:a - :b < 1e-15] [:a - :b > -1e-15]
end
Line 1,728:
 
show agm 1 1/sqrt 2
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
 
<langsyntaxhighlight lang=lua>function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
Line 1,742:
end
 
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))</langsyntaxhighlight>
 
'''Output:'''
Line 1,749:
 
=={{header|M2000 Interpreter}}==
<langsyntaxhighlight lang=M2000 Interpreter>
Module Checkit {
Function Agm {
Line 1,764:
}
Checkit
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
Maple provides this function under the name GaussAGM. To compute a floating point approximation, use evalf.
<langsyntaxhighlight lang=Maple>
> evalf( GaussAGM( 1, 1 / sqrt( 2 ) ) ); # default precision is 10 digits
0.8472130847
Line 1,775:
0.847213084793979086606499123482191636481445910326942185060579372659\
7340048341347597232002939946112300
</syntaxhighlight>
</lang>
Alternatively, if one or both arguments is already a float, Maple will compute a floating point approximation automatically.
<langsyntaxhighlight lang=Maple>
> GaussAGM( 1.0, 1 / sqrt( 2 ) );
0.8472130847
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
To any arbitrary precision, just increase PrecisionDigits
<langsyntaxhighlight lang=Mathematica>PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛</langsyntaxhighlight>
 
<pre>AGMean[1, 1/Sqrt[2]]
Line 1,791:
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight lang=MATLAB>function [a,g]=agm(a,g)
%%arithmetic_geometric_mean(a,g)
while (1)
Line 1,799:
if (abs(a0-a) < a*eps) break; end;
end;
end</langsyntaxhighlight>
<pre>octave:26> agm(1,1/sqrt(2))
ans = 0.84721
Line 1,805:
 
=={{header|Maxima}}==
<langsyntaxhighlight lang=maxima>agm(a, b) := %pi/4*(a + b)/elliptic_kc(((a - b)/(a + b))^2)$
 
agm(1, 1/sqrt(2)), bfloat, fpprec: 85;
/* 8.472130847939790866064991234821916364814459103269421850605793726597340048341347597232b-1 */</langsyntaxhighlight>
 
=={{header|МК-61/52}}==
Line 1,814:
- ИП2 - /-/ x<0 31 ИП1 П3 ИП0 ИП1
* КвКор П1 ИП0 ИП3 + 2 / П0 БП
08 ИП0 С/П</langsyntaxhighlight>
 
=={{header|Modula-2}}==
{{trans|C}}
<langsyntaxhighlight lang=modula2>MODULE AGM;
FROM EXCEPTIONS IMPORT AllocateSource,ExceptionSource,GetMessage,RAISE;
FROM LongConv IMPORT ValueReal;
Line 1,887:
WriteReal(AGM(x, y));
WriteLn
END AGM.</langsyntaxhighlight>
{{out}}
<pre>Enter two numbers: 1.0
Line 1,898:
=={{header|NetRexx}}==
{{trans|Java}}
<langsyntaxhighlight lang=NetRexx>/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 1,922:
end
return a1 + 0
</syntaxhighlight>
</lang>
'''Output:'''
<pre>
Line 1,929:
 
=={{header|NewLISP}}==
<langsyntaxhighlight lang=NewLISP>
(define (a-next a g) (mul 0.5 (add a g)))
 
Line 1,951:
(amg 1.0 root-reciprocal-2 quadrillionth)
)
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
<langsyntaxhighlight lang=nim>import math
 
proc agm(a, g: float,delta: float = 1.0e-15): float =
Line 1,967:
result = aOld
 
echo agm(1.0,1.0/sqrt(2.0))</langsyntaxhighlight>
 
Output:<br/>
Line 1,976:
See first 24 iterations:
 
<langsyntaxhighlight lang=nim>from math import sqrt
from strutils import parseFloat, formatFloat, ffDecimal
 
Line 1,995:
 
echo("Result A: " & formatFloat(t.resA, ffDecimal, 24))
echo("Result G: " & formatFloat(t.resG, ffDecimal, 24))</langsyntaxhighlight>
 
=={{header|Oberon-2}}==
{{works with|oo2c}}
<langsyntaxhighlight lang=oberon2>
MODULE Agm;
IMPORT
Line 2,025:
Out.LongReal(Of(1,1 / Math.sqrt(2)),0,0);Out.Ln
END Agm.
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,033:
=={{header|Objeck}}==
{{trans|Java}}
<langsyntaxhighlight lang=objeck>
class ArithmeticMean {
function : Amg(a : Float, g : Float) ~ Nil {
Line 2,050:
}
}
</syntaxhighlight>
</lang>
 
Output:
Line 2,056:
 
=={{header|OCaml}}==
<langsyntaxhighlight lang=ocaml>let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
 
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)</langsyntaxhighlight>
Output
<pre>0.8472130847939792</pre>
Line 2,066:
=={{header|Oforth}}==
 
<langsyntaxhighlight lang=Oforth>: agm \ a b -- m
while( 2dup <> ) [ 2dup + 2 / -rot * sqrt ] drop ;</langsyntaxhighlight>
 
Usage :
<syntaxhighlight lang =Oforth>1 2 sqrt inv agm</langsyntaxhighlight>
 
{{out}}
Line 2,078:
 
=={{header|OOC}}==
<langsyntaxhighlight lang=ooc>
import math // import for sqrt() function
 
Line 2,098:
"%.16f" printfln(agm(1., sqrt(0.5)))
}
</syntaxhighlight>
</lang>
Output
<pre>0.8472130847939792</pre>
 
=={{header|ooRexx}}==
<langsyntaxhighlight lang=ooRexx>numeric digits 20
say agm(1, 1/rxcalcsqrt(2,16))
 
Line 2,120:
return a1+0
 
::requires rxmath LIBRARY</langsyntaxhighlight>
{{out}}
<pre>0.8472130847939791968</pre>
Line 2,126:
=={{header|PARI/GP}}==
Built-in:
<langsyntaxhighlight lang=parigp>agm(1,1/sqrt(2))</langsyntaxhighlight>
 
Iteration:
<langsyntaxhighlight lang=parigp>agm2(x,y)=if(x==y,x,agm2((x+y)/2,sqrt(x*y))</langsyntaxhighlight>
 
=={{header|Pascal}}==
Line 2,135:
{{libheader|GMP}}
Port of the C example:
<langsyntaxhighlight lang=pascal>Program ArithmeticGeometricMean;
 
uses
Line 2,169:
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.</langsyntaxhighlight>
Output is as long as the C example.
 
=={{header|Perl}}==
<langsyntaxhighlight lang=perl>#!/usr/bin/perl -w
 
my ($a0, $g0, $a1, $g1);
Line 2,189:
}
 
print agm(1, 1/sqrt(2))."\n";</langsyntaxhighlight>
Output:
<pre>0.847213084793979</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">agm</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">tolerance</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1.0e-15</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #7060A8;">abs</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><span style="color: #000000;">tolerance</span> <span style="color: #008080;">do</span>
Line 2,203:
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">agm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- (rounds to 10 d.p.)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,214:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight lang=Phixmonti>include ..\Utilitys.pmt
 
1.0e-15 var tolerance
Line 2,228:
enddef
 
1 1 2 sqrt / agm tostr ?</langsyntaxhighlight>
 
=={{header|PHP}}==
<langsyntaxhighlight lang=php>
define('PRECISION', 13);
 
Line 2,254:
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,261:
 
=={{header|Picat}}==
<langsyntaxhighlight lang=Picat>main =>
println(agm(1.0, 1/sqrt(2))).
 
agm(A,G) = A, A-G < 1.0e-10 => true.
agm(A,G) = agm((A+G)/2, sqrt(A*G)).
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,274:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight lang=PicoLisp>(scl 80)
 
(de agm (A G)
Line 2,283:
(round
(agm 1.0 (*/ 1.0 1.0 (sqrt 2.0 1.0)))
70 )</langsyntaxhighlight>
Output:
<pre>-> "0.8472130847939790866064991234821916364814459103269421850605793726597340"</pre>
 
=={{header|PL/I}}==
<langsyntaxhighlight lang=PL/I>
arithmetic_geometric_mean: /* 31 August 2012 */
procedure options (main);
Line 2,302:
put skip list ('The result is:', a);
end arithmetic_geometric_mean;
</syntaxhighlight>
</lang>
Results:
<pre>
Line 2,315:
=={{header|Potion}}==
Input values should be floating point
<langsyntaxhighlight lang=potion>sqrt = (x) :
xi = 1
7 times :
Line 2,331:
.
x
.</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang=PowerShell>
function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
Line 2,349:
}
agm 1 (1/[Math]::Sqrt(2))
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 2,358:
 
=={{header|Prolog}}==
<langsyntaxhighlight lang=Prolog>
agm(A,G,A) :- abs(A-G) < 1.0e-15, !.
agm(A,G,Res) :- A1 is (A+G)/2.0, G1 is sqrt(A*G),!, agm(A1,G1,Res).
Line 2,364:
?- agm(1,1/sqrt(2),Res).
Res = 0.8472130847939792.
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang=purebasic>Procedure.d AGM(a.d, g.d, ErrLim.d=1e-15)
Protected.d ta=a+1, tg
While ta <> a
Line 2,381:
Input()
CloseConsole()
EndIf</langsyntaxhighlight>
 
0.8472130847939792
Line 2,389:
 
===Basic Version===
<langsyntaxhighlight lang=python>from math import sqrt
 
def agm(a0, g0, tolerance=1e-10):
Line 2,404:
return an
 
print agm(1, 1 / sqrt(2))</langsyntaxhighlight>
{{out}}
<pre> 0.847213084835</pre>
===Multi-Precision Version===
<langsyntaxhighlight lang=python>from decimal import Decimal, getcontext
 
def agm(a, g, tolerance=Decimal("1e-65")):
Line 2,417:
 
getcontext().prec = 70
print agm(Decimal(1), 1 / Decimal(2).sqrt())</langsyntaxhighlight>
{{out}}
<pre>0.847213084793979086606499123482191636481445910326942185060579372659734</pre>
Line 2,424:
=={{header|Quackery}}==
 
<langsyntaxhighlight lang=Quackery> [ $ "bigrat.qky" loadfile ] now!
 
[ temp put
Line 2,441:
125 point$ echo$ cr cr
swap say "Num: " echo cr
say "Den: " echo</langsyntaxhighlight>
 
{{out}}
Line 2,454:
 
=={{header|R}}==
<langsyntaxhighlight lang=r>arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
 
Line 2,467:
 
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))</langsyntaxhighlight>
{{out}}
<pre> agm rel_error
1 0.8472130847939792 1.310441309927519e-16</pre>
This function also works on vectors a and b (following the spirit of R):
<langsyntaxhighlight lang=r>a <- c(1, 1, 1)
b <- c(1/sqrt(2), 1/sqrt(3), 1/2)
agm <- arithmeticGeometricMean(a, b)
print(format(agm, digits=16))</langsyntaxhighlight>
{{out}}
<pre> agm rel_error
Line 2,484:
=={{header|Racket}}==
This version uses Racket's normal numbers:
<langsyntaxhighlight lang=racket>
#lang racket
(define (agm a g [ε 1e-15])
Line 2,492:
 
(agm 1 (/ 1 (sqrt 2)))
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,499:
 
This alternative version uses arbitrary precision floats:
<langsyntaxhighlight lang=racket>
#lang racket
(require math/bigfloat)
(bf-precision 200)
(bfagm 1.bf (bf/ (bfsqrt 2.bf)))
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,512:
=={{header|Raku}}==
(formerly Perl 6)
<langsyntaxhighlight lang=perl6>sub agm( $a is copy, $g is copy ) {
($a, $g) = ($a + $g)/2, sqrt $a * $g until $a ≅ $g;
return $a;
}
say agm 1, 1/sqrt 2;</langsyntaxhighlight>
{{out}}
<pre>0.84721308479397917</pre>
 
It's also possible to write it recursively:
<langsyntaxhighlight lang=perl6>sub agm( $a, $g ) {
$a ≅ $g ?? $a !! agm(|@$_)
given ($a + $g)/2, sqrt $a * $g;
}
 
say agm 1, 1/sqrt 2;</langsyntaxhighlight>
 
=={{header|Raven}}==
<langsyntaxhighlight lang=Raven>define agm use $a, $g, $errlim
# $errlim $g $a "%d %g %d\n" print
$a 1.0 + as $t
Line 2,541:
 
 
16 1 2 sqrt / 1 agm "agm: %.15g\n" print</langsyntaxhighlight>
{{out}}
<pre>t: 0.853553 a: 0.853553 g: 0.840896
Line 2,550:
 
=={{header|Relation}}==
<langsyntaxhighlight lang=Relation>
function agm(x,y)
set a = x
Line 2,569:
echo sqrt(x+y)
echo agm(x,y)
</syntaxhighlight>
</lang>
 
<pre>
Line 2,581:
 
REXX supports arbitrary precision, so the default digits can be changed if desired.
<langsyntaxhighlight 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.*/
if digs=='' | digs=="," then digs= 120 /*No DIGS specified? Then use default.*/
Line 2,612:
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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 2,621:
 
=={{header|Ring}}==
<langsyntaxhighlight lang=ring>
decimals(9)
see agm(1, 1/sqrt(2)) + nl
Line 2,635:
end
return gn
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
===Flt Version===
The thing to note about this implementation is that it uses the [http://flt.rubyforge.org/ Flt] library for high-precision math. This lets you adapt context (including precision and epsilon) to a ridiculous-in-real-life degree.
<langsyntaxhighlight lang=ruby># The flt package (http://flt.rubyforge.org/) is useful for high-precision floating-point math.
# It lets us control 'context' of numbers, individually or collectively -- including precision
# (which adjusts the context's value of epsilon accordingly).
Line 2,660:
end
 
puts agm(1, 1 / BinNum(2).sqrt)</langsyntaxhighlight>
{{out}}
<pre>0.84721308479397908660649912348219163648144591032694218506057937265973400483413475972320029399461122994212228562523341096309796266583087105969971363598338426</pre>
Line 2,667:
===BigDecimal Version===
Ruby has a BigDecimal class in standard library
<langsyntaxhighlight lang=ruby>require 'bigdecimal'
 
PRECISION = 100
Line 2,682:
a = BigDecimal(1)
g = 1 / BigDecimal(2).sqrt(PRECISION)
puts agm(a, g)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,690:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang=runbasic>print agm(1, 1/sqr(2))
print agm(1,1/2^.5)
print using("#.############################",agm(1, 1/sqr(2)))
Line 2,702:
g = gn
wend
end function</langsyntaxhighlight>Output:
<pre>0.847213085
0.847213085
Line 2,709:
=={{header|Rust}}==
 
<langsyntaxhighlight lang=rust>// Accepts two command line arguments
// cargo run --name agm arg1 arg2
 
Line 2,739:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,748:
 
=={{header|Scala}}==
<langsyntaxhighlight lang=scala>
def agm(a: Double, g: Double, eps: Double): Double = {
if (math.abs(a - g) < eps) (a + g) / 2
Line 2,755:
 
agm(1, math.sqrt(2)/2, 1e-15)
</syntaxhighlight>
</lang>
 
=={{header|Scheme}}==
 
<langsyntaxhighlight lang=scheme>
(define agm
(case-lambda
Line 2,770:
 
(display (agm 1 (/ 1 (sqrt 2)))) (newline)
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,778:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang=seed7>$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
Line 2,807:
writeln(agm(1.0, 2.0) digits 6);
writeln(agm(1.0, 1.0 / sqrt(2.0)) digits 6);
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,816:
 
=={{header|SequenceL}}==
<langsyntaxhighlight lang=sequencel>import <Utilities/Math.sl>;
 
agm(a, g) :=
Line 2,828:
agm(arithmeticMean, geometricMean);
 
main := agm(1.0, 1.0 / sqrt(2));</langsyntaxhighlight>
 
{{out}}
Line 2,836:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang=ruby>func agm(a, g) {
loop {
var (a1, g1) = ((a+g)/2, sqrt(a*g))
Line 2,844:
}
 
say agm(1, 1/sqrt(2))</langsyntaxhighlight>
{{out}}
<pre>0.8472130847939790866064991234821916364814</pre>
Line 2,855:
 
Better precision than this is not easily obtainable on the ZX81, unfortunately.
<langsyntaxhighlight lang=basic> 10 LET A=1
20 LET G=1/SQR 2
30 GOSUB 100
Line 2,865:
130 IF ABS(A-G)>.00000001 THEN GOTO 100
140 LET AGM=A
150 RETURN</langsyntaxhighlight>
{{out}}
<pre>0.84721309</pre>
Line 2,872:
{{works with|Smalltalk/X}}
That is simply a copy/paste of the already existing agm method in the Number class:
<langsyntaxhighlight lang=smalltalk>agm:y
"return the arithmetic-geometric mean agm(x, y)
of the receiver (x) and the argument, y.
Line 2,890:
gi := gn.
] doUntil:[ delta < epsilon ].
^ ai</langsyntaxhighlight>
 
<langsyntaxhighlight lang=smalltalk>Transcript showCR: (24 agm:6).
Transcript showCR: ( (1/2) agm:(1/6) ).
Transcript showCR: (1 agm:(1 / 2 sqrt)).</langsyntaxhighlight>
{{out}}
<pre>13.4581714817256
Line 2,903:
{{works with|oracle|11.2 and higher}}
The solution uses recursive WITH clause (aka recursive CTE, recursive query, recursive factored subquery). Some, perhaps many, but not all SQL dialects support recursive WITH clause. The solution below was written and tested in Oracle SQL - Oracle has supported recursive WITH clause since version 11.2.
<langsyntaxhighlight lang=sql>with
rec (rn, a, g, diff) as (
select 1, 1, 1/sqrt(2), 1 - 1/sqrt(2)
Line 2,915:
from rec
where diff <= 1e-38
;</langsyntaxhighlight>
 
 
Line 2,925:
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang=sml>
fun agm(a, g) = let
fun agm'(a, g, eps) =
Line 2,934:
in agm'(a, g, 1e~15)
end;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,941:
 
=={{header|Stata}}==
<langsyntaxhighlight lang=stata>mata
 
real scalar agm(real scalar a, real scalar b) {
Line 2,954:
 
agm(1,1/sqrt(2))
end</langsyntaxhighlight>
{{out}}
<pre>.8472130848</pre>
 
=={{header|Swift}}==
<langsyntaxhighlight lang=Swift>import Darwin
 
enum AGRError : Error {
Line 2,989:
} catch {
print("agr is undefined when a * g < 0")
}</langsyntaxhighlight>
{{out}}
<pre>0.847213084835193</pre>
Line 2,995:
=={{header|Tcl}}==
The tricky thing about this implementation is that despite the finite precision available to IEEE doubles (which Tcl uses in its implementation of floating point arithmetic, in common with many other languages) the sequence of values does not ''quite'' converge to a single value; it gets to within a ULP and then errors prevent it from getting closer. This means that an additional termination condition is required: once a value does not change (hence the <code>old_b</code> variable) we have got as close as we can. Note also that we are using exact equality with floating point; this is reasonable because this is a rapidly converging sequence (it only takes 4 iterations in this case).
<langsyntaxhighlight lang=tcl>proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
Line 3,004:
}
 
puts [agm 1 [expr 1/sqrt(2)]]</langsyntaxhighlight>
Output:
<pre>0.8472130847939792</pre>
 
=={{header|TI-83 BASIC}}==
<langsyntaxhighlight lang=ti83b>1→A:1/sqrt(2)→G
While abs(A-G)>e-15
(A+G)/2→B
sqrt(AG)→G:B→A
End
A</langsyntaxhighlight>
{{out}}
<pre>.8472130848</pre>
Line 3,021:
{{works with|ksh93}}
ksh is one of the few unix shells that can do floating point arithmetic (bash does not).
<langsyntaxhighlight lang=bash>function agm {
float a=$1 g=$2 eps=${3:-1e-11} tmp
while (( abs(a-g) > eps )); do
Line 3,032:
}
 
agm $((1/sqrt(2))) 1</langsyntaxhighlight>
 
{{output}}
Line 3,043:
0.8472130848</pre>
 
You can get a more approximate convergence by changing the while condition to compare the numbers as strings: change <langsyntaxhighlight lang=bash>while (( abs(a-g) > eps ))</langsyntaxhighlight> to <langsyntaxhighlight lang=bash>while [[ $a != $g ]]</langsyntaxhighlight>
 
=={{header|VBA}}==
<langsyntaxhighlight lang=vb>Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
Line 3,057:
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub</langsyntaxhighlight>{{out}}
<pre> 0,853553390593274
0,847224902923494
Line 3,066:
=={{header|VBScript}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang=vb>
<lang vb>
Function agm(a,g)
Do Until a = tmp_a
Line 3,077:
 
WScript.Echo agm(1,1/Sqr(2))
</syntaxhighlight>
</lang>
 
{{Out}}
Line 3,085:
{{trans|C#}}
===Double, Decimal Versions===
<langsyntaxhighlight lang=vbnet>Imports System.Math
Imports System.Console
 
Line 3,114:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Double result: 0.847213084793979
Line 3,122:
{{trans|C#}}
{{Libheader|System.Numerics}}
<langsyntaxhighlight lang=vbnet>Imports System.Math
Imports System.Console
Imports BI = System.Numerics.BigInteger
Line 3,157:
End Sub
End Module</langsyntaxhighlight>
{{out}}
<pre style="height:64ex; overflow:scroll; white-space: pre-wrap;">0.8472130847939790866064991234821916364814459103269421850605793726597340048341347597232002939946112299421222856252334109630979626658308710596997136359833842511763268142890603897067686016166500482811887218977133094117674620199443929629021672891944995072316778973468639476066710579805578521731403493983042004221192160398395535950981936412937163406460295999679705994343516020318426487569502421748638554059819545816017424178878541927588041627190120855876856483268341404312184008040358092045594943138778151209265222545743971242868207663409547336745996217926655353486256861185433086262872872875630108355631935706687147856390889821151088363521476969796126218329432284178681137684451700181460219136940270209459966835135963278808042743454817445873632200251539529362658066141983656164916262596074347237066169023530800173753128478525584306319074542749341526857906552694060031475910203327467196861247963255105546489028208552974396512499400966255286606758044873538921857014011677169765350140849524768489932573213370289846689391946658618737529663875622660459147770442046810892565844083803204091061900315370673411959410100747433105990550582052432600995169279241747821697678106168369771411073927334392155014302200708736736596227214925877619285105238036702689046390962190766364423553808590294523406519001334234510583834171218051425500392370111132541114461262890625413355052664365359582455215629339751825147065013464104705697935568130660632937334503871097709729487591717901581732028157828848714993134081549334236779704471278593761859508514667736455467920161593422399714298407078888227903265675159652843581779572728480835648996350440414073422611018338354697596266333042208499985230074270393027724347497971797326455254654301983169496846109869074390506801376611925291977093844129970701588949316666116199459226501131118396635250253056164643158720845452298877547517727274765672164898291823923889520720764283971088470596035692199292183190154814128076659269829446445714923966632997307581390495762243896242317520950731901842446244237098642728114951118082282605386248461767518014098312749725765198375649235690280021617490553142720815343954059556357637112728165705973733744297003905604015638866307222570038923015911237696012158008177907786335124086243107357158376592650454665278733787444483440631024475703968125545398226643035341641303561380163416557526558975294452116687345122019122746673319157124076375382110696814107692639007483317574339675231966033086497357138387419609898383220288269488219130281936694995442224069727616862136951165783888501219909616065545461154325314816424933269479700415949147632311292059351651899794335004597628821729262591808940550843146639378254833513955019065337087206206402407705607584879649984365159272826453442863661541914258577710675618501727803328717519518930503180550524542602233552290077141812879865435118791800635627959362476826778641224946033812608262825409889531252767753465624327921451122955551603181843313369296172304178385515712556740498341666592696958000895372457305769454227537216020968719147039887846636724326270619112707171659082464004167994112040565710364083000241929439855307399465653967781049270105541035951333943219992506667620207839469555376055179640100974921885631130101781388857879381317209594806253920130098365028791769582798590527994772194179799702494306215841946888532811549772157996019440962347768614408507573928429882375939682322367058033413477462311289762585932437663177897491107726190970448952220450963072551559009382490402136480779203476721504856844602255440999282616317431264228578762898338065072202301037175314926350463106018857377256700661838129058063895450812703131137104371613583348806583395543121790134839883321641305763524471251153947206667033010134871651632411382881763983962952612114126321979596509865678675525076076042409590751752302194610453256433324961490125353332922372386894812788502013596630537605584935892839163046940388785496002747148719780145765957904958580226006609952496736432496683346176010660815670697514238186650361083885220976165500251607311499216129477579019972924868963822060380876027628167237016681910663358577515465038133423672234764202655856558846416010210540489855618711473588497637840648642679818650448631907747038228671143515112300360708657429886477146674733750114345818852797006056211724692174847180694866251199472893444270378304620707354938052872720621560630718828685805645211106967080285699069825769177220998671959968507790681443494932804976811543680463259938693076235070999518295129581121235707245383354826190752395158273098248180549665897909168867984071707793705959045775840910473413109604194111357756620727337797833203797301137672658535747710279781409721309612142393854737462769615041307952837372882050658719152259765084027796991761175393006725492491229845082362975568722711065849435533850494532638736489804606655979954360169503092790092450057856477235876198848986034412195340795369002996411974549060741600978859537660722905160772428590070901156639138364299041220826769629797867649032356499981990765997439870548648769091024911927099968275697011368762244046402960383700066212734577664709711326374656811502985863032260337383421358423937896114617192083071953915643782093641496780334152464507396683173198363362743392555311712019454146844880895622417898031894341231284027858378289009624209541345002101072736323285272576209646851994468240550629391742053301706461917215178844296705314335503772310709716080285145314144106105023117310877779933248932087727229897821330120834074305604998159963202687793307156940302439156118926767517249511766526248547096041991473113657920697330996088897286789780735587578500623575157123771653042063631002703129296694025421967877168846655727580898306467662007014679585693082220620905330827782226503112520278733512519159918893900284319218166686548434879621972211763904959895793607330943697457628943200384117552941594754747183936381144125610351023459581080768558985657007445308909428669251190101718122826689349269528261052518556736045877702288147821446968500918347219741420546128072347950059811766364526150190788545471193803557145930744635656260752787518824386409506964649815131170591457990619376560858650175616864501924098327235724333688813080022186368700209641119724303603558649793773314916749593151188673535025505982303047060284740458456676849620934506396302909441632516408692889814507247877727673378033828929504978384342943766566737297587430575141036417476861639624198941904730996100228428079444920026904845254139188246001559089131943255610365769362364161784646693141456109984038312265504115251494445380042090428718182468431624610552637677520970104063944687837375017436089751693486887651283453677552786547090231542029453873076141196649767521919808902105772633472397958968722923357769041244458682297806209887089816018179521454920370956252850733023255060096611329479148443416687429872654204083552056456404421174124065041932362831296643126330768715450444950733554418200793669701331244638824360062439816712409346806322169771701563590417609841261977801052586956634654144702511135382841010278579543061802357275500930513955637771043922799597114118278203358118398952338720119626666828781215343331193353019800652511924103594315072427251589774226901431325149775220621148653209528291784172678852791825950189428306645453380829438548491390660090152646315666940813051689857738445716110134773528439558663918031477128997248977232695083095920860316390860179422146804892537147135669490647597566350405076105930300153453613446834614136284840473063909580064862482211399539962122107992774053203059756987131501429238941821989218445861496845306346078287058864262560349767113385390753047360747520569725532663517964059488138127648519130232826129551720747594498863925111049785977410104647258831744969489273332281068408949475978706769012216951869658194406136694310323411619613160554381608728305543504819071159752742665917363693001980988797627218662628543311906086034280619151845297823703639898449414417889008602782220998390227472837967411429578924346545640402855167478372538831386154780508035236893583332887355879794886804980971406868936719416711504307402575102269081707385928535837390976424975922421061832372517021428320986753744507133218963666908565634963306077455683011837149400258404997766113525532847665618870592978212729899729592794781820428719807102278646183807006401083138975677112754136221127444534535584959769252575758312999039536959893249951324106784265611556743660088737484274038234811784911002123537108015334407708175281579422928548731689863980071896268684985779061942582000173178473797975815609269087287850270024414741281953578873964745859459899535543412801653553049058528794674398220606230386688852700505218904927782197514115595435549125326115087432280435609563176116321811794164884206928474315699133677787956913705592704959893911100786224112449931719539890308215307126971807352814294437374058180589784287101566325873726600012296180403780429093175160473979931236882466314524590792512088916974765430245705320638670468411054034201437664442213212750799846299157010147106552946146746392249574530619682203425444816247545977269653430250686824205288099692448923652171403817749282935917315481284919621433304080904306867233682060716291289398517406255904282247558159509102324206160816363511440953267967974466214658121897383725705201831800678505181233270743236051760236565304605919728246762046497950757124332306210615236617229324468286251110577832854712371857906482302429199129753477340618812393224405123793229248698239302094605799468502209356458018864737205798950819968285087908120645175464792846657029993496146354533816989879012073959534299458051884682918835631136138879631316173442207506218212945047503433730640140356614106403320867621443183928438969994268286836082535591242751488383392264668222963323657488981599104902374571278077062853236895690028469742954774248422335523859049299225453318270693966088603518491166875108552006265340966412611220069290556369052744064893640087015171662929356529921474420793873710647399136453402185931518201576110059405556600166318190916348212818643068418256991194316266715898588673650488980580832972145195811525832974358064432698289209364284959616975339927502383832695801109608954786457256109785378297307074918168744735731189049849490781632210127110919398357638892753131749978321368280932894349330930087868884127092076359007648065118301317440813138170776478562086983456849957696333241556699085937149528437303782174166781012624737754844959408277598042857813775448446192929537153359741871355556678028606484917974827559022377376189703770332489774349235376523557139076431488967144133099539679871046284747721772185865851985971282165739148574494328320308464163956096301047370473988450307936956928683464113764226308568695688152053749196294562881085987015910764955019272667378276517237450013662421051146709184898952269727656206976263055094938932099216377529415335060027109430018977339221845390337351007942764665232509045377940478212355620488638969640291029182673024368888013982750049655688955540362739754118359277009094291839958396298535952123465573707751680432023872401008786292362558484920221296055948232317635214207117650427699747801290249150914873347204981208353486521246233538858471700470120592394582541522312967601307268280232044633644234100026474341568399123881048049819491200940244895720301881220640996997340843736095812449945913231793359333819197360248853375641030435643732302001328359990615298394916710687997693926699033522064083729586994304357670917169796698442332656830732550000321312902706719106342428311390049478179307304556219943912072209495471916547109605404919944186051724981471812994063119290173738101176617356976495636675620278895592099504686163440305250658681735840269428736633431167832903837475658050990783985384926064721246565130660487673608585790218386643241627198210378772796337736742692945663985470529377745854692207002046330357343505517537014050310355526578082729897049230547545589009275410944504014157125357682801074915174627928533783099570631952876838237806368177841661186334747789420166190186143388804514884174361681454810362321037643274595653364629397295294049952661691181657740018116146497654407589150912557599100855273107733703213603505619407350405223414533224306604743600257212590127202517146952605462439215815151732661454812243619860357386922465403688559787750083268386930674253759349376972691382532780570135683441862315010318955128705494038594760949278590520009881447715839714713971813720554960331191642239195313230213875992717401904622413925914800620171561815889352945121978193704745708538695427900233080410588007250947512318930796844637224171170594606197614751977323896101315556406372309310279476973938229476346893933755946893665094049910252612163538072005644241026471164639800490998535570282059396054554479255558624918709232180130454102936332893619326596350851413637207293142767763267817840066780089558654877782630822818446508158509625695020697797889664140551101421185533444015948880284701657904464926309216120238068566472631611326995533585414320547442896728173291714010643730593960222482733969720865809194288803963344344876467583385597351333330628439786357062196382217705500672607607570202305548328439335937369624085404957344415141889143812206076832329063384332685935928226648361622876815670931303789678327741487845287838232474038340893449427806045589018183673133602271167285304427194507315740913600066356089181219040305019319028163972135790696025211929562455952835850442627787993214468221041325612271290302469610374855134599106662606082143546126463790846952338680559237822828610361386416013753920426888371192602742087474507782730180882648297991489233434653363930327991816476995529468892904060335470265188317825821391915073117022336839564945335630414192442838503954209073337511117053790819768061378846157004292392264788138228486672543415580694421193506836000488465561599083339184724263183698928130695654949153165010313216361224018298711517222401523368101476246169896417259748838727189598765602350324828709741468793415378708814573190327920453219231685852735108372055942456601545647944675449566859142997988233179819059574125368681032194798082603876241044848730208905065871934264174092007936669883601462309762759844113071525758916288010581709353072588887654386253201848624931923638568216562603110434528313030704972291334873033240933736956347974889824930017415805659182123288343858101250171537305398462043432455721482088547523494730467761429282915391485852688505423074450548192619166975975031503447208211845313907683486006908772752077246485706597636740936173143436990399498908375710246545650814962015988805204483379491707040848303909417512426275869868668644293498242419667403627076032399201407183071270759837132000712447159523642782162488472933913713634046138974088894178399320090051543608421618891328957740354384456107645016010462709579098652495342014766016330458293537653454523438667413798731255017029554582809547897542497367109038598264606895622241257303208140890607025206140457815282368504505765710043804228592032720729190222134651835930255942940875306994701101153416476785623543575023993736414532895773499876167502240919794121893188059017977444329403624038551082491954751841177014150820554999148803286500065069030165028455616533514890711974194172310029663247936640825364542104897640445108081123906368188594908660418340025631562661211506365309297219580687177632051461355581309500814563826112416521487163593643553646268872746276680368630680088231249970572706496265335285424273723449757482776061300818063419639083097882249478922949525891665782610044424440110326748539620120023397129834624242363283711074267309902126029110038109050751840523266273905031934856015485510632624318778970878895198168073096354223096005536267735905099473408744371024816727970009494589707630185344952680106730984246828848883760016695887137355969244555238536396178788134209309376484848406842940499731494663578455826688245825356635393289729316700066238128368519670627697889769929009597838069557440769080950069594659578325366066060213000525012998145215099629307110700615796004759918829827472751877492472674770755413679265775060149528336859838085353420874215682758801259992855903410097963019943741001394975591822918846705741010634931594527954742032057295356596869586863097328488381174243827058441735659667485315202886191192125286398739560928127513223214119754229343092375569339614672740517569529376699061052365448344078610425576694541873486379356070861240473688356773437140126350120823765176390562050604076894729400293162079760342896846897639867830553941515230713725560502914671175123451932131962571791940911728951123948113598860588062424037835751996487088330150679210175429060531418836978611027896830689666851868410470182364780700615529883149883111601949965815038674390467105247175993726709203381051984777006122752302698038537619917731907133105816779008651480172440446403764720673784583395382889380902941273987910475254258486561698048543296782281040453997661165123290729161619992628751086519341731116513305659182981762584769428708454819029344222186027977405519291266188948708010515922860149238393490889782166965109499761673179583522105791358724355029782111425280584380959770472177893827382916471882671437865821461326011263516554280516418422188264141890686619186492751718984735037496602686033671961304915922609442146773092074476794711917820209913226872184947548378003848726148872742881265579174794634151444545105599464567614478293387968015412886418098284885525959617399177657635267081989985408930744564199296902459275405143647525648661932959903068323866757518479741015342911416508753572892479684280248440220211898390243430190746592470563991910024225814399068391457857458095344096826158489731615822039837691005171654390590093326827586419753439483771905973079465029210363641972615923872187876095687197681934481955852567024141433671590889694204781798936556351775101591005026585947279448642317311892727153525046034081896227383114600546852406398855471859684088277722162250586368419379964112646321070639818773794369650252104438622320671517228411475433482803041707675438555447584321271846396281391925884972509051040944134450429845346071848875654240709690138592611645519676563708429710676494635766201285381926791204110977805857352062737510466943591592074904378966129808716274322385039032007477854211063899544954185997641428116395197239708078986048758264126544825149923227286176571389697334537835963603962709038002668921324389159009375225033651171937770657226295341257068980907793198879997076783263303670667342657925395849950582363998610492878479976185891384024744790742355981796013254960652684988733518397287191251899388324341602608356164496670902390042273216221931567939944001215159910054381084520081133103207553492484487369268314444466610780275891777468369344585045949963237156043800258227618908603074550819931892899703285549507330240121766349515315827830897786432254556221744305752825143708087184314470811004510108612122699931396969361066523608721126359012344828262284427191281973187269761974740398071778378188160519801862257232970224762494767912932684020188061795236229174601398576604233579094407723017353015337974435643738584248250538061547193075224429309117207447677149522141919390974201716026970557825836923707297811545552570788004955666915477901830719591663516687057984336951611189153751912396714116378197000784953115386326766369269172016978409040396969804861828436417776804088449208439901095951205751340861060375353408155737087188313898337656322533650946010308686111901241541794900659835366926383515058402026098259570385429145865025692157987309807064597082326377138235585737704225628144262793497769429358804020882742028263786443615935817930817858306265712263479452174065216410798029333573961137404301928294367884626832432449078812684787281988676202931062510264948586549463964789154366240635570346688477784815271412470430646040615614277320107003575855033995279377529716156628381118518085523414187577256025217995103662771477552291036839539792329375184700131215428652464111526297830742328651189481978920924682746392250346179819781021313400022272303222234731521016033826145645816472110340883197207109422849637006090510260943044730126801795349152894613046101033061811314821366141874985466628809585678299308824993966655499624380015821082410781190328189506855057581990908848597095494573176672201417764187253816862426293852974092626551536758155537683368451820154793964862810533857810979434793077956125541240828563089647076354827276586047900779183041806574320855302776686899978897939486987950729652971448050889517660684386673056662911929857913206598752762097197279390208473846210277152094212386266930256260451209117402079233658157593274696841906354187366092529138116574357045728290417433832596884391356956442617823006949118156994294295529170211353842468704890572313005646106202029653246628477843902025194715815133791174898257040115532858624973690714844800747184719290671002133191274834310662201874141841328708920709275866745037664169280121112867057832132585948539987132879098472640550013972043153470930436509718084070853723316111111611632600262171748813737621046013600544051850633175245231989785291065646466038278748870331134307620041356514295482843502245454400571392386492526283423907951705366640483826875013469850263767974528926285288366544314868036628329638912254207094687335597669512007687507292940623176435604796651807847095408991068514998003358735387989422028901542800717906482276185298683079286137204396993726503610285463352157718364571843381950031926272352293654343387522809514152498052577486366048613580539162662183475105825647260311633442002377527140625112075332294909525522330744664115572260242435895269482927435844022622001466247093866533879048392320516224276433339282642640953964341822416705658461244760448817737705782669080880834418822622611342632727419248415651121035047131961583094994438779439078380664656207143187309895280874153167621657602227990850199615587578332393883365169478142077533262283694526612005465820771400826060398839255150948861553177333447506822679211849690448880479070102043288205874672361672971246062341973369704807867768609989464712379097525706498042381815865399434983035941162258347729020489356838477197804973214911448748749915616679253857438010864500220134843719609727912761136925035123155282535741655826107266099467657016111855684257826878422197833994329148734893923892153298966294232703135845615804723993624827409373966761563257981994036006655039613941881183164267144485664874468348587099434743710128859267552473831462181434321232124758618476925803128913233878664527525204324484796532776273320171351979849530142473805976430318655810403609897537469226336015596525652284888167037460054235043655813438329870872734142062859147847007274999414885129441657918212383876056572545671794085637289277002790218604788423519924573051811976377731594412994393860534559159658127123862955315918182841923881357245009246238507097741891437575676886206936433608263660374355173185026954239766173038826275043838965247160428689739548061640664606565379050539422795708801840829664956978192406737307076253014257542221763860230431809477056758905681723033326311408802886092880151777469082375063137750925275331638009836786645991949881018108222446858443984865972449621097999331605268587810061927125889694400669979755648800940895626242917531834388920035663113368763931463847812763130237825562198311791061780856687903309789539747505239545316630638169559777653347655949908779202359718666623572487055558216484036084925217803431104356647417600193631613474196113126657206064282217690428541246560204561459484317744683213906021267727411189443675804442911583757423572500214191467493342871160840582639470485636370375679604797073490813681083838562113841391587052553615073991983125473434527404596547926972539542447555990332809716643578039646945749813368621152410490288581779206318208255069166455507840899628333174744873951607229399258854694188637978240144635295264982572856632103053550891057171748674115218494774077589151115819489068851971959768129214023511454382738867557288320426608338030759515727545577639726238470674634011626346953231815229549718996906470438903536574430644436472716449550868519871817092814068746449470806856174570885105064766494332205391085097539987897980672278869943134632799032372604933150163386774039430519493297142505321117669011820293604482694166301309801111227443654953271242388534939973277749999335296667138307969441135719079969506099821923206878892624416110175909254904610286553512032488285673735148429324009831633211264460376172046209384270528903772251057643968938983722779640468452705694321085455273829462711022737243290606294601651732654594463569861350966095209962038508010899673666470073918705760679801337058347046567503369379598928154437380765511031719081985901371088639600700705631873099251480947989238619052479230983309717938226245725600119571130722386790431255742179135633111146646083268382596762356018472772209198013121983224179079476134977421748168833934278876403014334318798493417716613256506422668264638388429786875443810986754386459491846082078633346046469418429778813833857755519670005669840456587642130852057050148314568259387702428619224671173187370822224627538313365937868201435535126600146246249435880806572693573084485615073901842761167215162204840459913839674251648</pre>
 
=={{header|Vlang}}==
<langsyntaxhighlight lang=vlang>import math
const ep = 1e-14
Line 3,177:
fn main() {
println(agm(1.0, 1.0/math.sqrt2))
}</langsyntaxhighlight>
Using standard math module
<langsyntaxhighlight lang=vlang>import math.stats
import math
 
fn main() {
println(stats.geometric_mean<f64>([1.0, 1.0/math.sqrt2]))
}</langsyntaxhighlight>
{{out}}
<pre>0.8408964152537145
Line 3,191:
=={{header|Wren}}==
{{trans|Go}}
<langsyntaxhighlight lang=ecmascript>var eps = 1e-14
 
var agm = Fn.new { |a, g|
Line 3,202:
}
 
System.print(agm.call(1, 1/2.sqrt))</langsyntaxhighlight>
 
{{out}}
Line 3,210:
 
=={{header|XPL0}}==
<langsyntaxhighlight lang=XPL0>include c:\cxpl\codesi;
real A, A1, G;
[Format(0, 16);
Line 3,219:
RlOut(0, A); RlOut(0, G); RlOut(0, A-G); CrLf(0);
until A=G;
]</langsyntaxhighlight>
 
Output:
Line 3,231:
=={{header|zkl}}==
{{trans|XPL0}}
<langsyntaxhighlight lang=zkl>a:=1.0; g:=1.0/(2.0).sqrt();
while(not a.closeTo(g,1.0e-15)){
a1:=(a+g)/2.0; g=(a*g).sqrt(); a=a1;
println(a," ",g," ",a-g);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,244:
</pre>
Or, using tail recursion
<langsyntaxhighlight lang=zkl>fcn(a=1.0, g=1.0/(2.0).sqrt()){ println(a," ",g," ",a-g);
if(a.closeTo(g,1.0e-15)) return(a) else return(self.fcn((a+g)/2.0, (a*g).sqrt()));
}()</langsyntaxhighlight>
{{out}}
<pre>
Line 3,258:
=={{header|ZX Spectrum Basic}}==
{{trans|ERRE}}
<langsyntaxhighlight lang=zxbasic>10 LET a=1: LET g=1/SQR 2
20 LET ta=a
30 LET a=(a+g)/2
Line 3,264:
50 IF a<ta THEN GO TO 20
60 PRINT a
</syntaxhighlight>
</lang>
{{out}}
<pre>0.84721309</pre>
10,327

edits