Arithmetic-geometric mean: Difference between revisions

Content added Content deleted
No edit summary
m (syntax highlighting fixup automation)
Line 22: Line 22:
=={{header|11l}}==
=={{header|11l}}==
{{trans|Python}}
{{trans|Python}}
<lang 11l>F agm(a0, g0, tolerance = 1e-10)
<syntaxhighlight lang=11l>F agm(a0, g0, tolerance = 1e-10)
V an = (a0 + g0) / 2.0
V an = (a0 + g0) / 2.0
V gn = sqrt(a0 * g0)
V gn = sqrt(a0 * g0)
Line 29: Line 29:
R an
R an
print(agm(1, 1 / sqrt(2)))</lang>
print(agm(1, 1 / sqrt(2)))</syntaxhighlight>
{{out}}
{{out}}
<pre>0.847213</pre>
<pre>0.847213</pre>
Line 35: Line 35:
=={{header|360 Assembly}}==
=={{header|360 Assembly}}==
For maximum compatibility, this program uses only the basic instruction set.
For maximum compatibility, this program uses only the basic instruction set.
<lang 360asm>AGM CSECT
<syntaxhighlight lang=360asm>AGM CSECT
USING AGM,R13
USING AGM,R13
SAVEAREA B STM-SAVEAREA(R15)
SAVEAREA B STM-SAVEAREA(R15)
Line 125: Line 125:
LTORG
LTORG
YREGS
YREGS
END AGM</lang>
END AGM</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 132: Line 132:


=={{header|8th}}==
=={{header|8th}}==
<lang 8th>: epsilon 1.0e-12 ;
<syntaxhighlight lang=8th>: epsilon 1.0e-12 ;


with: n
with: n
Line 146: Line 146:
;with
;with
bye
bye
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 155: Line 155:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
{{libheader|Action! Real Math}}
<lang Action!>INCLUDE "H6:REALMATH.ACT"
<syntaxhighlight lang=Action!>INCLUDE "H6:REALMATH.ACT"


PROC Agm(REAL POINTER a0,g0,result)
PROC Agm(REAL POINTER a0,g0,result)
Line 191: Line 191:
Print(",") PrintR(g)
Print(",") PrintR(g)
Print(")=") PrintRE(res)
Print(")=") PrintRE(res)
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Arithmetic-geometric_mean.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Arithmetic-geometric_mean.png Screenshot from Atari 8-bit computer]
Line 199: Line 199:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions;
<syntaxhighlight lang=Ada>with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions;


procedure Arith_Geom_Mean is
procedure Arith_Geom_Mean is
Line 224: Line 224:
begin
begin
N_IO.Put(AGM(1.0, 1.0/Math.Sqrt(2.0)), Fore => 1, Aft => 17, Exp => 0);
N_IO.Put(AGM(1.0, 1.0/Math.Sqrt(2.0)), Fore => 1, Aft => 17, Exp => 0);
end Arith_Geom_Mean;</lang>
end Arith_Geom_Mean;</syntaxhighlight>


Output:<pre>0.84721308479397909</pre>
Output:<pre>0.84721308479397909</pre>
Line 232: Line 232:


Printing out the difference between the means at each iteration nicely demonstrates the quadratic convergence.
Printing out the difference between the means at each iteration nicely demonstrates the quadratic convergence.
<lang algol68>
<syntaxhighlight lang=algol68>
BEGIN
BEGIN
PROC agm = (LONG REAL x, y) LONG REAL :
PROC agm = (LONG REAL x, y) LONG REAL :
Line 256: Line 256:
printf (($l(-35,33)l$, agm (LONG 1.0, LONG 1.0 / long sqrt (LONG 2.0))))
printf (($l(-35,33)l$, agm (LONG 1.0, LONG 1.0 / long sqrt (LONG 2.0))))
END
END
</syntaxhighlight>
</lang>
Output:<pre>+1.707106781186547524400844362e +0 +2.928932188134524755991556379e -1
Output:<pre>+1.707106781186547524400844362e +0 +2.928932188134524755991556379e -1
+2.928932188134524755991556379e -1 +1.265697533955921916929670477e -2
+2.928932188134524755991556379e -1 +1.265697533955921916929670477e -2
Line 269: Line 269:


=={{header|APL}}==
=={{header|APL}}==
<lang APL>
<syntaxhighlight lang=APL>
agd←{(⍺-⍵)<10*¯8:⍺⋄((⍺+⍵)÷2)∇(⍺×⍵)*÷2}
agd←{(⍺-⍵)<10*¯8:⍺⋄((⍺+⍵)÷2)∇(⍺×⍵)*÷2}
1 agd ÷2*÷2
1 agd ÷2*÷2
</syntaxhighlight>
</lang>
Output: <pre>0.8472130848</pre>
Output: <pre>0.8472130848</pre>


Line 278: Line 278:
By functional composition:
By functional composition:


<lang AppleScript>-- ARITHMETIC GEOMETRIC MEAN -------------------------------------------------
<syntaxhighlight lang=AppleScript>-- ARITHMETIC GEOMETRIC MEAN -------------------------------------------------


property tolerance : 1.0E-5
property tolerance : 1.0E-5
Line 336: Line 336:
end script
end script
end if
end if
end mReturn</lang>
end mReturn</syntaxhighlight>
{{Out}}
{{Out}}
<pre>0.847213084835</pre>
<pre>0.847213084835</pre>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AHK>agm(a, g, tolerance=1.0e-15){
<syntaxhighlight lang=AHK>agm(a, g, tolerance=1.0e-15){
While abs(a-g) > tolerance
While abs(a-g) > tolerance
{
{
Line 351: Line 351:
}
}
SetFormat, FloatFast, 0.15
SetFormat, FloatFast, 0.15
MsgBox % agm(1, 1/sqrt(2))</lang>
MsgBox % agm(1, 1/sqrt(2))</syntaxhighlight>
Output:
Output:
<pre>0.847213084793979</pre>
<pre>0.847213084793979</pre>


=={{header|AWK}}==
=={{header|AWK}}==
<lang AWK>#!/usr/bin/awk -f
<syntaxhighlight lang=AWK>#!/usr/bin/awk -f
BEGIN {
BEGIN {
printf "%.16g\n", agm(1.0,sqrt(0.5))
printf "%.16g\n", agm(1.0,sqrt(0.5))
Line 372: Line 372:
return (x<0 ? -x : x)
return (x<0 ? -x : x)
}
}
</syntaxhighlight>
</lang>
Output
Output
<pre>0.8472130847939792</pre>
<pre>0.8472130847939792</pre>
Line 379: Line 379:
==={{header|BASIC}}===
==={{header|BASIC}}===
{{works with|QBasic}}
{{works with|QBasic}}
<lang qbasic>PRINT AGM(1, 1 / SQR(2))
<syntaxhighlight lang=qbasic>PRINT AGM(1, 1 / SQR(2))
END
END


Line 390: Line 390:
AGM = a
AGM = a
END FUNCTION</lang>
END FUNCTION</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 397: Line 397:


==={{header|BASIC256}}===
==={{header|BASIC256}}===
<lang BASIC256>print AGM(1, 1 / sqr(2))
<syntaxhighlight lang=BASIC256>print AGM(1, 1 / sqr(2))
end
end


Line 410: Line 410:


return a
return a
end function</lang>
end function</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 417: Line 417:


==={{header|Commodore BASIC}}===
==={{header|Commodore BASIC}}===
<lang commodorebasic>10 A = 1
<syntaxhighlight lang=commodorebasic>10 A = 1
20 G = 1/SQR(2)
20 G = 1/SQR(2)
30 GOSUB 100
30 GOSUB 100
Line 426: Line 426:
120 G = SQR(TA*G)
120 G = SQR(TA*G)
130 IF A<TA THEN 100
130 IF A<TA THEN 100
140 RETURN</lang>
140 RETURN</syntaxhighlight>


==={{header|BBC BASIC}}===
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<lang bbcbasic> *FLOAT 64
<syntaxhighlight lang=bbcbasic> *FLOAT 64
@% = &1010
@% = &1010
PRINT FNagm(1, 1/SQR(2))
PRINT FNagm(1, 1/SQR(2))
Line 443: Line 443:
UNTIL a = ta
UNTIL a = ta
= a
= a
</syntaxhighlight>
</lang>
Produces this output:
Produces this output:
<pre>
<pre>
Line 450: Line 450:


==={{header|GW-BASIC}}===
==={{header|GW-BASIC}}===
<lang gwbasic>10 A = 1
<syntaxhighlight lang=gwbasic>10 A = 1
20 G = 1!/SQR(2!)
20 G = 1!/SQR(2!)
30 FOR I=1 TO 20 'twenty iterations is plenty
30 FOR I=1 TO 20 'twenty iterations is plenty
Line 457: Line 457:
60 A = B
60 A = B
70 NEXT I
70 NEXT I
80 PRINT A</lang>
80 PRINT A</syntaxhighlight>


==={{header|IS-BASIC}}===
==={{header|IS-BASIC}}===
<lang IS-BASIC>100 PRINT AGM(1,1/SQR(2))
<syntaxhighlight lang=IS-BASIC>100 PRINT AGM(1,1/SQR(2))
110 DEF AGM(A,G)
110 DEF AGM(A,G)
120 DO
120 DO
Line 467: Line 467:
150 LOOP UNTIL A=TA
150 LOOP UNTIL A=TA
160 LET AGM=A
160 LET AGM=A
170 END DEF</lang>
170 END DEF</syntaxhighlight>


==={{header|True BASIC}}===
==={{header|True BASIC}}===
{{works with|QBasic}}
{{works with|QBasic}}
<lang qbasic>FUNCTION AGM (a, g)
<syntaxhighlight lang=qbasic>FUNCTION AGM (a, g)
DO
DO
LET ta = (a + g) / 2
LET ta = (a + g) / 2
Line 485: Line 485:


PRINT AGM(1, 1 / SQR(2))
PRINT AGM(1, 1 / SQR(2))
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 492: Line 492:


=={{header|bc}}==
=={{header|bc}}==
<lang bc>/* Calculate the arithmethic-geometric mean of two positive
<syntaxhighlight lang=bc>/* Calculate the arithmethic-geometric mean of two positive
* numbers x and y.
* numbers x and y.
* Result will have d digits after the decimal point.
* Result will have d digits after the decimal point.
Line 516: Line 516:


scale = 20
scale = 20
m(1, 1 / sqrt(2), 20)</lang>
m(1, 1 / sqrt(2), 20)</syntaxhighlight>


{{Out}}
{{Out}}
Line 522: Line 522:


=={{header|BQN}}==
=={{header|BQN}}==
<lang bqn>AGM ← {
<syntaxhighlight lang=bqn>AGM ← {
(|𝕨-𝕩) ≤ 1e¯15? 𝕨;
(|𝕨-𝕩) ≤ 1e¯15? 𝕨;
(0.5×𝕨+𝕩) 𝕊 √𝕨×𝕩
(0.5×𝕨+𝕩) 𝕊 √𝕨×𝕩
}
}


1 AGM 1÷√2</lang>
1 AGM 1÷√2</syntaxhighlight>
{{out}}
{{out}}
<pre>0.8472130847939792</pre>
<pre>0.8472130847939792</pre>
Line 533: Line 533:
=={{header|C}}==
=={{header|C}}==
===Basic===
===Basic===
<lang c>#include<math.h>
<syntaxhighlight lang=c>#include<math.h>
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
#include<stdlib.h>
Line 565: Line 565:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>


Original output:
Original output:
Line 579: Line 579:


===GMP===
===GMP===
<lang cpp>/*Arithmetic Geometric Mean of 1 and 1/sqrt(2)
<syntaxhighlight lang=cpp>/*Arithmetic Geometric Mean of 1 and 1/sqrt(2)


Nigel_Galloway
Nigel_Galloway
Line 612: Line 612:


return 0;
return 0;
}</lang>
}</syntaxhighlight>


The first couple of iterations produces:
The first couple of iterations produces:
Line 628: Line 628:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>namespace RosettaCode.ArithmeticGeometricMean
<syntaxhighlight lang=csharp>namespace RosettaCode.ArithmeticGeometricMean
{
{
using System;
using System;
Line 705: Line 705:
}
}
}
}
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>0.847213084835193</pre>
<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.
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===
===Using Decimal Type===
<lang csharp>using System;
<syntaxhighlight lang=csharp>using System;
 
 
class Program {
class Program {
Line 727: Line 727:
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
}
}
}</lang>
}</syntaxhighlight>
{{Out}}
{{Out}}
<pre>0.8472130847939790866064991235</pre>
<pre>0.8472130847939790866064991235</pre>
Line 734: Line 734:
{{Libheader|System.Numerics}}
{{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.
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.
<lang csharp>using static System.Math;
<syntaxhighlight lang=csharp>using static System.Math;
using static System.Console;
using static System.Console;
using BI = System.Numerics.BigInteger;
using BI = System.Numerics.BigInteger;
Line 763: Line 763:
WriteLine("0.{0}", CalcByAGM(digits));
WriteLine("0.{0}", CalcByAGM(digits));
if (System.Diagnostics.Debugger.IsAttached) ReadKey(); }
if (System.Diagnostics.Debugger.IsAttached) ReadKey(); }
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:64ex; overflow:scroll; white-space:pre-wrap;">0.8472130847939790866064991234821916364814459103269421850605793726597340048341347597232002939946112299421222856252334109630979626658308710596997136359833842511763268142890603897067686016166500482811887218977133094117674620199443929629021672891944995072316778973468639476066710579805578521731403493983042004221192160398395535950981936412937163406460295999679705994343516020318426487569502421748638554059819545816017424178878541927588041627190120855876856483268341404312184008040358092045594943138778151209265222545743971242868207663409547336745996217926655353486256861185433086262872872875630108355631935706687147856390889821151088363521476969796126218329432284178681137684451700181460219136940270209459966835135963278808042743454817445873632200251539529362658066141983656164916262596074347237066169023530800173753128478525584306319074542749341526857906552694060031475910203327467196861247963255105546489028208552974396512499400966255286606758044873538921857014011677169765350140849524768489932573213370289846689391946658618737529663875622660459147770442046810892565844083803204091061900315370673411959410100747433105990550582052432600995169279241747821697678106168369771411073927334392155014302200708736736596227214925877619285105238036702689046390962190766364423553808590294523406519001334234510583834171218051425500392370111132541114461262890625413355052664365359582455215629339751825147065013464104705697935568130660632937334503871097709729487591717901581732028157828848714993134081549334236779704471278593761859508514667736455467920161593422399714298407078888227903265675159652843581779572728480835648996350440414073422611018338354697596266333042208499985230074270393027724347497971797326455254654301983169496846109869074390506801376611925291977093844129970701588949316666116199459226501131118396635250253056164643158720845452298877547517727274765672164898291823923889520720764283971088470596035692199292183190154814128076659269829446445714923966632997307581390495762243896242317520950731901842446244237098642728114951118082282605386248461767518014098312749725765198375649235690280021617490553142720815343954059556357637112728165705973733744297003905604015638866307222570038923015911237696012158008177907786335124086243107357158376592650454665278733787444483440631024475703968125545398226643035341641303561380163416557526558975294452116687345122019122746673319157124076375382110696814107692639007483317574339675231966033086497357138387419609898383220288269488219130281936694995442224069727616862136951165783888501219909616065545461154325314816424933269479700415949147632311292059351651899794335004597628821729262591808940550843146639378254833513955019065337087206206402407705607584879649984365159272826453442863661541914258577710675618501727803328717519518930503180550524542602233552290077141812879865435118791800635627959362476826778641224946033812608262825409889531252767753465624327921451122955551603181843313369296172304178385515712556740498341666592696958000895372457305769454227537216020968719147039887846636724326270619112707171659082464004167994112040565710364083000241929439855307399465653967781049270105541035951333943219992506667620207839469555376055179640100974921885631130101781388857879381317209594806253920130098365028791769582798590527994772194179799702494306215841946888532811549772157996019440962347768614408507573928429882375939682322367058033413477462311289762585932437663177897491107726190970448952220450963072551559009382490402136480779203476721504856844602255440999282616317431264228578762898338065072202301037175314926350463106018857377256700661838129058063895450812703131137104371613583348806583395543121790134839883321641305763524471251153947206667033010134871651632411382881763983962952612114126321979596509865678675525076076042409590751752302194610453256433324961490125353332922372386894812788502013596630537605584935892839163046940388785496002747148719780145765957904958580226006609952496736432496683346176010660815670697514238186650361083885220976165500251607311499216129477579019972924868963822060380876027628167237016681910663358577515465038133423672234764202655856558846416010210540489855618711473588497637840648642679818650448631907747038228671143515112300360708657429886477146674733750114345818852797006056211724692174847180694866251199472893444270378304620707354938052872720621560630718828685805645211106967080285699069825769177220998671959968507790681443494932804976811543680463259938693076235070999518295129581121235707245383354826190752395158273098248180549665897909168867984071707793705959045775840910473413109604194111357756620727337797833203797301137672658535747710279781409721309612142393854737462769615041307952837372882050658719152259765084027796991761175393006725492491229845082362975568722711065849435533850494532638736489804606655979954360169503092790092450057856477235876198848986034412195340795369002996411974549060741600978859537660722905160772428590070901156639138364299041220826769629797867649032356499981990765997439870548648769091024911927099968275697011368762244046402960383700066212734577664709711326374656811502985863032260337383421358423937896114617192083071953915643782093641496780334152464507396683173198363362743392555311712019454146844880895622417898031894341231284027858378289009624209541345002101072736323285272576209646851994468240550629391742053301706461917215178844296705314335503772310709716080285145314144106105023117310877779933248932087727229897821330120834074305604998159963202687793307156940302439156118926767517249511766526248547096041991473113657920697330996088897286789780735587578500623575157123771653042063631002703129296694025421967877168846655727580898306467662007014679585693082220620905330827782226503112520278733512519159918893900284319218166686548434879621972211763904959895793607330943697457628943200384117552941594754747183936381144125610351023459581080768558985657007445308909428669251190101718122826689349269528261052518556736045877702288147821446968500918347219741420546128072347950059811766364526150190788545471193803557145930744635656260752787518824386409506964649815131170591457990619376560858650175616864501924098327235724333688813080022186368700209641119724303603558649793773314916749593151188673535025505982303047060284740458456676849620934506396302909441632516408692889814507247877727673378033828929504978384342943766566737297587430575141036417476861639624198941904730996100228428079444920026904845254139188246001559089131943255610365769362364161784646693141456109984038312265504115251494445380042090428718182468431624610552637677520970104063944687837375017436089751693486887651283453677552786547090231542029453873076141196649767521919808902105772633472397958968722923357769041244458682297806209887089816018179521454920370956252850733023255060096611329479148443416687429872654204083552056456404421174124065041932362831296643126330768715450444950733554418200793669701331244638824360062439816712409346806322169771701563590417609841261977801052586956634654144702511135382841010278579543061802357275500930513955637771043922799597114118278203358118398952338720119626666828781215343331193353019800652511924103594315072427251589774226901431325149775220621148653209528291784172678852791825950189428306645453380829438548491390660090152646315666940813051689857738445716110134773528439558663918031477128997248977232695083095920860316390860179422146804892537147135669490647597566350405076105930300153453613446834614136284840473063909580064862482211399539962122107992774053203059756987131501429238941821989218445861496845306346078287058864262560349767113385390753047360747520569725532663517964059488138127648519130232826129551720747594498863925111049785977410104647258831744969489273332281068408949475978706769012216951869658194406136694310323411619613160554381608728305543504819071159752742665917363693001980988797627218662628543311906086034280619151845297823703639898449414417889008602782220998390227472837967411429578924346545640402855167478372538831386154780508035236893583332887355879794886804980971406868936719416711504307402575102269081707385928535837390976424975922421061832372517021428320986753744507133218963666908565634963306077455683011837149400258404997766113525532847665618870592978212729899729592794781820428719807102278646183807006401083138975677112754136221127444534535584959769252575758312999039536959893249951324106784265611556743660088737484274038234811784911002123537108015334407708175281579422928548731689863980071896268684985779061942582000173178473797975815609269087287850270024414741281953578873964745859459899535543412801653553049058528794674398220606230386688852700505218904927782197514115595435549125326115087432280435609563176116321811794164884206928474315699133677787956913705592704959893911100786224112449931719539890308215307126971807352814294437374058180589784287101566325873726600012296180403780429093175160473979931236882466314524590792512088916974765430245705320638670468411054034201437664442213212750799846299157010147106552946146746392249574530619682203425444816247545977269653430250686824205288099692448923652171403817749282935917315481284919621433304080904306867233682060716291289398517406255904282247558159509102324206160816363511440953267967974466214658121897383725705201831800678505181233270743236051760236565304605919728246762046497950757124332306210615236617229324468286251110577832854712371857906482302429199129753477340618812393224405123793229248698239302094605799468502209356458018864737205798950819968285087908120645175464792846657029993496146354533816989879012073959534299458051884682918835631136138879631316173442207506218212945047503433730640140356614106403320867621443183928438969994268286836082535591242751488383392264668222963323657488981599104902374571278077062853236895690028469742954774248422335523859049299225453318270693966088603518491166875108552006265340966412611220069290556369052744064893640087015171662929356529921474420793873710647399136453402185931518201576110059405556600166318190916348212818643068418256991194316266715898588673650488980580832972145195811525832974358064432698289209364284959616975339927502383832695801109608954786457256109785378297307074918168744735731189049849490781632210127110919398357638892753131749978321368280932894349330930087868884127092076359007648065118301317440813138170776478562086983456849957696333241556699085937149528437303782174166781012624737754844959408277598042857813775448446192929537153359741871355556678028606484917974827559022377376189703770332489774349235376523557139076431488967144133099539679871046284747721772185865851985971282165739148574494328320308464163956096301047370473988450307936956928683464113764226308568695688152053749196294562881085987015910764955019272667378276517237450013662421051146709184898952269727656206976263055094938932099216377529415335060027109430018977339221845390337351007942764665232509045377940478212355620488638969640291029182673024368888013982750049655688955540362739754118359277009094291839958396298535952123465573707751680432023872401008786292362558484920221296055948232317635214207117650427699747801290249150914873347204981208353486521246233538858471700470120592394582541522312967601307268280232044633644234100026474341568399123881048049819491200940244895720301881220640996997340843736095812449945913231793359333819197360248853375641030435643732302001328359990615298394916710687997693926699033522064083729586994304357670917169796698442332656830732550000321312902706719106342428311390049478179307304556219943912072209495471916547109605404919944186051724981471812994063119290173738101176617356976495636675620278895592099504686163440305250658681735840269428736633431167832903837475658050990783985384926064721246565130660487673608585790218386643241627198210378772796337736742692945663985470529377745854692207002046330357343505517537014050310355526578082729897049230547545589009275410944504014157125357682801074915174627928533783099570631952876838237806368177841661186334747789420166190186143388804514884174361681454810362321037643274595653364629397295294049952661691181657740018116146497654407589150912557599100855273107733703213603505619407350405223414533224306604743600257212590127202517146952605462439215815151732661454812243619860357386922465403688559787750083268386930674253759349376972691382532780570135683441862315010318955128705494038594760949278590520009881447715839714713971813720554960331191642239195313230213875992717401904622413925914800620171561815889352945121978193704745708538695427900233080410588007250947512318930796844637224171170594606197614751977323896101315556406372309310279476973938229476346893933755946893665094049910252612163538072005644241026471164639800490998535570282059396054554479255558624918709232180130454102936332893619326596350851413637207293142767763267817840066780089558654877782630822818446508158509625695020697797889664140551101421185533444015948880284701657904464926309216120238068566472631611326995533585414320547442896728173291714010643730593960222482733969720865809194288803963344344876467583385597351333330628439786357062196382217705500672607607570202305548328439335937369624085404957344415141889143812206076832329063384332685935928226648361622876815670931303789678327741487845287838232474038340893449427806045589018183673133602271167285304427194507315740913600066356089181219040305019319028163972135790696025211929562455952835850442627787993214468221041325612271290302469610374855134599106662606082143546126463790846952338680559237822828610361386416013753920426888371192602742087474507782730180882648297991489233434653363930327991816476995529468892904060335470265188317825821391915073117022336839564945335630414192442838503954209073337511117053790819768061378846157004292392264788138228486672543415580694421193506836000488465561599083339184724263183698928130695654949153165010313216361224018298711517222401523368101476246169896417259748838727189598765602350324828709741468793415378708814573190327920453219231685852735108372055942456601545647944675449566859142997988233179819059574125368681032194798082603876241044848730208905065871934264174092007936669883601462309762759844113071525758916288010581709353072588887654386253201848624931923638568216562603110434528313030704972291334873033240933736956347974889824930017415805659182123288343858101250171537305398462043432455721482088547523494730467761429282915391485852688505423074450548192619166975975031503447208211845313907683486006908772752077246485706597636740936173143436990399498908375710246545650814962015988805204483379491707040848303909417512426275869868668644293498242419667403627076032399201407183071270759837132000712447159523642782162488472933913713634046138974088894178399320090051543608421618891328957740354384456107645016010462709579098652495342014766016330458293537653454523438667413798731255017029554582809547897542497367109038598264606895622241257303208140890607025206140457815282368504505765710043804228592032720729190222134651835930255942940875306994701101153416476785623543575023993736414532895773499876167502240919794121893188059017977444329403624038551082491954751841177014150820554999148803286500065069030165028455616533514890711974194172310029663247936640825364542104897640445108081123906368188594908660418340025631562661211506365309297219580687177632051461355581309500814563826112416521487163593643553646268872746276680368630680088231249970572706496265335285424273723449757482776061300818063419639083097882249478922949525891665782610044424440110326748539620120023397129834624242363283711074267309902126029110038109050751840523266273905031934856015485510632624318778970878895198168073096354223096005536267735905099473408744371024816727970009494589707630185344952680106730984246828848883760016695887137355969244555238536396178788134209309376484848406842940499731494663578455826688245825356635393289729316700066238128368519670627697889769929009597838069557440769080950069594659578325366066060213000525012998145215099629307110700615796004759918829827472751877492472674770755413679265775060149528336859838085353420874215682758801259992855903410097963019943741001394975591822918846705741010634931594527954742032057295356596869586863097328488381174243827058441735659667485315202886191192125286398739560928127513223214119754229343092375569339614672740517569529376699061052365448344078610425576694541873486379356070861240473688356773437140126350120823765176390562050604076894729400293162079760342896846897639867830553941515230713725560502914671175123451932131962571791940911728951123948113598860588062424037835751996487088330150679210175429060531418836978611027896830689666851868410470182364780700615529883149883111601949965815038674390467105247175993726709203381051984777006122752302698038537619917731907133105816779008651480172440446403764720673784583395382889380902941273987910475254258486561698048543296782281040453997661165123290729161619992628751086519341731116513305659182981762584769428708454819029344222186027977405519291266188948708010515922860149238393490889782166965109499761673179583522105791358724355029782111425280584380959770472177893827382916471882671437865821461326011263516554280516418422188264141890686619186492751718984735037496602686033671961304915922609442146773092074476794711917820209913226872184947548378003848726148872742881265579174794634151444545105599464567614478293387968015412886418098284885525959617399177657635267081989985408930744564199296902459275405143647525648661932959903068323866757518479741015342911416508753572892479684280248440220211898390243430190746592470563991910024225814399068391457857458095344096826158489731615822039837691005171654390590093326827586419753439483771905973079465029210363641972615923872187876095687197681934481955852567024141433671590889694204781798936556351775101591005026585947279448642317311892727153525046034081896227383114600546852406398855471859684088277722162250586368419379964112646321070639818773794369650252104438622320671517228411475433482803041707675438555447584321271846396281391925884972509051040944134450429845346071848875654240709690138592611645519676563708429710676494635766201285381926791204110977805857352062737510466943591592074904378966129808716274322385039032007477854211063899544954185997641428116395197239708078986048758264126544825149923227286176571389697334537835963603962709038002668921324389159009375225033651171937770657226295341257068980907793198879997076783263303670667342657925395849950582363998610492878479976185891384024744790742355981796013254960652684988733518397287191251899388324341602608356164496670902390042273216221931567939944001215159910054381084520081133103207553492484487369268314444466610780275891777468369344585045949963237156043800258227618908603074550819931892899703285549507330240121766349515315827830897786432254556221744305752825143708087184314470811004510108612122699931396969361066523608721126359012344828262284427191281973187269761974740398071778378188160519801862257232970224762494767912932684020188061795236229174601398576604233579094407723017353015337974435643738584248250538061547193075224429309117207447677149522141919390974201716026970557825836923707297811545552570788004955666915477901830719591663516687057984336951611189153751912396714116378197000784953115386326766369269172016978409040396969804861828436417776804088449208439901095951205751340861060375353408155737087188313898337656322533650946010308686111901241541794900659835366926383515058402026098259570385429145865025692157987309807064597082326377138235585737704225628144262793497769429358804020882742028263786443615935817930817858306265712263479452174065216410798029333573961137404301928294367884626832432449078812684787281988676202931062510264948586549463964789154366240635570346688477784815271412470430646040615614277320107003575855033995279377529716156628381118518085523414187577256025217995103662771477552291036839539792329375184700131215428652464111526297830742328651189481978920924682746392250346179819781021313400022272303222234731521016033826145645816472110340883197207109422849637006090510260943044730126801795349152894613046101033061811314821366141874985466628809585678299308824993966655499624380015821082410781190328189506855057581990908848597095494573176672201417764187253816862426293852974092626551536758155537683368451820154793964862810533857810979434793077956125541240828563089647076354827276586047900779183041806574320855302776686899978897939486987950729652971448050889517660684386673056662911929857913206598752762097197279390208473846210277152094212386266930256260451209117402079233658157593274696841906354187366092529138116574357045728290417433832596884391356956442617823006949118156994294295529170211353842468704890572313005646106202029653246628477843902025194715815133791174898257040115532858624973690714844800747184719290671002133191274834310662201874141841328708920709275866745037664169280121112867057832132585948539987132879098472640550013972043153470930436509718084070853723316111111611632600262171748813737621046013600544051850633175245231989785291065646466038278748870331134307620041356514295482843502245454400571392386492526283423907951705366640483826875013469850263767974528926285288366544314868036628329638912254207094687335597669512007687507292940623176435604796651807847095408991068514998003358735387989422028901542800717906482276185298683079286137204396993726503610285463352157718364571843381950031926272352293654343387522809514152498052577486366048613580539162662183475105825647260311633442002377527140625112075332294909525522330744664115572260242435895269482927435844022622001466247093866533879048392320516224276433339282642640953964341822416705658461244760448817737705782669080880834418822622611342632727419248415651121035047131961583094994438779439078380664656207143187309895280874153167621657602227990850199615587578332393883365169478142077533262283694526612005465820771400826060398839255150948861553177333447506822679211849690448880479070102043288205874672361672971246062341973369704807867768609989464712379097525706498042381815865399434983035941162258347729020489356838477197804973214911448748749915616679253857438010864500220134843719609727912761136925035123155282535741655826107266099467657016111855684257826878422197833994329148734893923892153298966294232703135845615804723993624827409373966761563257981994036006655039613941881183164267144485664874468348587099434743710128859267552473831462181434321232124758618476925803128913233878664527525204324484796532776273320171351979849530142473805976430318655810403609897537469226336015596525652284888167037460054235043655813438329870872734142062859147847007274999414885129441657918212383876056572545671794085637289277002790218604788423519924573051811976377731594412994393860534559159658127123862955315918182841923881357245009246238507097741891437575676886206936433608263660374355173185026954239766173038826275043838965247160428689739548061640664606565379050539422795708801840829664956978192406737307076253014257542221763860230431809477056758905681723033326311408802886092880151777469082375063137750925275331638009836786645991949881018108222446858443984865972449621097999331605268587810061927125889694400669979755648800940895626242917531834388920035663113368763931463847812763130237825562198311791061780856687903309789539747505239545316630638169559777653347655949908779202359718666623572487055558216484036084925217803431104356647417600193631613474196113126657206064282217690428541246560204561459484317744683213906021267727411189443675804442911583757423572500214191467493342871160840582639470485636370375679604797073490813681083838562113841391587052553615073991983125473434527404596547926972539542447555990332809716643578039646945749813368621152410490288581779206318208255069166455507840899628333174744873951607229399258854694188637978240144635295264982572856632103053550891057171748674115218494774077589151115819489068851971959768129214023511454382738867557288320426608338030759515727545577639726238470674634011626346953231815229549718996906470438903536574430644436472716449550868519871817092814068746449470806856174570885105064766494332205391085097539987897980672278869943134632799032372604933150163386774039430519493297142505321117669011820293604482694166301309801111227443654953271242388534939973277749999335296667138307969441135719079969506099821923206878892624416110175909254904610286553512032488285673735148429324009831633211264460376172046209384270528903772251057643968938983722779640468452705694321085455273829462711022737243290606294601651732654594463569861350966095209962038508010899673666470073918705760679801337058347046567503369379598928154437380765511031719081985901371088639600700705631873099251480947989238619052479230983309717938226245725600119571130722386790431255742179135633111146646083268382596762356018472772209198013121983224179079476134977421748168833934278876403014334318798493417716613256506422668264638388429786875443810986754386459491846082078633346046469418429778813833857755519670005669840456587642130852057050148314568259387702428619224671173187370822224627538313365937868201435535126600146246249435880806572693573084485615073901842761167215162204840459913839674251648</pre>
<pre style="height:64ex; overflow:scroll; white-space:pre-wrap;">0.8472130847939790866064991234821916364814459103269421850605793726597340048341347597232002939946112299421222856252334109630979626658308710596997136359833842511763268142890603897067686016166500482811887218977133094117674620199443929629021672891944995072316778973468639476066710579805578521731403493983042004221192160398395535950981936412937163406460295999679705994343516020318426487569502421748638554059819545816017424178878541927588041627190120855876856483268341404312184008040358092045594943138778151209265222545743971242868207663409547336745996217926655353486256861185433086262872872875630108355631935706687147856390889821151088363521476969796126218329432284178681137684451700181460219136940270209459966835135963278808042743454817445873632200251539529362658066141983656164916262596074347237066169023530800173753128478525584306319074542749341526857906552694060031475910203327467196861247963255105546489028208552974396512499400966255286606758044873538921857014011677169765350140849524768489932573213370289846689391946658618737529663875622660459147770442046810892565844083803204091061900315370673411959410100747433105990550582052432600995169279241747821697678106168369771411073927334392155014302200708736736596227214925877619285105238036702689046390962190766364423553808590294523406519001334234510583834171218051425500392370111132541114461262890625413355052664365359582455215629339751825147065013464104705697935568130660632937334503871097709729487591717901581732028157828848714993134081549334236779704471278593761859508514667736455467920161593422399714298407078888227903265675159652843581779572728480835648996350440414073422611018338354697596266333042208499985230074270393027724347497971797326455254654301983169496846109869074390506801376611925291977093844129970701588949316666116199459226501131118396635250253056164643158720845452298877547517727274765672164898291823923889520720764283971088470596035692199292183190154814128076659269829446445714923966632997307581390495762243896242317520950731901842446244237098642728114951118082282605386248461767518014098312749725765198375649235690280021617490553142720815343954059556357637112728165705973733744297003905604015638866307222570038923015911237696012158008177907786335124086243107357158376592650454665278733787444483440631024475703968125545398226643035341641303561380163416557526558975294452116687345122019122746673319157124076375382110696814107692639007483317574339675231966033086497357138387419609898383220288269488219130281936694995442224069727616862136951165783888501219909616065545461154325314816424933269479700415949147632311292059351651899794335004597628821729262591808940550843146639378254833513955019065337087206206402407705607584879649984365159272826453442863661541914258577710675618501727803328717519518930503180550524542602233552290077141812879865435118791800635627959362476826778641224946033812608262825409889531252767753465624327921451122955551603181843313369296172304178385515712556740498341666592696958000895372457305769454227537216020968719147039887846636724326270619112707171659082464004167994112040565710364083000241929439855307399465653967781049270105541035951333943219992506667620207839469555376055179640100974921885631130101781388857879381317209594806253920130098365028791769582798590527994772194179799702494306215841946888532811549772157996019440962347768614408507573928429882375939682322367058033413477462311289762585932437663177897491107726190970448952220450963072551559009382490402136480779203476721504856844602255440999282616317431264228578762898338065072202301037175314926350463106018857377256700661838129058063895450812703131137104371613583348806583395543121790134839883321641305763524471251153947206667033010134871651632411382881763983962952612114126321979596509865678675525076076042409590751752302194610453256433324961490125353332922372386894812788502013596630537605584935892839163046940388785496002747148719780145765957904958580226006609952496736432496683346176010660815670697514238186650361083885220976165500251607311499216129477579019972924868963822060380876027628167237016681910663358577515465038133423672234764202655856558846416010210540489855618711473588497637840648642679818650448631907747038228671143515112300360708657429886477146674733750114345818852797006056211724692174847180694866251199472893444270378304620707354938052872720621560630718828685805645211106967080285699069825769177220998671959968507790681443494932804976811543680463259938693076235070999518295129581121235707245383354826190752395158273098248180549665897909168867984071707793705959045775840910473413109604194111357756620727337797833203797301137672658535747710279781409721309612142393854737462769615041307952837372882050658719152259765084027796991761175393006725492491229845082362975568722711065849435533850494532638736489804606655979954360169503092790092450057856477235876198848986034412195340795369002996411974549060741600978859537660722905160772428590070901156639138364299041220826769629797867649032356499981990765997439870548648769091024911927099968275697011368762244046402960383700066212734577664709711326374656811502985863032260337383421358423937896114617192083071953915643782093641496780334152464507396683173198363362743392555311712019454146844880895622417898031894341231284027858378289009624209541345002101072736323285272576209646851994468240550629391742053301706461917215178844296705314335503772310709716080285145314144106105023117310877779933248932087727229897821330120834074305604998159963202687793307156940302439156118926767517249511766526248547096041991473113657920697330996088897286789780735587578500623575157123771653042063631002703129296694025421967877168846655727580898306467662007014679585693082220620905330827782226503112520278733512519159918893900284319218166686548434879621972211763904959895793607330943697457628943200384117552941594754747183936381144125610351023459581080768558985657007445308909428669251190101718122826689349269528261052518556736045877702288147821446968500918347219741420546128072347950059811766364526150190788545471193803557145930744635656260752787518824386409506964649815131170591457990619376560858650175616864501924098327235724333688813080022186368700209641119724303603558649793773314916749593151188673535025505982303047060284740458456676849620934506396302909441632516408692889814507247877727673378033828929504978384342943766566737297587430575141036417476861639624198941904730996100228428079444920026904845254139188246001559089131943255610365769362364161784646693141456109984038312265504115251494445380042090428718182468431624610552637677520970104063944687837375017436089751693486887651283453677552786547090231542029453873076141196649767521919808902105772633472397958968722923357769041244458682297806209887089816018179521454920370956252850733023255060096611329479148443416687429872654204083552056456404421174124065041932362831296643126330768715450444950733554418200793669701331244638824360062439816712409346806322169771701563590417609841261977801052586956634654144702511135382841010278579543061802357275500930513955637771043922799597114118278203358118398952338720119626666828781215343331193353019800652511924103594315072427251589774226901431325149775220621148653209528291784172678852791825950189428306645453380829438548491390660090152646315666940813051689857738445716110134773528439558663918031477128997248977232695083095920860316390860179422146804892537147135669490647597566350405076105930300153453613446834614136284840473063909580064862482211399539962122107992774053203059756987131501429238941821989218445861496845306346078287058864262560349767113385390753047360747520569725532663517964059488138127648519130232826129551720747594498863925111049785977410104647258831744969489273332281068408949475978706769012216951869658194406136694310323411619613160554381608728305543504819071159752742665917363693001980988797627218662628543311906086034280619151845297823703639898449414417889008602782220998390227472837967411429578924346545640402855167478372538831386154780508035236893583332887355879794886804980971406868936719416711504307402575102269081707385928535837390976424975922421061832372517021428320986753744507133218963666908565634963306077455683011837149400258404997766113525532847665618870592978212729899729592794781820428719807102278646183807006401083138975677112754136221127444534535584959769252575758312999039536959893249951324106784265611556743660088737484274038234811784911002123537108015334407708175281579422928548731689863980071896268684985779061942582000173178473797975815609269087287850270024414741281953578873964745859459899535543412801653553049058528794674398220606230386688852700505218904927782197514115595435549125326115087432280435609563176116321811794164884206928474315699133677787956913705592704959893911100786224112449931719539890308215307126971807352814294437374058180589784287101566325873726600012296180403780429093175160473979931236882466314524590792512088916974765430245705320638670468411054034201437664442213212750799846299157010147106552946146746392249574530619682203425444816247545977269653430250686824205288099692448923652171403817749282935917315481284919621433304080904306867233682060716291289398517406255904282247558159509102324206160816363511440953267967974466214658121897383725705201831800678505181233270743236051760236565304605919728246762046497950757124332306210615236617229324468286251110577832854712371857906482302429199129753477340618812393224405123793229248698239302094605799468502209356458018864737205798950819968285087908120645175464792846657029993496146354533816989879012073959534299458051884682918835631136138879631316173442207506218212945047503433730640140356614106403320867621443183928438969994268286836082535591242751488383392264668222963323657488981599104902374571278077062853236895690028469742954774248422335523859049299225453318270693966088603518491166875108552006265340966412611220069290556369052744064893640087015171662929356529921474420793873710647399136453402185931518201576110059405556600166318190916348212818643068418256991194316266715898588673650488980580832972145195811525832974358064432698289209364284959616975339927502383832695801109608954786457256109785378297307074918168744735731189049849490781632210127110919398357638892753131749978321368280932894349330930087868884127092076359007648065118301317440813138170776478562086983456849957696333241556699085937149528437303782174166781012624737754844959408277598042857813775448446192929537153359741871355556678028606484917974827559022377376189703770332489774349235376523557139076431488967144133099539679871046284747721772185865851985971282165739148574494328320308464163956096301047370473988450307936956928683464113764226308568695688152053749196294562881085987015910764955019272667378276517237450013662421051146709184898952269727656206976263055094938932099216377529415335060027109430018977339221845390337351007942764665232509045377940478212355620488638969640291029182673024368888013982750049655688955540362739754118359277009094291839958396298535952123465573707751680432023872401008786292362558484920221296055948232317635214207117650427699747801290249150914873347204981208353486521246233538858471700470120592394582541522312967601307268280232044633644234100026474341568399123881048049819491200940244895720301881220640996997340843736095812449945913231793359333819197360248853375641030435643732302001328359990615298394916710687997693926699033522064083729586994304357670917169796698442332656830732550000321312902706719106342428311390049478179307304556219943912072209495471916547109605404919944186051724981471812994063119290173738101176617356976495636675620278895592099504686163440305250658681735840269428736633431167832903837475658050990783985384926064721246565130660487673608585790218386643241627198210378772796337736742692945663985470529377745854692207002046330357343505517537014050310355526578082729897049230547545589009275410944504014157125357682801074915174627928533783099570631952876838237806368177841661186334747789420166190186143388804514884174361681454810362321037643274595653364629397295294049952661691181657740018116146497654407589150912557599100855273107733703213603505619407350405223414533224306604743600257212590127202517146952605462439215815151732661454812243619860357386922465403688559787750083268386930674253759349376972691382532780570135683441862315010318955128705494038594760949278590520009881447715839714713971813720554960331191642239195313230213875992717401904622413925914800620171561815889352945121978193704745708538695427900233080410588007250947512318930796844637224171170594606197614751977323896101315556406372309310279476973938229476346893933755946893665094049910252612163538072005644241026471164639800490998535570282059396054554479255558624918709232180130454102936332893619326596350851413637207293142767763267817840066780089558654877782630822818446508158509625695020697797889664140551101421185533444015948880284701657904464926309216120238068566472631611326995533585414320547442896728173291714010643730593960222482733969720865809194288803963344344876467583385597351333330628439786357062196382217705500672607607570202305548328439335937369624085404957344415141889143812206076832329063384332685935928226648361622876815670931303789678327741487845287838232474038340893449427806045589018183673133602271167285304427194507315740913600066356089181219040305019319028163972135790696025211929562455952835850442627787993214468221041325612271290302469610374855134599106662606082143546126463790846952338680559237822828610361386416013753920426888371192602742087474507782730180882648297991489233434653363930327991816476995529468892904060335470265188317825821391915073117022336839564945335630414192442838503954209073337511117053790819768061378846157004292392264788138228486672543415580694421193506836000488465561599083339184724263183698928130695654949153165010313216361224018298711517222401523368101476246169896417259748838727189598765602350324828709741468793415378708814573190327920453219231685852735108372055942456601545647944675449566859142997988233179819059574125368681032194798082603876241044848730208905065871934264174092007936669883601462309762759844113071525758916288010581709353072588887654386253201848624931923638568216562603110434528313030704972291334873033240933736956347974889824930017415805659182123288343858101250171537305398462043432455721482088547523494730467761429282915391485852688505423074450548192619166975975031503447208211845313907683486006908772752077246485706597636740936173143436990399498908375710246545650814962015988805204483379491707040848303909417512426275869868668644293498242419667403627076032399201407183071270759837132000712447159523642782162488472933913713634046138974088894178399320090051543608421618891328957740354384456107645016010462709579098652495342014766016330458293537653454523438667413798731255017029554582809547897542497367109038598264606895622241257303208140890607025206140457815282368504505765710043804228592032720729190222134651835930255942940875306994701101153416476785623543575023993736414532895773499876167502240919794121893188059017977444329403624038551082491954751841177014150820554999148803286500065069030165028455616533514890711974194172310029663247936640825364542104897640445108081123906368188594908660418340025631562661211506365309297219580687177632051461355581309500814563826112416521487163593643553646268872746276680368630680088231249970572706496265335285424273723449757482776061300818063419639083097882249478922949525891665782610044424440110326748539620120023397129834624242363283711074267309902126029110038109050751840523266273905031934856015485510632624318778970878895198168073096354223096005536267735905099473408744371024816727970009494589707630185344952680106730984246828848883760016695887137355969244555238536396178788134209309376484848406842940499731494663578455826688245825356635393289729316700066238128368519670627697889769929009597838069557440769080950069594659578325366066060213000525012998145215099629307110700615796004759918829827472751877492472674770755413679265775060149528336859838085353420874215682758801259992855903410097963019943741001394975591822918846705741010634931594527954742032057295356596869586863097328488381174243827058441735659667485315202886191192125286398739560928127513223214119754229343092375569339614672740517569529376699061052365448344078610425576694541873486379356070861240473688356773437140126350120823765176390562050604076894729400293162079760342896846897639867830553941515230713725560502914671175123451932131962571791940911728951123948113598860588062424037835751996487088330150679210175429060531418836978611027896830689666851868410470182364780700615529883149883111601949965815038674390467105247175993726709203381051984777006122752302698038537619917731907133105816779008651480172440446403764720673784583395382889380902941273987910475254258486561698048543296782281040453997661165123290729161619992628751086519341731116513305659182981762584769428708454819029344222186027977405519291266188948708010515922860149238393490889782166965109499761673179583522105791358724355029782111425280584380959770472177893827382916471882671437865821461326011263516554280516418422188264141890686619186492751718984735037496602686033671961304915922609442146773092074476794711917820209913226872184947548378003848726148872742881265579174794634151444545105599464567614478293387968015412886418098284885525959617399177657635267081989985408930744564199296902459275405143647525648661932959903068323866757518479741015342911416508753572892479684280248440220211898390243430190746592470563991910024225814399068391457857458095344096826158489731615822039837691005171654390590093326827586419753439483771905973079465029210363641972615923872187876095687197681934481955852567024141433671590889694204781798936556351775101591005026585947279448642317311892727153525046034081896227383114600546852406398855471859684088277722162250586368419379964112646321070639818773794369650252104438622320671517228411475433482803041707675438555447584321271846396281391925884972509051040944134450429845346071848875654240709690138592611645519676563708429710676494635766201285381926791204110977805857352062737510466943591592074904378966129808716274322385039032007477854211063899544954185997641428116395197239708078986048758264126544825149923227286176571389697334537835963603962709038002668921324389159009375225033651171937770657226295341257068980907793198879997076783263303670667342657925395849950582363998610492878479976185891384024744790742355981796013254960652684988733518397287191251899388324341602608356164496670902390042273216221931567939944001215159910054381084520081133103207553492484487369268314444466610780275891777468369344585045949963237156043800258227618908603074550819931892899703285549507330240121766349515315827830897786432254556221744305752825143708087184314470811004510108612122699931396969361066523608721126359012344828262284427191281973187269761974740398071778378188160519801862257232970224762494767912932684020188061795236229174601398576604233579094407723017353015337974435643738584248250538061547193075224429309117207447677149522141919390974201716026970557825836923707297811545552570788004955666915477901830719591663516687057984336951611189153751912396714116378197000784953115386326766369269172016978409040396969804861828436417776804088449208439901095951205751340861060375353408155737087188313898337656322533650946010308686111901241541794900659835366926383515058402026098259570385429145865025692157987309807064597082326377138235585737704225628144262793497769429358804020882742028263786443615935817930817858306265712263479452174065216410798029333573961137404301928294367884626832432449078812684787281988676202931062510264948586549463964789154366240635570346688477784815271412470430646040615614277320107003575855033995279377529716156628381118518085523414187577256025217995103662771477552291036839539792329375184700131215428652464111526297830742328651189481978920924682746392250346179819781021313400022272303222234731521016033826145645816472110340883197207109422849637006090510260943044730126801795349152894613046101033061811314821366141874985466628809585678299308824993966655499624380015821082410781190328189506855057581990908848597095494573176672201417764187253816862426293852974092626551536758155537683368451820154793964862810533857810979434793077956125541240828563089647076354827276586047900779183041806574320855302776686899978897939486987950729652971448050889517660684386673056662911929857913206598752762097197279390208473846210277152094212386266930256260451209117402079233658157593274696841906354187366092529138116574357045728290417433832596884391356956442617823006949118156994294295529170211353842468704890572313005646106202029653246628477843902025194715815133791174898257040115532858624973690714844800747184719290671002133191274834310662201874141841328708920709275866745037664169280121112867057832132585948539987132879098472640550013972043153470930436509718084070853723316111111611632600262171748813737621046013600544051850633175245231989785291065646466038278748870331134307620041356514295482843502245454400571392386492526283423907951705366640483826875013469850263767974528926285288366544314868036628329638912254207094687335597669512007687507292940623176435604796651807847095408991068514998003358735387989422028901542800717906482276185298683079286137204396993726503610285463352157718364571843381950031926272352293654343387522809514152498052577486366048613580539162662183475105825647260311633442002377527140625112075332294909525522330744664115572260242435895269482927435844022622001466247093866533879048392320516224276433339282642640953964341822416705658461244760448817737705782669080880834418822622611342632727419248415651121035047131961583094994438779439078380664656207143187309895280874153167621657602227990850199615587578332393883365169478142077533262283694526612005465820771400826060398839255150948861553177333447506822679211849690448880479070102043288205874672361672971246062341973369704807867768609989464712379097525706498042381815865399434983035941162258347729020489356838477197804973214911448748749915616679253857438010864500220134843719609727912761136925035123155282535741655826107266099467657016111855684257826878422197833994329148734893923892153298966294232703135845615804723993624827409373966761563257981994036006655039613941881183164267144485664874468348587099434743710128859267552473831462181434321232124758618476925803128913233878664527525204324484796532776273320171351979849530142473805976430318655810403609897537469226336015596525652284888167037460054235043655813438329870872734142062859147847007274999414885129441657918212383876056572545671794085637289277002790218604788423519924573051811976377731594412994393860534559159658127123862955315918182841923881357245009246238507097741891437575676886206936433608263660374355173185026954239766173038826275043838965247160428689739548061640664606565379050539422795708801840829664956978192406737307076253014257542221763860230431809477056758905681723033326311408802886092880151777469082375063137750925275331638009836786645991949881018108222446858443984865972449621097999331605268587810061927125889694400669979755648800940895626242917531834388920035663113368763931463847812763130237825562198311791061780856687903309789539747505239545316630638169559777653347655949908779202359718666623572487055558216484036084925217803431104356647417600193631613474196113126657206064282217690428541246560204561459484317744683213906021267727411189443675804442911583757423572500214191467493342871160840582639470485636370375679604797073490813681083838562113841391587052553615073991983125473434527404596547926972539542447555990332809716643578039646945749813368621152410490288581779206318208255069166455507840899628333174744873951607229399258854694188637978240144635295264982572856632103053550891057171748674115218494774077589151115819489068851971959768129214023511454382738867557288320426608338030759515727545577639726238470674634011626346953231815229549718996906470438903536574430644436472716449550868519871817092814068746449470806856174570885105064766494332205391085097539987897980672278869943134632799032372604933150163386774039430519493297142505321117669011820293604482694166301309801111227443654953271242388534939973277749999335296667138307969441135719079969506099821923206878892624416110175909254904610286553512032488285673735148429324009831633211264460376172046209384270528903772251057643968938983722779640468452705694321085455273829462711022737243290606294601651732654594463569861350966095209962038508010899673666470073918705760679801337058347046567503369379598928154437380765511031719081985901371088639600700705631873099251480947989238619052479230983309717938226245725600119571130722386790431255742179135633111146646083268382596762356018472772209198013121983224179079476134977421748168833934278876403014334318798493417716613256506422668264638388429786875443810986754386459491846082078633346046469418429778813833857755519670005669840456587642130852057050148314568259387702428619224671173187370822224627538313365937868201435535126600146246249435880806572693573084485615073901842761167215162204840459913839674251648</pre>


=={{header|C++}}==
=={{header|C++}}==
<lang c++>
<syntaxhighlight lang=c++>
#include<bits/stdc++.h>
#include<bits/stdc++.h>
using namespace std;
using namespace std;
Line 797: Line 797:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>




Line 806: Line 806:


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang lisp>(ns agmcompute
<syntaxhighlight lang=lisp>(ns agmcompute
(:gen-class))
(:gen-class))


Line 831: Line 831:


(println (agm one isqrt2))
(println (agm one isqrt2))
</syntaxhighlight>
</lang>
{{Output}}
{{Output}}
<pre>
<pre>
Line 838: Line 838:


=={{header|COBOL}}==
=={{header|COBOL}}==
<lang cobol>IDENTIFICATION DIVISION.
<syntaxhighlight lang=cobol>IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
DATA DIVISION.
Line 870: Line 870:
COMPUTE G = FUNCTION SQRT(G).
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).</lang>
COMPUTE DIFF = FUNCTION ABS(DIFF).</syntaxhighlight>
{{out}}
{{out}}
<pre>0.8472130847939792</pre>
<pre>0.8472130847939792</pre>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>(defun agm (a0 g0 &optional (tolerance 1d-8))
<syntaxhighlight lang=lisp>(defun agm (a0 g0 &optional (tolerance 1d-8))
(loop for a = a0 then (* (+ a g) 5d-1)
(loop for a = a0 then (* (+ a g) 5d-1)
and g = g0 then (sqrt (* a g))
and g = g0 then (sqrt (* a g))
until (< (abs (- a g)) tolerance)
until (< (abs (- a g)) tolerance)
finally (return a)))
finally (return a)))
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 891: Line 891:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio, std.math, std.meta, std.typecons;
<syntaxhighlight 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 {
real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe {
Line 903: Line 903:
void main() @safe {
void main() @safe {
writefln("%0.19f", agm(1, 1 / sqrt(2.0)));
writefln("%0.19f", agm(1, 1 / sqrt(2.0)));
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>0.8472130847939790866</pre>
<pre>0.8472130847939790866</pre>
Line 910: Line 910:
{{libheader| System.SysUtils}}
{{libheader| System.SysUtils}}
{{Trans|C#}}
{{Trans|C#}}
<lang Delphi>
<syntaxhighlight lang=Delphi>
program geometric_mean;
program geometric_mean;


Line 954: Line 954:
writeln(format('The arithmetic-geometric mean is %.6f', [agm(x, y)]));
writeln(format('The arithmetic-geometric mean is %.6f', [agm(x, y)]));
readln;
readln;
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>Enter two numbers:1
<pre>Enter two numbers:1
Line 962: Line 962:
=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
We use the '''(~= a b)''' operator which tests for |a - b| < ε = (math-precision).
We use the '''(~= a b)''' operator which tests for |a - b| < ε = (math-precision).
<lang scheme>
<syntaxhighlight lang=scheme>
(lib 'math)
(lib 'math)


Line 977: Line 977:
(agm 1 (/ 1 (sqrt 2)))
(agm 1 (/ 1 (sqrt 2)))
→ 0.8472130847939792
→ 0.8472130847939792
</syntaxhighlight>
</lang>


=={{header|Elixir}}==
=={{header|Elixir}}==


<lang Elixir>defmodule ArithhGeom do
<syntaxhighlight lang=Elixir>defmodule ArithhGeom do
def mean(a,g,tol) when abs(a-g) <= tol, do: a
def mean(a,g,tol) when abs(a-g) <= tol, do: a
def mean(a,g,tol) do
def mean(a,g,tol) do
Line 988: Line 988:
end
end


IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)</lang>
IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)</syntaxhighlight>


{{out}}
{{out}}
Line 996: Line 996:


=={{header|Erlang}}==
=={{header|Erlang}}==
<lang Erlang>%% Arithmetic Geometric Mean of 1 and 1 / sqrt(2)
<syntaxhighlight lang=Erlang>%% Arithmetic Geometric Mean of 1 and 1 / sqrt(2)
%% Author: Abhay Jain
%% Author: Abhay Jain


Line 1,014: Line 1,014:
A1 = (A+B) / 2,
A1 = (A+B) / 2,
B1 = math:pow(A*B, 0.5),
B1 = math:pow(A*B, 0.5),
agm(A1, B1).</lang>
agm(A1, B1).</syntaxhighlight>
Output:
Output:
<lang Erlang>AGM = 0.8472130848351929</lang>
<syntaxhighlight lang=Erlang>AGM = 0.8472130848351929</syntaxhighlight>


=={{header|ERRE}}==
=={{header|ERRE}}==
Line 1,041: Line 1,041:
PRINT(A)
PRINT(A)
END PROGRAM
END PROGRAM
</syntaxhighlight>
</lang>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
{{trans|OCaml}}
{{trans|OCaml}}
<lang fsharp>let rec agm a g precision =
<syntaxhighlight lang=fsharp>let rec agm a g precision =
if precision > abs(a - g) then a else
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
agm (0.5 * (a + g)) (sqrt (a * g)) precision


printfn "%g" (agm 1. (sqrt(0.5)) 1e-15)</lang>
printfn "%g" (agm 1. (sqrt(0.5)) 1e-15)</syntaxhighlight>
Output
Output
<pre>0.847213</pre>
<pre>0.847213</pre>


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: kernel math math.functions prettyprint ;
<syntaxhighlight lang=factor>USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
IN: rosetta-code.arithmetic-geometric-mean


: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;


1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .</lang>
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,066: Line 1,066:


=={{header|Forth}}==
=={{header|Forth}}==
<lang forth>: agm ( a g -- m )
<syntaxhighlight lang=forth>: agm ( a g -- m )
begin
begin
fover fover f+ 2e f/
fover fover f+ 2e f/
Line 1,074: Line 1,074:
fdrop ;
fdrop ;


1e 2e -0.5e f** agm f. \ 0.847213084793979</lang>
1e 2e -0.5e f** agm f. \ 0.847213084793979</syntaxhighlight>


=={{header|Fortran}}==
=={{header|Fortran}}==
A '''Fortran 77''' implementation
A '''Fortran 77''' implementation
<lang fortran> function agm(a,b)
<syntaxhighlight lang=fortran> function agm(a,b)
implicit none
implicit none
double precision agm,a,b,eps,c
double precision agm,a,b,eps,c
Line 1,092: Line 1,092:
double precision agm
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
end</lang>
end</syntaxhighlight>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' version 16-09-2015
<syntaxhighlight lang=freebasic>' version 16-09-2015
' compile with: fbc -s console
' compile with: fbc -s console


Line 1,120: Line 1,120:
Print : Print "hit any key to end program"
Print : Print "hit any key to end program"
Sleep
Sleep
End</lang>
End</syntaxhighlight>
{{out}}
{{out}}
<pre> 0.8472130847939792</pre>
<pre> 0.8472130847939792</pre>
Line 1,127: Line 1,127:
{{incorrect|Futhark|Futhark's syntax has changed, so this example will not compile}}
{{incorrect|Futhark|Futhark's syntax has changed, so this example will not compile}}


<lang Futhark>
<syntaxhighlight lang=Futhark>
import "futlib/math"
import "futlib/math"


Line 1,139: Line 1,139:
fun main(x: f64, y: f64): f64 =
fun main(x: f64, y: f64): f64 =
agm(x,y)
agm(x,y)
</syntaxhighlight>
</lang>


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


import (
import (
Line 1,160: Line 1,160:
func main() {
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
fmt.Println(agm(1, 1/math.Sqrt2))
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,169: Line 1,169:
{{trans|Java}}
{{trans|Java}}
Solution:
Solution:
<lang groovy>double agm (double a, double g) {
<syntaxhighlight lang=groovy>double agm (double a, double g) {
double an = a, gn = g
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
an
}</lang>
}</syntaxhighlight>


Test:
Test:
<lang groovy>println "agm(1, 0.5**0.5) = agm(1, ${0.5**0.5}) = ${agm(1, 0.5**0.5)}"
<syntaxhighlight 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</lang>
assert (0.8472130847939792 - agm(1, 0.5**0.5)).abs() <= 10.0**-14</syntaxhighlight>


Output:
Output:
Line 1,183: Line 1,183:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>-- Return an approximation to the arithmetic-geometric mean of two numbers.
<syntaxhighlight lang=haskell>-- Return an approximation to the arithmetic-geometric mean of two numbers.
-- The result is considered accurate when two successive approximations are
-- The result is considered accurate when two successive approximations are
-- sufficiently close, as determined by "eq".
-- sufficiently close, as determined by "eq".
Line 1,202: Line 1,202:
main = do
main = do
let equal = (< 0.000000001) . relDiff
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal</lang>
print $ agm 1 (1 / sqrt 2) equal</syntaxhighlight>
{{out}}
{{out}}
<pre>0.8472130847527654</pre>
<pre>0.8472130847527654</pre>
Line 1,223: Line 1,223:
}
}
return an
return an
end</lang>
end</syntaxhighlight>


Output:
Output:
Line 1,239: 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):
First, the basic approach (with display precision set to 16 digits, which slightly exceeds the accuracy of 64 bit IEEE floating point arithmetic):


<lang j>mean=: +/ % #
<syntaxhighlight lang=j>mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791</lang>
0.8472130847939792 0.8472130847939791</syntaxhighlight>


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):
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):


<lang j> ~.(mean , */ %:~ #)^:_] 1,%%:2
<syntaxhighlight lang=j> ~.(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792</lang>
0.8472130847939792</syntaxhighlight>


Another variation would be to show intermediate values, in the limit process:
Another variation would be to show intermediate values, in the limit process:


<lang j> (mean, */ %:~ #)^:a: 1,%%:2
<syntaxhighlight lang=j> (mean, */ %:~ #)^:a: 1,%%:2
1 0.7071067811865475
1 0.7071067811865475
0.8535533905932737 0.8408964152537145
0.8535533905932737 0.8408964152537145
0.8472249029234942 0.8472012667468915
0.8472249029234942 0.8472012667468915
0.8472130848351929 0.8472130847527654
0.8472130848351929 0.8472130847527654
0.8472130847939792 0.8472130847939791</lang>
0.8472130847939792 0.8472130847939791</syntaxhighlight>


=== Arbitrary Precision ===
=== Arbitrary Precision ===
Line 1,263: Line 1,263:
Borrowing routines from that page, but going with a default of approximately 100 digits of precision:
Borrowing routines from that page, but going with a default of approximately 100 digits of precision:


<lang J>DP=:101
<syntaxhighlight lang=J>DP=:101


round=: DP&$: : (4 : 0)
round=: DP&$: : (4 : 0)
Line 1,292: Line 1,292:
n=. e (>i.1:) a (^%!@]) i.>.a^.e [ a=. |y-m*^.2
n=. e (>i.1:) a (^%!@]) i.>.a^.e [ a=. |y-m*^.2
(2x^m) * 1++/*/\d%1+i.n
(2x^m) * 1++/*/\d%1+i.n
)</lang>
)</syntaxhighlight>


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:
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:


<lang J>fmt=:[: ;:inv DP&$: : (4 :0)&.>
<syntaxhighlight lang=J>fmt=:[: ;:inv DP&$: : (4 :0)&.>
x{.deb (x*2j1)":y
x{.deb (x*2j1)":y
)
)
Line 1,302: Line 1,302:
root=: ln@] exp@% [
root=: ln@] exp@% [


epsilon=: 1r9^DP</lang>
epsilon=: 1r9^DP</syntaxhighlight>


Some example uses:
Some example uses:


<lang J> fmt sqrt 2
<syntaxhighlight lang=J> fmt sqrt 2
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572
fmt *~sqrt 2
fmt *~sqrt 2
Line 1,313: Line 1,313:
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000418
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000418
fmt 2 root 2
fmt 2 root 2
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572</lang>
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572</syntaxhighlight>


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:
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:


<lang J>geomean=: */ root~ #
<syntaxhighlight lang=J>geomean=: */ root~ #
geomean2=: [: sqrt */</lang>
geomean2=: [: sqrt */</syntaxhighlight>


A quick test to make sure these can be equivalent:
A quick test to make sure these can be equivalent:


<lang J> fmt geomean 3 5
<syntaxhighlight lang=J> fmt geomean 3 5
3.872983346207416885179265399782399610832921705291590826587573766113483091936979033519287376858673517
3.872983346207416885179265399782399610832921705291590826587573766113483091936979033519287376858673517
fmt geomean2 3 5
fmt geomean2 3 5
3.872983346207416885179265399782399610832921705291590826587573766113483091936979033519287376858673517</lang>
3.872983346207416885179265399782399610832921705291590826587573766113483091936979033519287376858673517</syntaxhighlight>


Now for our task example:
Now for our task example:


<lang J> fmt (mean, geomean2)^:(epsilon <&| -/)^:a: 1,%sqrt 2
<syntaxhighlight lang=J> fmt (mean, geomean2)^:(epsilon <&| -/)^:a: 1,%sqrt 2
1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0.707106781186547524400844362104849039284835937688474036588339868995366239231053519425193767163820786
1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0.707106781186547524400844362104849039284835937688474036588339868995366239231053519425193767163820786
0.853553390593273762200422181052424519642417968844237018294169934497683119615526759712596883581910393 0.840896415253714543031125476233214895040034262356784510813226085974924754953902239814324004199292536
0.853553390593273762200422181052424519642417968844237018294169934497683119615526759712596883581910393 0.840896415253714543031125476233214895040034262356784510813226085974924754953902239814324004199292536
Line 1,337: Line 1,337:
0.847213084793979086606499123482191636481445984459557704232275241670533381126169243513557113565344075 0.847213084793979086606499123482191636481445836194326665888883503648934628542100275932846717790147361
0.847213084793979086606499123482191636481445984459557704232275241670533381126169243513557113565344075 0.847213084793979086606499123482191636481445836194326665888883503648934628542100275932846717790147361
0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723201915677745718 0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723198672311476741
0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723201915677745718 0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723198672311476741
0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723200293994611229 0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723200293994611229</lang>
0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723200293994611229 0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723200293994611229</syntaxhighlight>


We could of course extract out only a representative final value, but it's obvious enough, and showing how rapidly this converges is fun.
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: Line 1,343:
=={{header|Java}}==
=={{header|Java}}==


<lang Java>/*
<syntaxhighlight lang=Java>/*
* Arithmetic-Geometric Mean of 1 & 1/sqrt(2)
* Arithmetic-Geometric Mean of 1 & 1/sqrt(2)
* Brendan Shaklovitz
* Brendan Shaklovitz
Line 1,365: Line 1,365:
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>0.8472130847939792</pre>
<pre>0.8472130847939792</pre>
Line 1,372: Line 1,372:


===ES5===
===ES5===
<lang JavaScript>function agm(a0, g0) {
<syntaxhighlight lang=JavaScript>function agm(a0, g0) {
var an = (a0 + g0) / 2,
var an = (a0 + g0) / 2,
gn = Math.sqrt(a0 * g0);
gn = Math.sqrt(a0 * g0);
Line 1,381: Line 1,381:
}
}


agm(1, 1 / Math.sqrt(2));</lang>
agm(1, 1 / Math.sqrt(2));</syntaxhighlight>


===ES6===
===ES6===
<lang JavaScript>(() => {
<syntaxhighlight lang=JavaScript>(() => {
'use strict';
'use strict';


Line 1,426: Line 1,426:
return agm(1, 1 / Math.sqrt(2));
return agm(1, 1 / Math.sqrt(2));


})();</lang>
})();</syntaxhighlight>


{{Out}}
{{Out}}
<lang JavaScript>0.8472130848351929</lang>
<syntaxhighlight lang=JavaScript>0.8472130848351929</syntaxhighlight>


=={{header|jq}}==
=={{header|jq}}==
{{works with|jq|1.4}}
{{works with|jq|1.4}}
Naive version that assumes tolerance is appropriately specified:
Naive version that assumes tolerance is appropriately specified:
<lang jq>def naive_agm(a; g; tolerance):
<syntaxhighlight lang=jq>def naive_agm(a; g; tolerance):
def abs: if . < 0 then -. else . end;
def abs: if . < 0 then -. else . end;
def _agm:
def _agm:
Line 1,442: Line 1,442:
else .
else .
end;
end;
[a, g] | _agm | .[0] ;</lang>
[a, g] | _agm | .[0] ;</syntaxhighlight>
This version avoids an infinite loop if the requested tolerance is too small:
This version avoids an infinite loop if the requested tolerance is too small:
<lang jq>def agm(a; g; tolerance):
<syntaxhighlight lang=jq>def agm(a; g; tolerance):
def abs: if . < 0 then -. else . end;
def abs: if . < 0 then -. else . end;
def _agm:
def _agm:
Line 1,459: Line 1,459:
# Example:
# Example:
agm(1; 1/(2|sqrt); 1e-100)</lang>
agm(1; 1/(2|sqrt); 1e-100)</syntaxhighlight>
{{Out}}
{{Out}}
$ jq -n -f Arithmetic-geometric_mean.jq
$ jq -n -f Arithmetic-geometric_mean.jq
Line 1,466: Line 1,466:
=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|1.2}}
{{works with|Julia|1.2}}
<lang Julia>function agm(x, y, e::Real = 5)
<syntaxhighlight lang=Julia>function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
g, a = minmax(x, y)
Line 1,485: Line 1,485:
println("# Using ", precision(BigFloat), "-bit float numbers:")
println("# Using ", precision(BigFloat), "-bit float numbers:")
x, y = big(1.0), 1 / √big(2.0)
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)</lang>
@show agm(x, y)</syntaxhighlight>
The &epsilon; for this calculation is given as a positive integer multiple of the machine &epsilon; for <tt>x</tt>.
The &epsilon; for this calculation is given as a positive integer multiple of the machine &epsilon; for <tt>x</tt>.


Line 1,498: Line 1,498:
=={{header|Klingphix}}==
=={{header|Klingphix}}==
{{trans|Oforth}}
{{trans|Oforth}}
<lang Klingphix>include ..\Utilitys.tlhy
<syntaxhighlight lang=Klingphix>include ..\Utilitys.tlhy


:agm [ over over + 2 / rot rot * sqrt ] [ over over tostr swap tostr # ] while drop ;
:agm [ over over + 2 / rot rot * sqrt ] [ over over tostr swap tostr # ] while drop ;
Line 1,506: Line 1,506:
pstack
pstack


" " input</lang>
" " input</syntaxhighlight>
{{trans|F#}}
{{trans|F#}}
<lang Klingphix>include ..\Utilitys.tlhy
<syntaxhighlight lang=Klingphix>include ..\Utilitys.tlhy


:agm %a %g %p !p !g !a
:agm %a %g %p !p !g !a
Line 1,517: Line 1,517:
pstack
pstack


" " input</lang>
" " input</syntaxhighlight>
{{out}}
{{out}}
<pre>(0.847213)</pre>
<pre>(0.847213)</pre>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.0.5-2
<syntaxhighlight lang=scala>// version 1.0.5-2


fun agm(a: Double, g: Double): Double {
fun agm(a: Double, g: Double): Double {
Line 1,540: Line 1,540:
fun main(args: Array<String>) {
fun main(args: Array<String>) {
println(agm(1.0, 1.0 / Math.sqrt(2.0)))
println(agm(1.0, 1.0 / Math.sqrt(2.0)))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,549: Line 1,549:
=={{header|LFE}}==
=={{header|LFE}}==


<lang lisp>
<syntaxhighlight lang=lisp>
(defun agm (a g)
(defun agm (a g)
(agm a g 1.0e-15))
(agm a g 1.0e-15))
Line 1,565: Line 1,565:
(defun next-g (a g)
(defun next-g (a g)
(math:sqrt (* a g)))
(math:sqrt (* a g)))
</syntaxhighlight>
</lang>


Usage:
Usage:
Line 1,575: Line 1,575:


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
<syntaxhighlight lang=lb>
<lang lb>
print agm(1, 1/sqr(2))
print agm(1, 1/sqr(2))
print using("#.#################",agm(1, 1/sqr(2)))
print using("#.#################",agm(1, 1/sqr(2)))
Line 1,590: Line 1,590:
end function
end function
</syntaxhighlight>
</lang>


=={{header|LiveCode}}==
=={{header|LiveCode}}==
<lang LiveCode>function agm aa,g
<syntaxhighlight lang=LiveCode>function agm aa,g
put abs(aa-g) into absdiff
put abs(aa-g) into absdiff
put (aa+g)/2 into aan
put (aa+g)/2 into aan
Line 1,605: Line 1,605:
end repeat
end repeat
return aa
return aa
end agm</lang>
end agm</syntaxhighlight>
Example
Example
<lang LiveCode>put agm(1, 1/sqrt(2))
<syntaxhighlight lang=LiveCode>put agm(1, 1/sqrt(2))
-- ouput
-- ouput
-- 0.847213</lang>
-- 0.847213</syntaxhighlight>


=={{header|LLVM}}==
=={{header|LLVM}}==
<lang llvm>; This is not strictly LLVM, as it uses the C library function "printf".
<syntaxhighlight 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
; 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.
; to just load the string into memory, and that would be boring.
Line 1,714: Line 1,714:
attributes #2 = { nounwind readnone speculatable }
attributes #2 = { nounwind readnone speculatable }
attributes #4 = { nounwind }
attributes #4 = { nounwind }
attributes #6 = { noreturn }</lang>
attributes #6 = { noreturn }</syntaxhighlight>
{{out}}
{{out}}
<pre>The arithmetic-geometric mean is 0.8472130847939791654</pre>
<pre>The arithmetic-geometric mean is 0.8472130847939791654</pre>


=={{header|Logo}}==
=={{header|Logo}}==
<lang logo>to about :a :b
<syntaxhighlight lang=logo>to about :a :b
output and [:a - :b < 1e-15] [:a - :b > -1e-15]
output and [:a - :b < 1e-15] [:a - :b > -1e-15]
end
end
Line 1,728: Line 1,728:


show agm 1 1/sqrt 2
show agm 1 1/sqrt 2
</syntaxhighlight>
</lang>


=={{header|Lua}}==
=={{header|Lua}}==


<lang lua>function agm(a, b, tolerance)
<syntaxhighlight lang=lua>function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
tolerance = 1e-15
Line 1,742: Line 1,742:
end
end


print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))</lang>
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))</syntaxhighlight>


'''Output:'''
'''Output:'''
Line 1,749: Line 1,749:


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<lang M2000 Interpreter>
<syntaxhighlight lang=M2000 Interpreter>
Module Checkit {
Module Checkit {
Function Agm {
Function Agm {
Line 1,764: Line 1,764:
}
}
Checkit
Checkit
</syntaxhighlight>
</lang>


=={{header|Maple}}==
=={{header|Maple}}==
Maple provides this function under the name GaussAGM. To compute a floating point approximation, use evalf.
Maple provides this function under the name GaussAGM. To compute a floating point approximation, use evalf.
<lang Maple>
<syntaxhighlight lang=Maple>
> evalf( GaussAGM( 1, 1 / sqrt( 2 ) ) ); # default precision is 10 digits
> evalf( GaussAGM( 1, 1 / sqrt( 2 ) ) ); # default precision is 10 digits
0.8472130847
0.8472130847
Line 1,775: Line 1,775:
0.847213084793979086606499123482191636481445910326942185060579372659\
0.847213084793979086606499123482191636481445910326942185060579372659\
7340048341347597232002939946112300
7340048341347597232002939946112300
</syntaxhighlight>
</lang>
Alternatively, if one or both arguments is already a float, Maple will compute a floating point approximation automatically.
Alternatively, if one or both arguments is already a float, Maple will compute a floating point approximation automatically.
<lang Maple>
<syntaxhighlight lang=Maple>
> GaussAGM( 1.0, 1 / sqrt( 2 ) );
> GaussAGM( 1.0, 1 / sqrt( 2 ) );
0.8472130847
0.8472130847
</syntaxhighlight>
</lang>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
To any arbitrary precision, just increase PrecisionDigits
To any arbitrary precision, just increase PrecisionDigits
<lang Mathematica>PrecisionDigits = 85;
<syntaxhighlight lang=Mathematica>PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛</lang>
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛</syntaxhighlight>


<pre>AGMean[1, 1/Sqrt[2]]
<pre>AGMean[1, 1/Sqrt[2]]
Line 1,791: Line 1,791:


=={{header|MATLAB}} / {{header|Octave}}==
=={{header|MATLAB}} / {{header|Octave}}==
<lang MATLAB>function [a,g]=agm(a,g)
<syntaxhighlight lang=MATLAB>function [a,g]=agm(a,g)
%%arithmetic_geometric_mean(a,g)
%%arithmetic_geometric_mean(a,g)
while (1)
while (1)
Line 1,799: Line 1,799:
if (abs(a0-a) < a*eps) break; end;
if (abs(a0-a) < a*eps) break; end;
end;
end;
end</lang>
end</syntaxhighlight>
<pre>octave:26> agm(1,1/sqrt(2))
<pre>octave:26> agm(1,1/sqrt(2))
ans = 0.84721
ans = 0.84721
Line 1,805: Line 1,805:


=={{header|Maxima}}==
=={{header|Maxima}}==
<lang maxima>agm(a, b) := %pi/4*(a + b)/elliptic_kc(((a - b)/(a + b))^2)$
<syntaxhighlight lang=maxima>agm(a, b) := %pi/4*(a + b)/elliptic_kc(((a - b)/(a + b))^2)$


agm(1, 1/sqrt(2)), bfloat, fpprec: 85;
agm(1, 1/sqrt(2)), bfloat, fpprec: 85;
/* 8.472130847939790866064991234821916364814459103269421850605793726597340048341347597232b-1 */</lang>
/* 8.472130847939790866064991234821916364814459103269421850605793726597340048341347597232b-1 */</syntaxhighlight>


=={{header|МК-61/52}}==
=={{header|МК-61/52}}==
Line 1,814: Line 1,814:
- ИП2 - /-/ x<0 31 ИП1 П3 ИП0 ИП1
- ИП2 - /-/ x<0 31 ИП1 П3 ИП0 ИП1
* КвКор П1 ИП0 ИП3 + 2 / П0 БП
* КвКор П1 ИП0 ИП3 + 2 / П0 БП
08 ИП0 С/П</lang>
08 ИП0 С/П</syntaxhighlight>


=={{header|Modula-2}}==
=={{header|Modula-2}}==
{{trans|C}}
{{trans|C}}
<lang modula2>MODULE AGM;
<syntaxhighlight lang=modula2>MODULE AGM;
FROM EXCEPTIONS IMPORT AllocateSource,ExceptionSource,GetMessage,RAISE;
FROM EXCEPTIONS IMPORT AllocateSource,ExceptionSource,GetMessage,RAISE;
FROM LongConv IMPORT ValueReal;
FROM LongConv IMPORT ValueReal;
Line 1,887: Line 1,887:
WriteReal(AGM(x, y));
WriteReal(AGM(x, y));
WriteLn
WriteLn
END AGM.</lang>
END AGM.</syntaxhighlight>
{{out}}
{{out}}
<pre>Enter two numbers: 1.0
<pre>Enter two numbers: 1.0
Line 1,898: Line 1,898:
=={{header|NetRexx}}==
=={{header|NetRexx}}==
{{trans|Java}}
{{trans|Java}}
<lang NetRexx>/* NetRexx */
<syntaxhighlight lang=NetRexx>/* NetRexx */
options replace format comments java crossref symbols nobinary
options replace format comments java crossref symbols nobinary


Line 1,922: Line 1,922:
end
end
return a1 + 0
return a1 + 0
</syntaxhighlight>
</lang>
'''Output:'''
'''Output:'''
<pre>
<pre>
Line 1,929: Line 1,929:


=={{header|NewLISP}}==
=={{header|NewLISP}}==
<lang NewLISP>
<syntaxhighlight lang=NewLISP>
(define (a-next a g) (mul 0.5 (add a g)))
(define (a-next a g) (mul 0.5 (add a g)))


Line 1,951: Line 1,951:
(amg 1.0 root-reciprocal-2 quadrillionth)
(amg 1.0 root-reciprocal-2 quadrillionth)
)
)
</syntaxhighlight>
</lang>


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>import math
<syntaxhighlight lang=nim>import math


proc agm(a, g: float,delta: float = 1.0e-15): float =
proc agm(a, g: float,delta: float = 1.0e-15): float =
Line 1,967: Line 1,967:
result = aOld
result = aOld


echo agm(1.0,1.0/sqrt(2.0))</lang>
echo agm(1.0,1.0/sqrt(2.0))</syntaxhighlight>


Output:<br/>
Output:<br/>
Line 1,976: Line 1,976:
See first 24 iterations:
See first 24 iterations:


<lang nim>from math import sqrt
<syntaxhighlight lang=nim>from math import sqrt
from strutils import parseFloat, formatFloat, ffDecimal
from strutils import parseFloat, formatFloat, ffDecimal


Line 1,995: Line 1,995:


echo("Result A: " & formatFloat(t.resA, ffDecimal, 24))
echo("Result A: " & formatFloat(t.resA, ffDecimal, 24))
echo("Result G: " & formatFloat(t.resG, ffDecimal, 24))</lang>
echo("Result G: " & formatFloat(t.resG, ffDecimal, 24))</syntaxhighlight>


=={{header|Oberon-2}}==
=={{header|Oberon-2}}==
{{works with|oo2c}}
{{works with|oo2c}}
<lang oberon2>
<syntaxhighlight lang=oberon2>
MODULE Agm;
MODULE Agm;
IMPORT
IMPORT
Line 2,025: Line 2,025:
Out.LongReal(Of(1,1 / Math.sqrt(2)),0,0);Out.Ln
Out.LongReal(Of(1,1 / Math.sqrt(2)),0,0);Out.Ln
END Agm.
END Agm.
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 2,033: Line 2,033:
=={{header|Objeck}}==
=={{header|Objeck}}==
{{trans|Java}}
{{trans|Java}}
<lang objeck>
<syntaxhighlight lang=objeck>
class ArithmeticMean {
class ArithmeticMean {
function : Amg(a : Float, g : Float) ~ Nil {
function : Amg(a : Float, g : Float) ~ Nil {
Line 2,050: Line 2,050:
}
}
}
}
</syntaxhighlight>
</lang>


Output:
Output:
Line 2,056: Line 2,056:


=={{header|OCaml}}==
=={{header|OCaml}}==
<lang ocaml>let rec agm a g tol =
<syntaxhighlight lang=ocaml>let rec agm a g tol =
if tol > abs_float (a -. g) then a else
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol


let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)</lang>
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)</syntaxhighlight>
Output
Output
<pre>0.8472130847939792</pre>
<pre>0.8472130847939792</pre>
Line 2,066: Line 2,066:
=={{header|Oforth}}==
=={{header|Oforth}}==


<lang Oforth>: agm \ a b -- m
<syntaxhighlight lang=Oforth>: agm \ a b -- m
while( 2dup <> ) [ 2dup + 2 / -rot * sqrt ] drop ;</lang>
while( 2dup <> ) [ 2dup + 2 / -rot * sqrt ] drop ;</syntaxhighlight>


Usage :
Usage :
<lang Oforth>1 2 sqrt inv agm</lang>
<syntaxhighlight lang=Oforth>1 2 sqrt inv agm</syntaxhighlight>


{{out}}
{{out}}
Line 2,078: Line 2,078:


=={{header|OOC}}==
=={{header|OOC}}==
<lang ooc>
<syntaxhighlight lang=ooc>
import math // import for sqrt() function
import math // import for sqrt() function


Line 2,098: Line 2,098:
"%.16f" printfln(agm(1., sqrt(0.5)))
"%.16f" printfln(agm(1., sqrt(0.5)))
}
}
</syntaxhighlight>
</lang>
Output
Output
<pre>0.8472130847939792</pre>
<pre>0.8472130847939792</pre>


=={{header|ooRexx}}==
=={{header|ooRexx}}==
<lang ooRexx>numeric digits 20
<syntaxhighlight lang=ooRexx>numeric digits 20
say agm(1, 1/rxcalcsqrt(2,16))
say agm(1, 1/rxcalcsqrt(2,16))


Line 2,120: Line 2,120:
return a1+0
return a1+0


::requires rxmath LIBRARY</lang>
::requires rxmath LIBRARY</syntaxhighlight>
{{out}}
{{out}}
<pre>0.8472130847939791968</pre>
<pre>0.8472130847939791968</pre>
Line 2,126: Line 2,126:
=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
Built-in:
Built-in:
<lang parigp>agm(1,1/sqrt(2))</lang>
<syntaxhighlight lang=parigp>agm(1,1/sqrt(2))</syntaxhighlight>


Iteration:
Iteration:
<lang parigp>agm2(x,y)=if(x==y,x,agm2((x+y)/2,sqrt(x*y))</lang>
<syntaxhighlight lang=parigp>agm2(x,y)=if(x==y,x,agm2((x+y)/2,sqrt(x*y))</syntaxhighlight>


=={{header|Pascal}}==
=={{header|Pascal}}==
Line 2,135: Line 2,135:
{{libheader|GMP}}
{{libheader|GMP}}
Port of the C example:
Port of the C example:
<lang pascal>Program ArithmeticGeometricMean;
<syntaxhighlight lang=pascal>Program ArithmeticGeometricMean;


uses
uses
Line 2,169: Line 2,169:
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.</lang>
end.</syntaxhighlight>
Output is as long as the C example.
Output is as long as the C example.


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl -w
<syntaxhighlight lang=perl>#!/usr/bin/perl -w


my ($a0, $g0, $a1, $g1);
my ($a0, $g0, $a1, $g1);
Line 2,189: Line 2,189:
}
}


print agm(1, 1/sqrt(2))."\n";</lang>
print agm(1, 1/sqrt(2))."\n";</syntaxhighlight>
Output:
Output:
<pre>0.847213084793979</pre>
<pre>0.847213084793979</pre>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight 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;">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>
<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: Line 2,203:
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,214: Line 2,214:


=={{header|Phixmonti}}==
=={{header|Phixmonti}}==
<lang Phixmonti>include ..\Utilitys.pmt
<syntaxhighlight lang=Phixmonti>include ..\Utilitys.pmt


1.0e-15 var tolerance
1.0e-15 var tolerance
Line 2,228: Line 2,228:
enddef
enddef


1 1 2 sqrt / agm tostr ?</lang>
1 1 2 sqrt / agm tostr ?</syntaxhighlight>


=={{header|PHP}}==
=={{header|PHP}}==
<lang php>
<syntaxhighlight lang=php>
define('PRECISION', 13);
define('PRECISION', 13);


Line 2,254: Line 2,254:
bcscale(PRECISION);
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
echo agm(1, 1 / bcsqrt(2));
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,261: Line 2,261:


=={{header|Picat}}==
=={{header|Picat}}==
<lang Picat>main =>
<syntaxhighlight lang=Picat>main =>
println(agm(1.0, 1/sqrt(2))).
println(agm(1.0, 1/sqrt(2))).


agm(A,G) = A, A-G < 1.0e-10 => true.
agm(A,G) = A, A-G < 1.0e-10 => true.
agm(A,G) = agm((A+G)/2, sqrt(A*G)).
agm(A,G) = agm((A+G)/2, sqrt(A*G)).
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,274: Line 2,274:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(scl 80)
<syntaxhighlight lang=PicoLisp>(scl 80)


(de agm (A G)
(de agm (A G)
Line 2,283: Line 2,283:
(round
(round
(agm 1.0 (*/ 1.0 1.0 (sqrt 2.0 1.0)))
(agm 1.0 (*/ 1.0 1.0 (sqrt 2.0 1.0)))
70 )</lang>
70 )</syntaxhighlight>
Output:
Output:
<pre>-> "0.8472130847939790866064991234821916364814459103269421850605793726597340"</pre>
<pre>-> "0.8472130847939790866064991234821916364814459103269421850605793726597340"</pre>


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang PL/I>
<syntaxhighlight lang=PL/I>
arithmetic_geometric_mean: /* 31 August 2012 */
arithmetic_geometric_mean: /* 31 August 2012 */
procedure options (main);
procedure options (main);
Line 2,302: Line 2,302:
put skip list ('The result is:', a);
put skip list ('The result is:', a);
end arithmetic_geometric_mean;
end arithmetic_geometric_mean;
</syntaxhighlight>
</lang>
Results:
Results:
<pre>
<pre>
Line 2,315: Line 2,315:
=={{header|Potion}}==
=={{header|Potion}}==
Input values should be floating point
Input values should be floating point
<lang potion>sqrt = (x) :
<syntaxhighlight lang=potion>sqrt = (x) :
xi = 1
xi = 1
7 times :
7 times :
Line 2,331: Line 2,331:
.
.
x
x
.</lang>
.</syntaxhighlight>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<lang PowerShell>
<syntaxhighlight lang=PowerShell>
function agm ([Double]$a, [Double]$g) {
function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
[Double]$eps = 1E-15
Line 2,349: Line 2,349:
}
}
agm 1 (1/[Math]::Sqrt(2))
agm 1 (1/[Math]::Sqrt(2))
</syntaxhighlight>
</lang>
<b>Output:</b>
<b>Output:</b>
<pre>
<pre>
Line 2,358: Line 2,358:


=={{header|Prolog}}==
=={{header|Prolog}}==
<lang Prolog>
<syntaxhighlight lang=Prolog>
agm(A,G,A) :- abs(A-G) < 1.0e-15, !.
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).
agm(A,G,Res) :- A1 is (A+G)/2.0, G1 is sqrt(A*G),!, agm(A1,G1,Res).
Line 2,364: Line 2,364:
?- agm(1,1/sqrt(2),Res).
?- agm(1,1/sqrt(2),Res).
Res = 0.8472130847939792.
Res = 0.8472130847939792.
</syntaxhighlight>
</lang>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang purebasic>Procedure.d AGM(a.d, g.d, ErrLim.d=1e-15)
<syntaxhighlight lang=purebasic>Procedure.d AGM(a.d, g.d, ErrLim.d=1e-15)
Protected.d ta=a+1, tg
Protected.d ta=a+1, tg
While ta <> a
While ta <> a
Line 2,381: Line 2,381:
Input()
Input()
CloseConsole()
CloseConsole()
EndIf</lang>
EndIf</syntaxhighlight>


0.8472130847939792
0.8472130847939792
Line 2,389: Line 2,389:


===Basic Version===
===Basic Version===
<lang python>from math import sqrt
<syntaxhighlight lang=python>from math import sqrt


def agm(a0, g0, tolerance=1e-10):
def agm(a0, g0, tolerance=1e-10):
Line 2,404: Line 2,404:
return an
return an


print agm(1, 1 / sqrt(2))</lang>
print agm(1, 1 / sqrt(2))</syntaxhighlight>
{{out}}
{{out}}
<pre> 0.847213084835</pre>
<pre> 0.847213084835</pre>
===Multi-Precision Version===
===Multi-Precision Version===
<lang python>from decimal import Decimal, getcontext
<syntaxhighlight lang=python>from decimal import Decimal, getcontext


def agm(a, g, tolerance=Decimal("1e-65")):
def agm(a, g, tolerance=Decimal("1e-65")):
Line 2,417: Line 2,417:


getcontext().prec = 70
getcontext().prec = 70
print agm(Decimal(1), 1 / Decimal(2).sqrt())</lang>
print agm(Decimal(1), 1 / Decimal(2).sqrt())</syntaxhighlight>
{{out}}
{{out}}
<pre>0.847213084793979086606499123482191636481445910326942185060579372659734</pre>
<pre>0.847213084793979086606499123482191636481445910326942185060579372659734</pre>
Line 2,424: Line 2,424:
=={{header|Quackery}}==
=={{header|Quackery}}==


<lang Quackery> [ $ "bigrat.qky" loadfile ] now!
<syntaxhighlight lang=Quackery> [ $ "bigrat.qky" loadfile ] now!


[ temp put
[ temp put
Line 2,441: Line 2,441:
125 point$ echo$ cr cr
125 point$ echo$ cr cr
swap say "Num: " echo cr
swap say "Num: " echo cr
say "Den: " echo</lang>
say "Den: " echo</syntaxhighlight>


{{out}}
{{out}}
Line 2,454: Line 2,454:


=={{header|R}}==
=={{header|R}}==
<lang r>arithmeticMean <- function(a, b) { (a + b)/2 }
<syntaxhighlight lang=r>arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
geometricMean <- function(a, b) { sqrt(a * b) }


Line 2,467: Line 2,467:


agm <- arithmeticGeometricMean(1, 1/sqrt(2))
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))</lang>
print(format(agm, digits=16))</syntaxhighlight>
{{out}}
{{out}}
<pre> agm rel_error
<pre> agm rel_error
1 0.8472130847939792 1.310441309927519e-16</pre>
1 0.8472130847939792 1.310441309927519e-16</pre>
This function also works on vectors a and b (following the spirit of R):
This function also works on vectors a and b (following the spirit of R):
<lang r>a <- c(1, 1, 1)
<syntaxhighlight lang=r>a <- c(1, 1, 1)
b <- c(1/sqrt(2), 1/sqrt(3), 1/2)
b <- c(1/sqrt(2), 1/sqrt(3), 1/2)
agm <- arithmeticGeometricMean(a, b)
agm <- arithmeticGeometricMean(a, b)
print(format(agm, digits=16))</lang>
print(format(agm, digits=16))</syntaxhighlight>
{{out}}
{{out}}
<pre> agm rel_error
<pre> agm rel_error
Line 2,484: Line 2,484:
=={{header|Racket}}==
=={{header|Racket}}==
This version uses Racket's normal numbers:
This version uses Racket's normal numbers:
<lang racket>
<syntaxhighlight lang=racket>
#lang racket
#lang racket
(define (agm a g [ε 1e-15])
(define (agm a g [ε 1e-15])
Line 2,492: Line 2,492:


(agm 1 (/ 1 (sqrt 2)))
(agm 1 (/ 1 (sqrt 2)))
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 2,499: Line 2,499:


This alternative version uses arbitrary precision floats:
This alternative version uses arbitrary precision floats:
<lang racket>
<syntaxhighlight lang=racket>
#lang racket
#lang racket
(require math/bigfloat)
(require math/bigfloat)
(bf-precision 200)
(bf-precision 200)
(bfagm 1.bf (bf/ (bfsqrt 2.bf)))
(bfagm 1.bf (bf/ (bfsqrt 2.bf)))
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 2,512: Line 2,512:
=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>sub agm( $a is copy, $g is copy ) {
<syntaxhighlight lang=perl6>sub agm( $a is copy, $g is copy ) {
($a, $g) = ($a + $g)/2, sqrt $a * $g until $a ≅ $g;
($a, $g) = ($a + $g)/2, sqrt $a * $g until $a ≅ $g;
return $a;
return $a;
}
}
say agm 1, 1/sqrt 2;</lang>
say agm 1, 1/sqrt 2;</syntaxhighlight>
{{out}}
{{out}}
<pre>0.84721308479397917</pre>
<pre>0.84721308479397917</pre>


It's also possible to write it recursively:
It's also possible to write it recursively:
<lang perl6>sub agm( $a, $g ) {
<syntaxhighlight lang=perl6>sub agm( $a, $g ) {
$a ≅ $g ?? $a !! agm(|@$_)
$a ≅ $g ?? $a !! agm(|@$_)
given ($a + $g)/2, sqrt $a * $g;
given ($a + $g)/2, sqrt $a * $g;
}
}


say agm 1, 1/sqrt 2;</lang>
say agm 1, 1/sqrt 2;</syntaxhighlight>


=={{header|Raven}}==
=={{header|Raven}}==
<lang Raven>define agm use $a, $g, $errlim
<syntaxhighlight lang=Raven>define agm use $a, $g, $errlim
# $errlim $g $a "%d %g %d\n" print
# $errlim $g $a "%d %g %d\n" print
$a 1.0 + as $t
$a 1.0 + as $t
Line 2,541: Line 2,541:




16 1 2 sqrt / 1 agm "agm: %.15g\n" print</lang>
16 1 2 sqrt / 1 agm "agm: %.15g\n" print</syntaxhighlight>
{{out}}
{{out}}
<pre>t: 0.853553 a: 0.853553 g: 0.840896
<pre>t: 0.853553 a: 0.853553 g: 0.840896
Line 2,550: Line 2,550:


=={{header|Relation}}==
=={{header|Relation}}==
<lang Relation>
<syntaxhighlight lang=Relation>
function agm(x,y)
function agm(x,y)
set a = x
set a = x
Line 2,569: Line 2,569:
echo sqrt(x+y)
echo sqrt(x+y)
echo agm(x,y)
echo agm(x,y)
</syntaxhighlight>
</lang>


<pre>
<pre>
Line 2,581: Line 2,581:


REXX supports arbitrary precision, so the default digits can be changed if desired.
REXX supports arbitrary precision, so the default digits can be changed if desired.
<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= 120 /*No DIGS specified? Then use default.*/
if digs=='' | digs=="," then digs= 120 /*No DIGS specified? Then use default.*/
Line 2,612: Line 2,612:
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 input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 2,621: Line 2,621:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang=ring>
decimals(9)
decimals(9)
see agm(1, 1/sqrt(2)) + nl
see agm(1, 1/sqrt(2)) + nl
Line 2,635: Line 2,635:
end
end
return gn
return gn
</syntaxhighlight>
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
===Flt Version===
===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.
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.
<lang ruby># The flt package (http://flt.rubyforge.org/) is useful for high-precision floating-point math.
<syntaxhighlight 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
# It lets us control 'context' of numbers, individually or collectively -- including precision
# (which adjusts the context's value of epsilon accordingly).
# (which adjusts the context's value of epsilon accordingly).
Line 2,660: Line 2,660:
end
end


puts agm(1, 1 / BinNum(2).sqrt)</lang>
puts agm(1, 1 / BinNum(2).sqrt)</syntaxhighlight>
{{out}}
{{out}}
<pre>0.84721308479397908660649912348219163648144591032694218506057937265973400483413475972320029399461122994212228562523341096309796266583087105969971363598338426</pre>
<pre>0.84721308479397908660649912348219163648144591032694218506057937265973400483413475972320029399461122994212228562523341096309796266583087105969971363598338426</pre>
Line 2,667: Line 2,667:
===BigDecimal Version===
===BigDecimal Version===
Ruby has a BigDecimal class in standard library
Ruby has a BigDecimal class in standard library
<lang ruby>require 'bigdecimal'
<syntaxhighlight lang=ruby>require 'bigdecimal'


PRECISION = 100
PRECISION = 100
Line 2,682: Line 2,682:
a = BigDecimal(1)
a = BigDecimal(1)
g = 1 / BigDecimal(2).sqrt(PRECISION)
g = 1 / BigDecimal(2).sqrt(PRECISION)
puts agm(a, g)</lang>
puts agm(a, g)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,690: Line 2,690:


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<lang runbasic>print agm(1, 1/sqr(2))
<syntaxhighlight lang=runbasic>print agm(1, 1/sqr(2))
print agm(1,1/2^.5)
print agm(1,1/2^.5)
print using("#.############################",agm(1, 1/sqr(2)))
print using("#.############################",agm(1, 1/sqr(2)))
Line 2,702: Line 2,702:
g = gn
g = gn
wend
wend
end function</lang>Output:
end function</syntaxhighlight>Output:
<pre>0.847213085
<pre>0.847213085
0.847213085
0.847213085
Line 2,709: Line 2,709:
=={{header|Rust}}==
=={{header|Rust}}==


<lang rust>// Accepts two command line arguments
<syntaxhighlight lang=rust>// Accepts two command line arguments
// cargo run --name agm arg1 arg2
// cargo run --name agm arg1 arg2


Line 2,739: Line 2,739:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,748: Line 2,748:


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>
<syntaxhighlight lang=scala>
def agm(a: Double, g: Double, eps: Double): Double = {
def agm(a: Double, g: Double, eps: Double): Double = {
if (math.abs(a - g) < eps) (a + g) / 2
if (math.abs(a - g) < eps) (a + g) / 2
Line 2,755: Line 2,755:


agm(1, math.sqrt(2)/2, 1e-15)
agm(1, math.sqrt(2)/2, 1e-15)
</syntaxhighlight>
</lang>


=={{header|Scheme}}==
=={{header|Scheme}}==


<lang scheme>
<syntaxhighlight lang=scheme>
(define agm
(define agm
(case-lambda
(case-lambda
Line 2,770: Line 2,770:


(display (agm 1 (/ 1 (sqrt 2)))) (newline)
(display (agm 1 (/ 1 (sqrt 2)))) (newline)
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,778: Line 2,778:


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i";
include "float.s7i";
include "float.s7i";
include "math.s7i";
include "math.s7i";
Line 2,807: Line 2,807:
writeln(agm(1.0, 2.0) digits 6);
writeln(agm(1.0, 2.0) digits 6);
writeln(agm(1.0, 1.0 / sqrt(2.0)) digits 6);
writeln(agm(1.0, 1.0 / sqrt(2.0)) digits 6);
end func;</lang>
end func;</syntaxhighlight>


{{out}}
{{out}}
Line 2,816: Line 2,816:


=={{header|SequenceL}}==
=={{header|SequenceL}}==
<lang sequencel>import <Utilities/Math.sl>;
<syntaxhighlight lang=sequencel>import <Utilities/Math.sl>;


agm(a, g) :=
agm(a, g) :=
Line 2,828: Line 2,828:
agm(arithmeticMean, geometricMean);
agm(arithmeticMean, geometricMean);


main := agm(1.0, 1.0 / sqrt(2));</lang>
main := agm(1.0, 1.0 / sqrt(2));</syntaxhighlight>


{{out}}
{{out}}
Line 2,836: Line 2,836:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func agm(a, g) {
<syntaxhighlight lang=ruby>func agm(a, g) {
loop {
loop {
var (a1, g1) = ((a+g)/2, sqrt(a*g))
var (a1, g1) = ((a+g)/2, sqrt(a*g))
Line 2,844: Line 2,844:
}
}


say agm(1, 1/sqrt(2))</lang>
say agm(1, 1/sqrt(2))</syntaxhighlight>
{{out}}
{{out}}
<pre>0.8472130847939790866064991234821916364814</pre>
<pre>0.8472130847939790866064991234821916364814</pre>
Line 2,855: Line 2,855:


Better precision than this is not easily obtainable on the ZX81, unfortunately.
Better precision than this is not easily obtainable on the ZX81, unfortunately.
<lang basic> 10 LET A=1
<syntaxhighlight lang=basic> 10 LET A=1
20 LET G=1/SQR 2
20 LET G=1/SQR 2
30 GOSUB 100
30 GOSUB 100
Line 2,865: Line 2,865:
130 IF ABS(A-G)>.00000001 THEN GOTO 100
130 IF ABS(A-G)>.00000001 THEN GOTO 100
140 LET AGM=A
140 LET AGM=A
150 RETURN</lang>
150 RETURN</syntaxhighlight>
{{out}}
{{out}}
<pre>0.84721309</pre>
<pre>0.84721309</pre>
Line 2,872: Line 2,872:
{{works with|Smalltalk/X}}
{{works with|Smalltalk/X}}
That is simply a copy/paste of the already existing agm method in the Number class:
That is simply a copy/paste of the already existing agm method in the Number class:
<lang smalltalk>agm:y
<syntaxhighlight lang=smalltalk>agm:y
"return the arithmetic-geometric mean agm(x, y)
"return the arithmetic-geometric mean agm(x, y)
of the receiver (x) and the argument, y.
of the receiver (x) and the argument, y.
Line 2,890: Line 2,890:
gi := gn.
gi := gn.
] doUntil:[ delta < epsilon ].
] doUntil:[ delta < epsilon ].
^ ai</lang>
^ ai</syntaxhighlight>


<lang smalltalk>Transcript showCR: (24 agm:6).
<syntaxhighlight lang=smalltalk>Transcript showCR: (24 agm:6).
Transcript showCR: ( (1/2) agm:(1/6) ).
Transcript showCR: ( (1/2) agm:(1/6) ).
Transcript showCR: (1 agm:(1 / 2 sqrt)).</lang>
Transcript showCR: (1 agm:(1 / 2 sqrt)).</syntaxhighlight>
{{out}}
{{out}}
<pre>13.4581714817256
<pre>13.4581714817256
Line 2,903: Line 2,903:
{{works with|oracle|11.2 and higher}}
{{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.
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.
<lang sql>with
<syntaxhighlight lang=sql>with
rec (rn, a, g, diff) as (
rec (rn, a, g, diff) as (
select 1, 1, 1/sqrt(2), 1 - 1/sqrt(2)
select 1, 1, 1/sqrt(2), 1 - 1/sqrt(2)
Line 2,915: Line 2,915:
from rec
from rec
where diff <= 1e-38
where diff <= 1e-38
;</lang>
;</syntaxhighlight>




Line 2,925: Line 2,925:


=={{header|Standard ML}}==
=={{header|Standard ML}}==
<lang sml>
<syntaxhighlight lang=sml>
fun agm(a, g) = let
fun agm(a, g) = let
fun agm'(a, g, eps) =
fun agm'(a, g, eps) =
Line 2,934: Line 2,934:
in agm'(a, g, 1e~15)
in agm'(a, g, 1e~15)
end;
end;
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,941: Line 2,941:


=={{header|Stata}}==
=={{header|Stata}}==
<lang stata>mata
<syntaxhighlight lang=stata>mata


real scalar agm(real scalar a, real scalar b) {
real scalar agm(real scalar a, real scalar b) {
Line 2,954: Line 2,954:


agm(1,1/sqrt(2))
agm(1,1/sqrt(2))
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>.8472130848</pre>
<pre>.8472130848</pre>


=={{header|Swift}}==
=={{header|Swift}}==
<lang Swift>import Darwin
<syntaxhighlight lang=Swift>import Darwin


enum AGRError : Error {
enum AGRError : Error {
Line 2,989: Line 2,989:
} catch {
} catch {
print("agr is undefined when a * g < 0")
print("agr is undefined when a * g < 0")
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>0.847213084835193</pre>
<pre>0.847213084835193</pre>
Line 2,995: Line 2,995:
=={{header|Tcl}}==
=={{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).
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).
<lang tcl>proc agm {a b} {
<syntaxhighlight lang=tcl>proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
while {$a != $b && $b != $old_b} {
Line 3,004: Line 3,004:
}
}


puts [agm 1 [expr 1/sqrt(2)]]</lang>
puts [agm 1 [expr 1/sqrt(2)]]</syntaxhighlight>
Output:
Output:
<pre>0.8472130847939792</pre>
<pre>0.8472130847939792</pre>


=={{header|TI-83 BASIC}}==
=={{header|TI-83 BASIC}}==
<lang ti83b>1→A:1/sqrt(2)→G
<syntaxhighlight lang=ti83b>1→A:1/sqrt(2)→G
While abs(A-G)>e-15
While abs(A-G)>e-15
(A+G)/2→B
(A+G)/2→B
sqrt(AG)→G:B→A
sqrt(AG)→G:B→A
End
End
A</lang>
A</syntaxhighlight>
{{out}}
{{out}}
<pre>.8472130848</pre>
<pre>.8472130848</pre>
Line 3,021: Line 3,021:
{{works with|ksh93}}
{{works with|ksh93}}
ksh is one of the few unix shells that can do floating point arithmetic (bash does not).
ksh is one of the few unix shells that can do floating point arithmetic (bash does not).
<lang bash>function agm {
<syntaxhighlight lang=bash>function agm {
float a=$1 g=$2 eps=${3:-1e-11} tmp
float a=$1 g=$2 eps=${3:-1e-11} tmp
while (( abs(a-g) > eps )); do
while (( abs(a-g) > eps )); do
Line 3,032: Line 3,032:
}
}


agm $((1/sqrt(2))) 1</lang>
agm $((1/sqrt(2))) 1</syntaxhighlight>


{{output}}
{{output}}
Line 3,043: Line 3,043:
0.8472130848</pre>
0.8472130848</pre>


You can get a more approximate convergence by changing the while condition to compare the numbers as strings: change <lang bash>while (( abs(a-g) > eps ))</lang> to <lang bash>while [[ $a != $g ]]</lang>
You can get a more approximate convergence by changing the while condition to compare the numbers as strings: change <syntaxhighlight lang=bash>while (( abs(a-g) > eps ))</syntaxhighlight> to <syntaxhighlight lang=bash>while [[ $a != $g ]]</syntaxhighlight>


=={{header|VBA}}==
=={{header|VBA}}==
<lang vb>Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
<syntaxhighlight 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
Do While Abs(a - g) > tolerance
tmp = a
tmp = a
Line 3,057: Line 3,057:
Public Sub main()
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
Debug.Print agm(1, 1 / Sqr(2))
End Sub</lang>{{out}}
End Sub</syntaxhighlight>{{out}}
<pre> 0,853553390593274
<pre> 0,853553390593274
0,847224902923494
0,847224902923494
Line 3,066: Line 3,066:
=={{header|VBScript}}==
=={{header|VBScript}}==
{{trans|BBC BASIC}}
{{trans|BBC BASIC}}
<syntaxhighlight lang=vb>
<lang vb>
Function agm(a,g)
Function agm(a,g)
Do Until a = tmp_a
Do Until a = tmp_a
Line 3,077: Line 3,077:


WScript.Echo agm(1,1/Sqr(2))
WScript.Echo agm(1,1/Sqr(2))
</syntaxhighlight>
</lang>


{{Out}}
{{Out}}
Line 3,085: Line 3,085:
{{trans|C#}}
{{trans|C#}}
===Double, Decimal Versions===
===Double, Decimal Versions===
<lang vbnet>Imports System.Math
<syntaxhighlight lang=vbnet>Imports System.Math
Imports System.Console
Imports System.Console


Line 3,114: Line 3,114:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre>Double result: 0.847213084793979
<pre>Double result: 0.847213084793979
Line 3,122: Line 3,122:
{{trans|C#}}
{{trans|C#}}
{{Libheader|System.Numerics}}
{{Libheader|System.Numerics}}
<lang vbnet>Imports System.Math
<syntaxhighlight lang=vbnet>Imports System.Math
Imports System.Console
Imports System.Console
Imports BI = System.Numerics.BigInteger
Imports BI = System.Numerics.BigInteger
Line 3,157: Line 3,157:
End Sub
End Sub
End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:64ex; overflow:scroll; white-space: pre-wrap;">0.8472130847939790866064991234821916364814459103269421850605793726597340048341347597232002939946112299421222856252334109630979626658308710596997136359833842511763268142890603897067686016166500482811887218977133094117674620199443929629021672891944995072316778973468639476066710579805578521731403493983042004221192160398395535950981936412937163406460295999679705994343516020318426487569502421748638554059819545816017424178878541927588041627190120855876856483268341404312184008040358092045594943138778151209265222545743971242868207663409547336745996217926655353486256861185433086262872872875630108355631935706687147856390889821151088363521476969796126218329432284178681137684451700181460219136940270209459966835135963278808042743454817445873632200251539529362658066141983656164916262596074347237066169023530800173753128478525584306319074542749341526857906552694060031475910203327467196861247963255105546489028208552974396512499400966255286606758044873538921857014011677169765350140849524768489932573213370289846689391946658618737529663875622660459147770442046810892565844083803204091061900315370673411959410100747433105990550582052432600995169279241747821697678106168369771411073927334392155014302200708736736596227214925877619285105238036702689046390962190766364423553808590294523406519001334234510583834171218051425500392370111132541114461262890625413355052664365359582455215629339751825147065013464104705697935568130660632937334503871097709729487591717901581732028157828848714993134081549334236779704471278593761859508514667736455467920161593422399714298407078888227903265675159652843581779572728480835648996350440414073422611018338354697596266333042208499985230074270393027724347497971797326455254654301983169496846109869074390506801376611925291977093844129970701588949316666116199459226501131118396635250253056164643158720845452298877547517727274765672164898291823923889520720764283971088470596035692199292183190154814128076659269829446445714923966632997307581390495762243896242317520950731901842446244237098642728114951118082282605386248461767518014098312749725765198375649235690280021617490553142720815343954059556357637112728165705973733744297003905604015638866307222570038923015911237696012158008177907786335124086243107357158376592650454665278733787444483440631024475703968125545398226643035341641303561380163416557526558975294452116687345122019122746673319157124076375382110696814107692639007483317574339675231966033086497357138387419609898383220288269488219130281936694995442224069727616862136951165783888501219909616065545461154325314816424933269479700415949147632311292059351651899794335004597628821729262591808940550843146639378254833513955019065337087206206402407705607584879649984365159272826453442863661541914258577710675618501727803328717519518930503180550524542602233552290077141812879865435118791800635627959362476826778641224946033812608262825409889531252767753465624327921451122955551603181843313369296172304178385515712556740498341666592696958000895372457305769454227537216020968719147039887846636724326270619112707171659082464004167994112040565710364083000241929439855307399465653967781049270105541035951333943219992506667620207839469555376055179640100974921885631130101781388857879381317209594806253920130098365028791769582798590527994772194179799702494306215841946888532811549772157996019440962347768614408507573928429882375939682322367058033413477462311289762585932437663177897491107726190970448952220450963072551559009382490402136480779203476721504856844602255440999282616317431264228578762898338065072202301037175314926350463106018857377256700661838129058063895450812703131137104371613583348806583395543121790134839883321641305763524471251153947206667033010134871651632411382881763983962952612114126321979596509865678675525076076042409590751752302194610453256433324961490125353332922372386894812788502013596630537605584935892839163046940388785496002747148719780145765957904958580226006609952496736432496683346176010660815670697514238186650361083885220976165500251607311499216129477579019972924868963822060380876027628167237016681910663358577515465038133423672234764202655856558846416010210540489855618711473588497637840648642679818650448631907747038228671143515112300360708657429886477146674733750114345818852797006056211724692174847180694866251199472893444270378304620707354938052872720621560630718828685805645211106967080285699069825769177220998671959968507790681443494932804976811543680463259938693076235070999518295129581121235707245383354826190752395158273098248180549665897909168867984071707793705959045775840910473413109604194111357756620727337797833203797301137672658535747710279781409721309612142393854737462769615041307952837372882050658719152259765084027796991761175393006725492491229845082362975568722711065849435533850494532638736489804606655979954360169503092790092450057856477235876198848986034412195340795369002996411974549060741600978859537660722905160772428590070901156639138364299041220826769629797867649032356499981990765997439870548648769091024911927099968275697011368762244046402960383700066212734577664709711326374656811502985863032260337383421358423937896114617192083071953915643782093641496780334152464507396683173198363362743392555311712019454146844880895622417898031894341231284027858378289009624209541345002101072736323285272576209646851994468240550629391742053301706461917215178844296705314335503772310709716080285145314144106105023117310877779933248932087727229897821330120834074305604998159963202687793307156940302439156118926767517249511766526248547096041991473113657920697330996088897286789780735587578500623575157123771653042063631002703129296694025421967877168846655727580898306467662007014679585693082220620905330827782226503112520278733512519159918893900284319218166686548434879621972211763904959895793607330943697457628943200384117552941594754747183936381144125610351023459581080768558985657007445308909428669251190101718122826689349269528261052518556736045877702288147821446968500918347219741420546128072347950059811766364526150190788545471193803557145930744635656260752787518824386409506964649815131170591457990619376560858650175616864501924098327235724333688813080022186368700209641119724303603558649793773314916749593151188673535025505982303047060284740458456676849620934506396302909441632516408692889814507247877727673378033828929504978384342943766566737297587430575141036417476861639624198941904730996100228428079444920026904845254139188246001559089131943255610365769362364161784646693141456109984038312265504115251494445380042090428718182468431624610552637677520970104063944687837375017436089751693486887651283453677552786547090231542029453873076141196649767521919808902105772633472397958968722923357769041244458682297806209887089816018179521454920370956252850733023255060096611329479148443416687429872654204083552056456404421174124065041932362831296643126330768715450444950733554418200793669701331244638824360062439816712409346806322169771701563590417609841261977801052586956634654144702511135382841010278579543061802357275500930513955637771043922799597114118278203358118398952338720119626666828781215343331193353019800652511924103594315072427251589774226901431325149775220621148653209528291784172678852791825950189428306645453380829438548491390660090152646315666940813051689857738445716110134773528439558663918031477128997248977232695083095920860316390860179422146804892537147135669490647597566350405076105930300153453613446834614136284840473063909580064862482211399539962122107992774053203059756987131501429238941821989218445861496845306346078287058864262560349767113385390753047360747520569725532663517964059488138127648519130232826129551720747594498863925111049785977410104647258831744969489273332281068408949475978706769012216951869658194406136694310323411619613160554381608728305543504819071159752742665917363693001980988797627218662628543311906086034280619151845297823703639898449414417889008602782220998390227472837967411429578924346545640402855167478372538831386154780508035236893583332887355879794886804980971406868936719416711504307402575102269081707385928535837390976424975922421061832372517021428320986753744507133218963666908565634963306077455683011837149400258404997766113525532847665618870592978212729899729592794781820428719807102278646183807006401083138975677112754136221127444534535584959769252575758312999039536959893249951324106784265611556743660088737484274038234811784911002123537108015334407708175281579422928548731689863980071896268684985779061942582000173178473797975815609269087287850270024414741281953578873964745859459899535543412801653553049058528794674398220606230386688852700505218904927782197514115595435549125326115087432280435609563176116321811794164884206928474315699133677787956913705592704959893911100786224112449931719539890308215307126971807352814294437374058180589784287101566325873726600012296180403780429093175160473979931236882466314524590792512088916974765430245705320638670468411054034201437664442213212750799846299157010147106552946146746392249574530619682203425444816247545977269653430250686824205288099692448923652171403817749282935917315481284919621433304080904306867233682060716291289398517406255904282247558159509102324206160816363511440953267967974466214658121897383725705201831800678505181233270743236051760236565304605919728246762046497950757124332306210615236617229324468286251110577832854712371857906482302429199129753477340618812393224405123793229248698239302094605799468502209356458018864737205798950819968285087908120645175464792846657029993496146354533816989879012073959534299458051884682918835631136138879631316173442207506218212945047503433730640140356614106403320867621443183928438969994268286836082535591242751488383392264668222963323657488981599104902374571278077062853236895690028469742954774248422335523859049299225453318270693966088603518491166875108552006265340966412611220069290556369052744064893640087015171662929356529921474420793873710647399136453402185931518201576110059405556600166318190916348212818643068418256991194316266715898588673650488980580832972145195811525832974358064432698289209364284959616975339927502383832695801109608954786457256109785378297307074918168744735731189049849490781632210127110919398357638892753131749978321368280932894349330930087868884127092076359007648065118301317440813138170776478562086983456849957696333241556699085937149528437303782174166781012624737754844959408277598042857813775448446192929537153359741871355556678028606484917974827559022377376189703770332489774349235376523557139076431488967144133099539679871046284747721772185865851985971282165739148574494328320308464163956096301047370473988450307936956928683464113764226308568695688152053749196294562881085987015910764955019272667378276517237450013662421051146709184898952269727656206976263055094938932099216377529415335060027109430018977339221845390337351007942764665232509045377940478212355620488638969640291029182673024368888013982750049655688955540362739754118359277009094291839958396298535952123465573707751680432023872401008786292362558484920221296055948232317635214207117650427699747801290249150914873347204981208353486521246233538858471700470120592394582541522312967601307268280232044633644234100026474341568399123881048049819491200940244895720301881220640996997340843736095812449945913231793359333819197360248853375641030435643732302001328359990615298394916710687997693926699033522064083729586994304357670917169796698442332656830732550000321312902706719106342428311390049478179307304556219943912072209495471916547109605404919944186051724981471812994063119290173738101176617356976495636675620278895592099504686163440305250658681735840269428736633431167832903837475658050990783985384926064721246565130660487673608585790218386643241627198210378772796337736742692945663985470529377745854692207002046330357343505517537014050310355526578082729897049230547545589009275410944504014157125357682801074915174627928533783099570631952876838237806368177841661186334747789420166190186143388804514884174361681454810362321037643274595653364629397295294049952661691181657740018116146497654407589150912557599100855273107733703213603505619407350405223414533224306604743600257212590127202517146952605462439215815151732661454812243619860357386922465403688559787750083268386930674253759349376972691382532780570135683441862315010318955128705494038594760949278590520009881447715839714713971813720554960331191642239195313230213875992717401904622413925914800620171561815889352945121978193704745708538695427900233080410588007250947512318930796844637224171170594606197614751977323896101315556406372309310279476973938229476346893933755946893665094049910252612163538072005644241026471164639800490998535570282059396054554479255558624918709232180130454102936332893619326596350851413637207293142767763267817840066780089558654877782630822818446508158509625695020697797889664140551101421185533444015948880284701657904464926309216120238068566472631611326995533585414320547442896728173291714010643730593960222482733969720865809194288803963344344876467583385597351333330628439786357062196382217705500672607607570202305548328439335937369624085404957344415141889143812206076832329063384332685935928226648361622876815670931303789678327741487845287838232474038340893449427806045589018183673133602271167285304427194507315740913600066356089181219040305019319028163972135790696025211929562455952835850442627787993214468221041325612271290302469610374855134599106662606082143546126463790846952338680559237822828610361386416013753920426888371192602742087474507782730180882648297991489233434653363930327991816476995529468892904060335470265188317825821391915073117022336839564945335630414192442838503954209073337511117053790819768061378846157004292392264788138228486672543415580694421193506836000488465561599083339184724263183698928130695654949153165010313216361224018298711517222401523368101476246169896417259748838727189598765602350324828709741468793415378708814573190327920453219231685852735108372055942456601545647944675449566859142997988233179819059574125368681032194798082603876241044848730208905065871934264174092007936669883601462309762759844113071525758916288010581709353072588887654386253201848624931923638568216562603110434528313030704972291334873033240933736956347974889824930017415805659182123288343858101250171537305398462043432455721482088547523494730467761429282915391485852688505423074450548192619166975975031503447208211845313907683486006908772752077246485706597636740936173143436990399498908375710246545650814962015988805204483379491707040848303909417512426275869868668644293498242419667403627076032399201407183071270759837132000712447159523642782162488472933913713634046138974088894178399320090051543608421618891328957740354384456107645016010462709579098652495342014766016330458293537653454523438667413798731255017029554582809547897542497367109038598264606895622241257303208140890607025206140457815282368504505765710043804228592032720729190222134651835930255942940875306994701101153416476785623543575023993736414532895773499876167502240919794121893188059017977444329403624038551082491954751841177014150820554999148803286500065069030165028455616533514890711974194172310029663247936640825364542104897640445108081123906368188594908660418340025631562661211506365309297219580687177632051461355581309500814563826112416521487163593643553646268872746276680368630680088231249970572706496265335285424273723449757482776061300818063419639083097882249478922949525891665782610044424440110326748539620120023397129834624242363283711074267309902126029110038109050751840523266273905031934856015485510632624318778970878895198168073096354223096005536267735905099473408744371024816727970009494589707630185344952680106730984246828848883760016695887137355969244555238536396178788134209309376484848406842940499731494663578455826688245825356635393289729316700066238128368519670627697889769929009597838069557440769080950069594659578325366066060213000525012998145215099629307110700615796004759918829827472751877492472674770755413679265775060149528336859838085353420874215682758801259992855903410097963019943741001394975591822918846705741010634931594527954742032057295356596869586863097328488381174243827058441735659667485315202886191192125286398739560928127513223214119754229343092375569339614672740517569529376699061052365448344078610425576694541873486379356070861240473688356773437140126350120823765176390562050604076894729400293162079760342896846897639867830553941515230713725560502914671175123451932131962571791940911728951123948113598860588062424037835751996487088330150679210175429060531418836978611027896830689666851868410470182364780700615529883149883111601949965815038674390467105247175993726709203381051984777006122752302698038537619917731907133105816779008651480172440446403764720673784583395382889380902941273987910475254258486561698048543296782281040453997661165123290729161619992628751086519341731116513305659182981762584769428708454819029344222186027977405519291266188948708010515922860149238393490889782166965109499761673179583522105791358724355029782111425280584380959770472177893827382916471882671437865821461326011263516554280516418422188264141890686619186492751718984735037496602686033671961304915922609442146773092074476794711917820209913226872184947548378003848726148872742881265579174794634151444545105599464567614478293387968015412886418098284885525959617399177657635267081989985408930744564199296902459275405143647525648661932959903068323866757518479741015342911416508753572892479684280248440220211898390243430190746592470563991910024225814399068391457857458095344096826158489731615822039837691005171654390590093326827586419753439483771905973079465029210363641972615923872187876095687197681934481955852567024141433671590889694204781798936556351775101591005026585947279448642317311892727153525046034081896227383114600546852406398855471859684088277722162250586368419379964112646321070639818773794369650252104438622320671517228411475433482803041707675438555447584321271846396281391925884972509051040944134450429845346071848875654240709690138592611645519676563708429710676494635766201285381926791204110977805857352062737510466943591592074904378966129808716274322385039032007477854211063899544954185997641428116395197239708078986048758264126544825149923227286176571389697334537835963603962709038002668921324389159009375225033651171937770657226295341257068980907793198879997076783263303670667342657925395849950582363998610492878479976185891384024744790742355981796013254960652684988733518397287191251899388324341602608356164496670902390042273216221931567939944001215159910054381084520081133103207553492484487369268314444466610780275891777468369344585045949963237156043800258227618908603074550819931892899703285549507330240121766349515315827830897786432254556221744305752825143708087184314470811004510108612122699931396969361066523608721126359012344828262284427191281973187269761974740398071778378188160519801862257232970224762494767912932684020188061795236229174601398576604233579094407723017353015337974435643738584248250538061547193075224429309117207447677149522141919390974201716026970557825836923707297811545552570788004955666915477901830719591663516687057984336951611189153751912396714116378197000784953115386326766369269172016978409040396969804861828436417776804088449208439901095951205751340861060375353408155737087188313898337656322533650946010308686111901241541794900659835366926383515058402026098259570385429145865025692157987309807064597082326377138235585737704225628144262793497769429358804020882742028263786443615935817930817858306265712263479452174065216410798029333573961137404301928294367884626832432449078812684787281988676202931062510264948586549463964789154366240635570346688477784815271412470430646040615614277320107003575855033995279377529716156628381118518085523414187577256025217995103662771477552291036839539792329375184700131215428652464111526297830742328651189481978920924682746392250346179819781021313400022272303222234731521016033826145645816472110340883197207109422849637006090510260943044730126801795349152894613046101033061811314821366141874985466628809585678299308824993966655499624380015821082410781190328189506855057581990908848597095494573176672201417764187253816862426293852974092626551536758155537683368451820154793964862810533857810979434793077956125541240828563089647076354827276586047900779183041806574320855302776686899978897939486987950729652971448050889517660684386673056662911929857913206598752762097197279390208473846210277152094212386266930256260451209117402079233658157593274696841906354187366092529138116574357045728290417433832596884391356956442617823006949118156994294295529170211353842468704890572313005646106202029653246628477843902025194715815133791174898257040115532858624973690714844800747184719290671002133191274834310662201874141841328708920709275866745037664169280121112867057832132585948539987132879098472640550013972043153470930436509718084070853723316111111611632600262171748813737621046013600544051850633175245231989785291065646466038278748870331134307620041356514295482843502245454400571392386492526283423907951705366640483826875013469850263767974528926285288366544314868036628329638912254207094687335597669512007687507292940623176435604796651807847095408991068514998003358735387989422028901542800717906482276185298683079286137204396993726503610285463352157718364571843381950031926272352293654343387522809514152498052577486366048613580539162662183475105825647260311633442002377527140625112075332294909525522330744664115572260242435895269482927435844022622001466247093866533879048392320516224276433339282642640953964341822416705658461244760448817737705782669080880834418822622611342632727419248415651121035047131961583094994438779439078380664656207143187309895280874153167621657602227990850199615587578332393883365169478142077533262283694526612005465820771400826060398839255150948861553177333447506822679211849690448880479070102043288205874672361672971246062341973369704807867768609989464712379097525706498042381815865399434983035941162258347729020489356838477197804973214911448748749915616679253857438010864500220134843719609727912761136925035123155282535741655826107266099467657016111855684257826878422197833994329148734893923892153298966294232703135845615804723993624827409373966761563257981994036006655039613941881183164267144485664874468348587099434743710128859267552473831462181434321232124758618476925803128913233878664527525204324484796532776273320171351979849530142473805976430318655810403609897537469226336015596525652284888167037460054235043655813438329870872734142062859147847007274999414885129441657918212383876056572545671794085637289277002790218604788423519924573051811976377731594412994393860534559159658127123862955315918182841923881357245009246238507097741891437575676886206936433608263660374355173185026954239766173038826275043838965247160428689739548061640664606565379050539422795708801840829664956978192406737307076253014257542221763860230431809477056758905681723033326311408802886092880151777469082375063137750925275331638009836786645991949881018108222446858443984865972449621097999331605268587810061927125889694400669979755648800940895626242917531834388920035663113368763931463847812763130237825562198311791061780856687903309789539747505239545316630638169559777653347655949908779202359718666623572487055558216484036084925217803431104356647417600193631613474196113126657206064282217690428541246560204561459484317744683213906021267727411189443675804442911583757423572500214191467493342871160840582639470485636370375679604797073490813681083838562113841391587052553615073991983125473434527404596547926972539542447555990332809716643578039646945749813368621152410490288581779206318208255069166455507840899628333174744873951607229399258854694188637978240144635295264982572856632103053550891057171748674115218494774077589151115819489068851971959768129214023511454382738867557288320426608338030759515727545577639726238470674634011626346953231815229549718996906470438903536574430644436472716449550868519871817092814068746449470806856174570885105064766494332205391085097539987897980672278869943134632799032372604933150163386774039430519493297142505321117669011820293604482694166301309801111227443654953271242388534939973277749999335296667138307969441135719079969506099821923206878892624416110175909254904610286553512032488285673735148429324009831633211264460376172046209384270528903772251057643968938983722779640468452705694321085455273829462711022737243290606294601651732654594463569861350966095209962038508010899673666470073918705760679801337058347046567503369379598928154437380765511031719081985901371088639600700705631873099251480947989238619052479230983309717938226245725600119571130722386790431255742179135633111146646083268382596762356018472772209198013121983224179079476134977421748168833934278876403014334318798493417716613256506422668264638388429786875443810986754386459491846082078633346046469418429778813833857755519670005669840456587642130852057050148314568259387702428619224671173187370822224627538313365937868201435535126600146246249435880806572693573084485615073901842761167215162204840459913839674251648</pre>
<pre style="height:64ex; overflow:scroll; white-space: pre-wrap;">0.8472130847939790866064991234821916364814459103269421850605793726597340048341347597232002939946112299421222856252334109630979626658308710596997136359833842511763268142890603897067686016166500482811887218977133094117674620199443929629021672891944995072316778973468639476066710579805578521731403493983042004221192160398395535950981936412937163406460295999679705994343516020318426487569502421748638554059819545816017424178878541927588041627190120855876856483268341404312184008040358092045594943138778151209265222545743971242868207663409547336745996217926655353486256861185433086262872872875630108355631935706687147856390889821151088363521476969796126218329432284178681137684451700181460219136940270209459966835135963278808042743454817445873632200251539529362658066141983656164916262596074347237066169023530800173753128478525584306319074542749341526857906552694060031475910203327467196861247963255105546489028208552974396512499400966255286606758044873538921857014011677169765350140849524768489932573213370289846689391946658618737529663875622660459147770442046810892565844083803204091061900315370673411959410100747433105990550582052432600995169279241747821697678106168369771411073927334392155014302200708736736596227214925877619285105238036702689046390962190766364423553808590294523406519001334234510583834171218051425500392370111132541114461262890625413355052664365359582455215629339751825147065013464104705697935568130660632937334503871097709729487591717901581732028157828848714993134081549334236779704471278593761859508514667736455467920161593422399714298407078888227903265675159652843581779572728480835648996350440414073422611018338354697596266333042208499985230074270393027724347497971797326455254654301983169496846109869074390506801376611925291977093844129970701588949316666116199459226501131118396635250253056164643158720845452298877547517727274765672164898291823923889520720764283971088470596035692199292183190154814128076659269829446445714923966632997307581390495762243896242317520950731901842446244237098642728114951118082282605386248461767518014098312749725765198375649235690280021617490553142720815343954059556357637112728165705973733744297003905604015638866307222570038923015911237696012158008177907786335124086243107357158376592650454665278733787444483440631024475703968125545398226643035341641303561380163416557526558975294452116687345122019122746673319157124076375382110696814107692639007483317574339675231966033086497357138387419609898383220288269488219130281936694995442224069727616862136951165783888501219909616065545461154325314816424933269479700415949147632311292059351651899794335004597628821729262591808940550843146639378254833513955019065337087206206402407705607584879649984365159272826453442863661541914258577710675618501727803328717519518930503180550524542602233552290077141812879865435118791800635627959362476826778641224946033812608262825409889531252767753465624327921451122955551603181843313369296172304178385515712556740498341666592696958000895372457305769454227537216020968719147039887846636724326270619112707171659082464004167994112040565710364083000241929439855307399465653967781049270105541035951333943219992506667620207839469555376055179640100974921885631130101781388857879381317209594806253920130098365028791769582798590527994772194179799702494306215841946888532811549772157996019440962347768614408507573928429882375939682322367058033413477462311289762585932437663177897491107726190970448952220450963072551559009382490402136480779203476721504856844602255440999282616317431264228578762898338065072202301037175314926350463106018857377256700661838129058063895450812703131137104371613583348806583395543121790134839883321641305763524471251153947206667033010134871651632411382881763983962952612114126321979596509865678675525076076042409590751752302194610453256433324961490125353332922372386894812788502013596630537605584935892839163046940388785496002747148719780145765957904958580226006609952496736432496683346176010660815670697514238186650361083885220976165500251607311499216129477579019972924868963822060380876027628167237016681910663358577515465038133423672234764202655856558846416010210540489855618711473588497637840648642679818650448631907747038228671143515112300360708657429886477146674733750114345818852797006056211724692174847180694866251199472893444270378304620707354938052872720621560630718828685805645211106967080285699069825769177220998671959968507790681443494932804976811543680463259938693076235070999518295129581121235707245383354826190752395158273098248180549665897909168867984071707793705959045775840910473413109604194111357756620727337797833203797301137672658535747710279781409721309612142393854737462769615041307952837372882050658719152259765084027796991761175393006725492491229845082362975568722711065849435533850494532638736489804606655979954360169503092790092450057856477235876198848986034412195340795369002996411974549060741600978859537660722905160772428590070901156639138364299041220826769629797867649032356499981990765997439870548648769091024911927099968275697011368762244046402960383700066212734577664709711326374656811502985863032260337383421358423937896114617192083071953915643782093641496780334152464507396683173198363362743392555311712019454146844880895622417898031894341231284027858378289009624209541345002101072736323285272576209646851994468240550629391742053301706461917215178844296705314335503772310709716080285145314144106105023117310877779933248932087727229897821330120834074305604998159963202687793307156940302439156118926767517249511766526248547096041991473113657920697330996088897286789780735587578500623575157123771653042063631002703129296694025421967877168846655727580898306467662007014679585693082220620905330827782226503112520278733512519159918893900284319218166686548434879621972211763904959895793607330943697457628943200384117552941594754747183936381144125610351023459581080768558985657007445308909428669251190101718122826689349269528261052518556736045877702288147821446968500918347219741420546128072347950059811766364526150190788545471193803557145930744635656260752787518824386409506964649815131170591457990619376560858650175616864501924098327235724333688813080022186368700209641119724303603558649793773314916749593151188673535025505982303047060284740458456676849620934506396302909441632516408692889814507247877727673378033828929504978384342943766566737297587430575141036417476861639624198941904730996100228428079444920026904845254139188246001559089131943255610365769362364161784646693141456109984038312265504115251494445380042090428718182468431624610552637677520970104063944687837375017436089751693486887651283453677552786547090231542029453873076141196649767521919808902105772633472397958968722923357769041244458682297806209887089816018179521454920370956252850733023255060096611329479148443416687429872654204083552056456404421174124065041932362831296643126330768715450444950733554418200793669701331244638824360062439816712409346806322169771701563590417609841261977801052586956634654144702511135382841010278579543061802357275500930513955637771043922799597114118278203358118398952338720119626666828781215343331193353019800652511924103594315072427251589774226901431325149775220621148653209528291784172678852791825950189428306645453380829438548491390660090152646315666940813051689857738445716110134773528439558663918031477128997248977232695083095920860316390860179422146804892537147135669490647597566350405076105930300153453613446834614136284840473063909580064862482211399539962122107992774053203059756987131501429238941821989218445861496845306346078287058864262560349767113385390753047360747520569725532663517964059488138127648519130232826129551720747594498863925111049785977410104647258831744969489273332281068408949475978706769012216951869658194406136694310323411619613160554381608728305543504819071159752742665917363693001980988797627218662628543311906086034280619151845297823703639898449414417889008602782220998390227472837967411429578924346545640402855167478372538831386154780508035236893583332887355879794886804980971406868936719416711504307402575102269081707385928535837390976424975922421061832372517021428320986753744507133218963666908565634963306077455683011837149400258404997766113525532847665618870592978212729899729592794781820428719807102278646183807006401083138975677112754136221127444534535584959769252575758312999039536959893249951324106784265611556743660088737484274038234811784911002123537108015334407708175281579422928548731689863980071896268684985779061942582000173178473797975815609269087287850270024414741281953578873964745859459899535543412801653553049058528794674398220606230386688852700505218904927782197514115595435549125326115087432280435609563176116321811794164884206928474315699133677787956913705592704959893911100786224112449931719539890308215307126971807352814294437374058180589784287101566325873726600012296180403780429093175160473979931236882466314524590792512088916974765430245705320638670468411054034201437664442213212750799846299157010147106552946146746392249574530619682203425444816247545977269653430250686824205288099692448923652171403817749282935917315481284919621433304080904306867233682060716291289398517406255904282247558159509102324206160816363511440953267967974466214658121897383725705201831800678505181233270743236051760236565304605919728246762046497950757124332306210615236617229324468286251110577832854712371857906482302429199129753477340618812393224405123793229248698239302094605799468502209356458018864737205798950819968285087908120645175464792846657029993496146354533816989879012073959534299458051884682918835631136138879631316173442207506218212945047503433730640140356614106403320867621443183928438969994268286836082535591242751488383392264668222963323657488981599104902374571278077062853236895690028469742954774248422335523859049299225453318270693966088603518491166875108552006265340966412611220069290556369052744064893640087015171662929356529921474420793873710647399136453402185931518201576110059405556600166318190916348212818643068418256991194316266715898588673650488980580832972145195811525832974358064432698289209364284959616975339927502383832695801109608954786457256109785378297307074918168744735731189049849490781632210127110919398357638892753131749978321368280932894349330930087868884127092076359007648065118301317440813138170776478562086983456849957696333241556699085937149528437303782174166781012624737754844959408277598042857813775448446192929537153359741871355556678028606484917974827559022377376189703770332489774349235376523557139076431488967144133099539679871046284747721772185865851985971282165739148574494328320308464163956096301047370473988450307936956928683464113764226308568695688152053749196294562881085987015910764955019272667378276517237450013662421051146709184898952269727656206976263055094938932099216377529415335060027109430018977339221845390337351007942764665232509045377940478212355620488638969640291029182673024368888013982750049655688955540362739754118359277009094291839958396298535952123465573707751680432023872401008786292362558484920221296055948232317635214207117650427699747801290249150914873347204981208353486521246233538858471700470120592394582541522312967601307268280232044633644234100026474341568399123881048049819491200940244895720301881220640996997340843736095812449945913231793359333819197360248853375641030435643732302001328359990615298394916710687997693926699033522064083729586994304357670917169796698442332656830732550000321312902706719106342428311390049478179307304556219943912072209495471916547109605404919944186051724981471812994063119290173738101176617356976495636675620278895592099504686163440305250658681735840269428736633431167832903837475658050990783985384926064721246565130660487673608585790218386643241627198210378772796337736742692945663985470529377745854692207002046330357343505517537014050310355526578082729897049230547545589009275410944504014157125357682801074915174627928533783099570631952876838237806368177841661186334747789420166190186143388804514884174361681454810362321037643274595653364629397295294049952661691181657740018116146497654407589150912557599100855273107733703213603505619407350405223414533224306604743600257212590127202517146952605462439215815151732661454812243619860357386922465403688559787750083268386930674253759349376972691382532780570135683441862315010318955128705494038594760949278590520009881447715839714713971813720554960331191642239195313230213875992717401904622413925914800620171561815889352945121978193704745708538695427900233080410588007250947512318930796844637224171170594606197614751977323896101315556406372309310279476973938229476346893933755946893665094049910252612163538072005644241026471164639800490998535570282059396054554479255558624918709232180130454102936332893619326596350851413637207293142767763267817840066780089558654877782630822818446508158509625695020697797889664140551101421185533444015948880284701657904464926309216120238068566472631611326995533585414320547442896728173291714010643730593960222482733969720865809194288803963344344876467583385597351333330628439786357062196382217705500672607607570202305548328439335937369624085404957344415141889143812206076832329063384332685935928226648361622876815670931303789678327741487845287838232474038340893449427806045589018183673133602271167285304427194507315740913600066356089181219040305019319028163972135790696025211929562455952835850442627787993214468221041325612271290302469610374855134599106662606082143546126463790846952338680559237822828610361386416013753920426888371192602742087474507782730180882648297991489233434653363930327991816476995529468892904060335470265188317825821391915073117022336839564945335630414192442838503954209073337511117053790819768061378846157004292392264788138228486672543415580694421193506836000488465561599083339184724263183698928130695654949153165010313216361224018298711517222401523368101476246169896417259748838727189598765602350324828709741468793415378708814573190327920453219231685852735108372055942456601545647944675449566859142997988233179819059574125368681032194798082603876241044848730208905065871934264174092007936669883601462309762759844113071525758916288010581709353072588887654386253201848624931923638568216562603110434528313030704972291334873033240933736956347974889824930017415805659182123288343858101250171537305398462043432455721482088547523494730467761429282915391485852688505423074450548192619166975975031503447208211845313907683486006908772752077246485706597636740936173143436990399498908375710246545650814962015988805204483379491707040848303909417512426275869868668644293498242419667403627076032399201407183071270759837132000712447159523642782162488472933913713634046138974088894178399320090051543608421618891328957740354384456107645016010462709579098652495342014766016330458293537653454523438667413798731255017029554582809547897542497367109038598264606895622241257303208140890607025206140457815282368504505765710043804228592032720729190222134651835930255942940875306994701101153416476785623543575023993736414532895773499876167502240919794121893188059017977444329403624038551082491954751841177014150820554999148803286500065069030165028455616533514890711974194172310029663247936640825364542104897640445108081123906368188594908660418340025631562661211506365309297219580687177632051461355581309500814563826112416521487163593643553646268872746276680368630680088231249970572706496265335285424273723449757482776061300818063419639083097882249478922949525891665782610044424440110326748539620120023397129834624242363283711074267309902126029110038109050751840523266273905031934856015485510632624318778970878895198168073096354223096005536267735905099473408744371024816727970009494589707630185344952680106730984246828848883760016695887137355969244555238536396178788134209309376484848406842940499731494663578455826688245825356635393289729316700066238128368519670627697889769929009597838069557440769080950069594659578325366066060213000525012998145215099629307110700615796004759918829827472751877492472674770755413679265775060149528336859838085353420874215682758801259992855903410097963019943741001394975591822918846705741010634931594527954742032057295356596869586863097328488381174243827058441735659667485315202886191192125286398739560928127513223214119754229343092375569339614672740517569529376699061052365448344078610425576694541873486379356070861240473688356773437140126350120823765176390562050604076894729400293162079760342896846897639867830553941515230713725560502914671175123451932131962571791940911728951123948113598860588062424037835751996487088330150679210175429060531418836978611027896830689666851868410470182364780700615529883149883111601949965815038674390467105247175993726709203381051984777006122752302698038537619917731907133105816779008651480172440446403764720673784583395382889380902941273987910475254258486561698048543296782281040453997661165123290729161619992628751086519341731116513305659182981762584769428708454819029344222186027977405519291266188948708010515922860149238393490889782166965109499761673179583522105791358724355029782111425280584380959770472177893827382916471882671437865821461326011263516554280516418422188264141890686619186492751718984735037496602686033671961304915922609442146773092074476794711917820209913226872184947548378003848726148872742881265579174794634151444545105599464567614478293387968015412886418098284885525959617399177657635267081989985408930744564199296902459275405143647525648661932959903068323866757518479741015342911416508753572892479684280248440220211898390243430190746592470563991910024225814399068391457857458095344096826158489731615822039837691005171654390590093326827586419753439483771905973079465029210363641972615923872187876095687197681934481955852567024141433671590889694204781798936556351775101591005026585947279448642317311892727153525046034081896227383114600546852406398855471859684088277722162250586368419379964112646321070639818773794369650252104438622320671517228411475433482803041707675438555447584321271846396281391925884972509051040944134450429845346071848875654240709690138592611645519676563708429710676494635766201285381926791204110977805857352062737510466943591592074904378966129808716274322385039032007477854211063899544954185997641428116395197239708078986048758264126544825149923227286176571389697334537835963603962709038002668921324389159009375225033651171937770657226295341257068980907793198879997076783263303670667342657925395849950582363998610492878479976185891384024744790742355981796013254960652684988733518397287191251899388324341602608356164496670902390042273216221931567939944001215159910054381084520081133103207553492484487369268314444466610780275891777468369344585045949963237156043800258227618908603074550819931892899703285549507330240121766349515315827830897786432254556221744305752825143708087184314470811004510108612122699931396969361066523608721126359012344828262284427191281973187269761974740398071778378188160519801862257232970224762494767912932684020188061795236229174601398576604233579094407723017353015337974435643738584248250538061547193075224429309117207447677149522141919390974201716026970557825836923707297811545552570788004955666915477901830719591663516687057984336951611189153751912396714116378197000784953115386326766369269172016978409040396969804861828436417776804088449208439901095951205751340861060375353408155737087188313898337656322533650946010308686111901241541794900659835366926383515058402026098259570385429145865025692157987309807064597082326377138235585737704225628144262793497769429358804020882742028263786443615935817930817858306265712263479452174065216410798029333573961137404301928294367884626832432449078812684787281988676202931062510264948586549463964789154366240635570346688477784815271412470430646040615614277320107003575855033995279377529716156628381118518085523414187577256025217995103662771477552291036839539792329375184700131215428652464111526297830742328651189481978920924682746392250346179819781021313400022272303222234731521016033826145645816472110340883197207109422849637006090510260943044730126801795349152894613046101033061811314821366141874985466628809585678299308824993966655499624380015821082410781190328189506855057581990908848597095494573176672201417764187253816862426293852974092626551536758155537683368451820154793964862810533857810979434793077956125541240828563089647076354827276586047900779183041806574320855302776686899978897939486987950729652971448050889517660684386673056662911929857913206598752762097197279390208473846210277152094212386266930256260451209117402079233658157593274696841906354187366092529138116574357045728290417433832596884391356956442617823006949118156994294295529170211353842468704890572313005646106202029653246628477843902025194715815133791174898257040115532858624973690714844800747184719290671002133191274834310662201874141841328708920709275866745037664169280121112867057832132585948539987132879098472640550013972043153470930436509718084070853723316111111611632600262171748813737621046013600544051850633175245231989785291065646466038278748870331134307620041356514295482843502245454400571392386492526283423907951705366640483826875013469850263767974528926285288366544314868036628329638912254207094687335597669512007687507292940623176435604796651807847095408991068514998003358735387989422028901542800717906482276185298683079286137204396993726503610285463352157718364571843381950031926272352293654343387522809514152498052577486366048613580539162662183475105825647260311633442002377527140625112075332294909525522330744664115572260242435895269482927435844022622001466247093866533879048392320516224276433339282642640953964341822416705658461244760448817737705782669080880834418822622611342632727419248415651121035047131961583094994438779439078380664656207143187309895280874153167621657602227990850199615587578332393883365169478142077533262283694526612005465820771400826060398839255150948861553177333447506822679211849690448880479070102043288205874672361672971246062341973369704807867768609989464712379097525706498042381815865399434983035941162258347729020489356838477197804973214911448748749915616679253857438010864500220134843719609727912761136925035123155282535741655826107266099467657016111855684257826878422197833994329148734893923892153298966294232703135845615804723993624827409373966761563257981994036006655039613941881183164267144485664874468348587099434743710128859267552473831462181434321232124758618476925803128913233878664527525204324484796532776273320171351979849530142473805976430318655810403609897537469226336015596525652284888167037460054235043655813438329870872734142062859147847007274999414885129441657918212383876056572545671794085637289277002790218604788423519924573051811976377731594412994393860534559159658127123862955315918182841923881357245009246238507097741891437575676886206936433608263660374355173185026954239766173038826275043838965247160428689739548061640664606565379050539422795708801840829664956978192406737307076253014257542221763860230431809477056758905681723033326311408802886092880151777469082375063137750925275331638009836786645991949881018108222446858443984865972449621097999331605268587810061927125889694400669979755648800940895626242917531834388920035663113368763931463847812763130237825562198311791061780856687903309789539747505239545316630638169559777653347655949908779202359718666623572487055558216484036084925217803431104356647417600193631613474196113126657206064282217690428541246560204561459484317744683213906021267727411189443675804442911583757423572500214191467493342871160840582639470485636370375679604797073490813681083838562113841391587052553615073991983125473434527404596547926972539542447555990332809716643578039646945749813368621152410490288581779206318208255069166455507840899628333174744873951607229399258854694188637978240144635295264982572856632103053550891057171748674115218494774077589151115819489068851971959768129214023511454382738867557288320426608338030759515727545577639726238470674634011626346953231815229549718996906470438903536574430644436472716449550868519871817092814068746449470806856174570885105064766494332205391085097539987897980672278869943134632799032372604933150163386774039430519493297142505321117669011820293604482694166301309801111227443654953271242388534939973277749999335296667138307969441135719079969506099821923206878892624416110175909254904610286553512032488285673735148429324009831633211264460376172046209384270528903772251057643968938983722779640468452705694321085455273829462711022737243290606294601651732654594463569861350966095209962038508010899673666470073918705760679801337058347046567503369379598928154437380765511031719081985901371088639600700705631873099251480947989238619052479230983309717938226245725600119571130722386790431255742179135633111146646083268382596762356018472772209198013121983224179079476134977421748168833934278876403014334318798493417716613256506422668264638388429786875443810986754386459491846082078633346046469418429778813833857755519670005669840456587642130852057050148314568259387702428619224671173187370822224627538313365937868201435535126600146246249435880806572693573084485615073901842761167215162204840459913839674251648</pre>


=={{header|Vlang}}==
=={{header|Vlang}}==
<lang vlang>import math
<syntaxhighlight lang=vlang>import math
const ep = 1e-14
const ep = 1e-14
Line 3,177: Line 3,177:
fn main() {
fn main() {
println(agm(1.0, 1.0/math.sqrt2))
println(agm(1.0, 1.0/math.sqrt2))
}</lang>
}</syntaxhighlight>
Using standard math module
Using standard math module
<lang vlang>import math.stats
<syntaxhighlight lang=vlang>import math.stats
import math
import math


fn main() {
fn main() {
println(stats.geometric_mean<f64>([1.0, 1.0/math.sqrt2]))
println(stats.geometric_mean<f64>([1.0, 1.0/math.sqrt2]))
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>0.8408964152537145
<pre>0.8408964152537145
Line 3,191: Line 3,191:
=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Go}}
{{trans|Go}}
<lang ecmascript>var eps = 1e-14
<syntaxhighlight lang=ecmascript>var eps = 1e-14


var agm = Fn.new { |a, g|
var agm = Fn.new { |a, g|
Line 3,202: Line 3,202:
}
}


System.print(agm.call(1, 1/2.sqrt))</lang>
System.print(agm.call(1, 1/2.sqrt))</syntaxhighlight>


{{out}}
{{out}}
Line 3,210: Line 3,210:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>include c:\cxpl\codesi;
<syntaxhighlight lang=XPL0>include c:\cxpl\codesi;
real A, A1, G;
real A, A1, G;
[Format(0, 16);
[Format(0, 16);
Line 3,219: Line 3,219:
RlOut(0, A); RlOut(0, G); RlOut(0, A-G); CrLf(0);
RlOut(0, A); RlOut(0, G); RlOut(0, A-G); CrLf(0);
until A=G;
until A=G;
]</lang>
]</syntaxhighlight>


Output:
Output:
Line 3,231: Line 3,231:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|XPL0}}
{{trans|XPL0}}
<lang zkl>a:=1.0; g:=1.0/(2.0).sqrt();
<syntaxhighlight lang=zkl>a:=1.0; g:=1.0/(2.0).sqrt();
while(not a.closeTo(g,1.0e-15)){
while(not a.closeTo(g,1.0e-15)){
a1:=(a+g)/2.0; g=(a*g).sqrt(); a=a1;
a1:=(a+g)/2.0; g=(a*g).sqrt(); a=a1;
println(a," ",g," ",a-g);
println(a," ",g," ",a-g);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,244: Line 3,244:
</pre>
</pre>
Or, using tail recursion
Or, using tail recursion
<lang zkl>fcn(a=1.0, g=1.0/(2.0).sqrt()){ println(a," ",g," ",a-g);
<syntaxhighlight 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()));
if(a.closeTo(g,1.0e-15)) return(a) else return(self.fcn((a+g)/2.0, (a*g).sqrt()));
}()</lang>
}()</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,258: Line 3,258:
=={{header|ZX Spectrum Basic}}==
=={{header|ZX Spectrum Basic}}==
{{trans|ERRE}}
{{trans|ERRE}}
<lang zxbasic>10 LET a=1: LET g=1/SQR 2
<syntaxhighlight lang=zxbasic>10 LET a=1: LET g=1/SQR 2
20 LET ta=a
20 LET ta=a
30 LET a=(a+g)/2
30 LET a=(a+g)/2
Line 3,264: Line 3,264:
50 IF a<ta THEN GO TO 20
50 IF a<ta THEN GO TO 20
60 PRINT a
60 PRINT a
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>0.84721309</pre>
<pre>0.84721309</pre>