Arithmetic-geometric mean: Difference between revisions

no edit summary
m (→‎{{header|C sharp}}: Regularize header markup to recommended on category page)
imported>Lacika7
No edit summary
 
(27 intermediate revisions by 16 users not shown)
Line 22:
=={{header|11l}}==
{{trans|Python}}
<langsyntaxhighlight lang="11l">F agm(a0, g0, tolerance = 1e-10)
V an = (a0 + g0) / 2.0
V gn = sqrt(a0 * g0)
Line 29:
R an
print(agm(1, 1 / sqrt(2)))</langsyntaxhighlight>
{{out}}
<pre>0.847213</pre>
Line 35:
=={{header|360 Assembly}}==
For maximum compatibility, this program uses only the basic instruction set.
<langsyntaxhighlight lang="360asm">AGM CSECT
USING AGM,R13
SAVEAREA B STM-SAVEAREA(R15)
Line 125:
LTORG
YREGS
END AGM</langsyntaxhighlight>
{{out}}
<pre>
Line 132:
 
=={{header|8th}}==
<langsyntaxhighlight lang="8th">: epsilon 1.0e-12 ;
 
with: n
Line 146:
;with
bye
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 155:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:REALMATH.ACT"
 
PROC Agm(REAL POINTER a0,g0,result)
Line 191:
Print(",") PrintR(g)
Print(")=") PrintRE(res)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Arithmetic-geometric_mean.png Screenshot from Atari 8-bit computer]
Line 199:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions;
 
procedure Arith_Geom_Mean is
Line 224:
begin
N_IO.Put(AGM(1.0, 1.0/Math.Sqrt(2.0)), Fore => 1, Aft => 17, Exp => 0);
end Arith_Geom_Mean;</langsyntaxhighlight>
 
Output:<pre>0.84721308479397909</pre>
Line 232:
 
Printing out the difference between the means at each iteration nicely demonstrates the quadratic convergence.
<langsyntaxhighlight lang="algol68">
BEGIN
PROC agm = (LONG REAL x, y) LONG REAL :
Line 239:
ELIF x + y = LONG 0.0 THEN LONG 0.0 CO Edge cases CO
ELSE
LONG REAL a := x, g := y;
LONG REAL epsilon := a + g;
LONG REAL next a := (a + g) / LONG 2.0, next g := long sqrt (a * g);
LONG REAL next epsilon := ABS (a - g);
WHILE next epsilon < epsilon
DO
print ((epsilon, " ", next epsilon, newline));
epsilon := next epsilon;
a := next a; g := next g;
next a := (a + g) / LONG 2.0; next g := long sqrt (a * g);
next epsilon := ABS (a - g)
OD;
a
a
FI
END;
printf (($l(-35,33)l$, agm (LONG 1.0, LONG 1.0 / long sqrt (LONG 2.0))))
END
</syntaxhighlight>
</lang>
Output:<pre>+1.707106781186547524400844362e +0 +2.928932188134524755991556379e -1
+2.928932188134524755991556379e -1 +1.265697533955921916929670477e -2
Line 269:
 
=={{header|APL}}==
<syntaxhighlight lang="apl">
<lang APL>
agd←{(⍺-⍵)<10*¯8:⍺⋄((⍺+⍵)÷2)∇(⍺×⍵)*÷2}
1 agd ÷2*÷2
</syntaxhighlight>
</lang>
Output: <pre>0.8472130848</pre>
 
Line 278:
By functional composition:
 
<langsyntaxhighlight AppleScriptlang="applescript">-- ARITHMETIC GEOMETRIC MEAN -------------------------------------------------
 
property tolerance : 1.0E-5
Line 336:
end script
end if
end mReturn</langsyntaxhighlight>
{{Out}}
<pre>0.847213084835</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">agm: function [a,g][
delta: 1e-15
[aNew, aOld, gOld]: @[0, a, g]
 
while [delta < abs aOld - gOld][
aNew: 0.5 * aOld + gOld
gOld: sqrt aOld * gOld
aOld: aNew
]
return aOld
]
 
print agm 1.0 1.0/sqrt 2.0</lang>
 
{{out}}
 
<pre>0.8472130847939792</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AHKlang="ahk">agm(a, g, tolerance=1.0e-15){
While abs(a-g) > tolerance
{
Line 351 ⟶ 371:
}
SetFormat, FloatFast, 0.15
MsgBox % agm(1, 1/sqrt(2))</langsyntaxhighlight>
Output:
<pre>0.847213084793979</pre>
 
=={{header|AWK}}==
<langsyntaxhighlight AWKlang="awk">#!/usr/bin/awk -f
BEGIN {
printf "%.16g\n", agm(1.0,sqrt(0.5))
Line 372 ⟶ 392:
return (x<0 ? -x : x)
}
</syntaxhighlight>
</lang>
Output
<pre>0.8472130847939792</pre>
 
=={{header|BASIC}}==
==={{header|ANSI BASIC}}===
{{works with|QBasicDecimal BASIC}}
<syntaxhighlight lang="basic">100 PROGRAM ArithmeticGeometricMean
<lang qbasic>PRINT AGM(1, 1 / SQR(2))
110 FUNCTION AGM (A, G)
END
120 DO
 
130 LET TA = (A + G) / 2
FUNCTION AGM (a, g)
140 LET G = SQR(A * G)
DO
150 LET taTmp = (a + g) / 2A
160 LET gA = SQR(a * g)TA
170 LET SWAPTA a,= taTmp
180 LOOP UNTIL aA = taTA
190 LET AGM = A
200 END FUNCTION
AGM = a
210 REM ********************
END FUNCTION</lang>
220 PRINT AGM(1, 1 / SQR(2))
230 END</syntaxhighlight>
{{out}}
<pre> .84721308479398 </pre>
<pre>
 
.8472131
==={{header|Applesoft BASIC}}===
</pre>
Same code as [[#Commodore_BASIC|Commodore BASIC]]
The [[#BASIC|BASIC]] solution works without any changes.
 
==={{header|BASIC256}}===
<langsyntaxhighlight BASIC256lang="basic256">print AGM(1, 1 / sqr(2))
end
 
Line 410 ⟶ 434:
 
return a
end function</langsyntaxhighlight>
{{out}}
<pre>0.84721308479</pre>
0.84721308479
</pre>
 
==={{header|Commodore BASIC}}===
<lang commodorebasic>10 A = 1
20 G = 1/SQR(2)
30 GOSUB 100
40 PRINT A
50 END
100 TA = A
110 A = (A+G)/2
120 G = SQR(TA*G)
130 IF A<TA THEN 100
140 RETURN</lang>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> *FLOAT 64
@% = &1010
PRINT FNagm(1, 1/SQR(2))
Line 443 ⟶ 453:
UNTIL a = ta
= a
</syntaxhighlight>
</lang>
{{out}}
Produces this output:
<pre>0.8472130847939792</pre>
<pre>
 
0.8472130847939792
==={{header|Chipmunk Basic}}===
</pre>
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">10 print agm(1,1/sqr(2))
20 end
100 sub agm(a,g)
110 do
120 let ta = (a+g)/2
130 let g = sqr(a*g)
140 let x = a
150 let a = ta
160 let ta = x
170 loop until a = ta
180 agm = a
190 end sub</syntaxhighlight>
{{out}}
<pre>0.847213</pre>
 
==={{header|Commodore BASIC}}===
<syntaxhighlight lang="commodorebasic">10 A = 1
20 G = 1/SQR(2)
30 GOSUB 100
40 PRINT A
50 END
100 TA = A
110 A = (A+G)/2
120 G = SQR(TA*G)
130 IF A<TA THEN 100
140 RETURN</syntaxhighlight>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">let a = 1
let g = 1 / sqrt(2)
 
do
 
let t = (a + g) / 2
let g = sqrt(a * g)
let x = a
let a = t
let t = x
 
loopuntil a = t
 
print a</syntaxhighlight>
{{out| Output}}
<pre>0.85</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' version 16-09-2015
' compile with: fbc -s console
 
Function agm(a As Double, g As Double) As Double
Dim As Double t_a
Do
t_a = (a + g) / 2
g = Sqr(a * g)
Swap a, t_a
Loop Until a = t_a
Return a
End Function
 
' ------=< MAIN >=------
 
Print agm(1, 1 / Sqr(2) )
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre> 0.8472130847939792</pre>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Public Sub Main()
Print AGM(1, 1 / Sqr(2))
 
End
 
Function AGM(a As Float, g As Float) As Float
 
Dim t_a As Float
Do
t_a = (a + g) / 2
g = Sqr(a * g)
Swap a, t_a
Loop Until a = t_a
Return a
 
End Function</syntaxhighlight>
 
==={{header|GW-BASIC}}===
<langsyntaxhighlight lang="gwbasic">10 A = 1
20 G = 1!/SQR(2!)
30 FOR I=1 TO 20 'twenty iterations is plenty
Line 457 ⟶ 564:
60 A = B
70 NEXT I
80 PRINT A</langsyntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 PRINT AGM(1,1/SQR(2))
110 DEF AGM(A,G)
120 DO
Line 467 ⟶ 574:
150 LOOP UNTIL A=TA
160 LET AGM=A
170 END DEF</langsyntaxhighlight>
 
==={{header|Liberty BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="lb">
print agm(1, 1/sqr(2))
print using("#.#################",agm(1, 1/sqr(2)))
end
 
function agm(a,g)
do
absdiff = abs(a-g)
an=(a+g)/2
gn=sqr(a*g)
a=an
g=gn
loop while abs(an-gn)< absdiff
agm = a
end function
</syntaxhighlight>
{{out}}
<pre>0.84721308
0.84721308479397904</pre>
 
==={{header|Minimal BASIC}}===
{{trans|Commodore BASIC}}
{{works with|IS-BASIC}}
<syntaxhighlight lang="qbasic">10 LET A = 1
20 LET G = 1 / SQR(2)
30 GOSUB 60
40 PRINT A
50 STOP
60 LET T = A
70 LET A = (A + G) / 2
80 LET G = SQR(T * G)
90 IF A < T THEN 60
100 RETURN
110 END</syntaxhighlight>
{{out}}
<pre> .84721308</pre>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#Commodore BASIC|Commodore BASIC]] solution works without any changes.
 
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure.d AGM(a.d, g.d, ErrLim.d=1e-15)
Protected.d ta=a+1, tg
While ta <> a
ta=a: tg=g
a=(ta+tg)*0.5
g=Sqr(ta*tg)
Wend
ProcedureReturn a
EndProcedure
 
If OpenConsole()
PrintN(StrD(AGM(1, 1/Sqr(2)), 16))
Input()
CloseConsole()
EndIf</syntaxhighlight>
{{out}}
<pre> 0.8472130847939792</pre>
 
==={{header|QuickBASIC}}===
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">PRINT AGM(1, 1 / SQR(2))
END
 
FUNCTION AGM (a, g)
DO
ta = (a + g) / 2
g = SQR(a * g)
SWAP a, ta
LOOP UNTIL a = ta
AGM = a
END FUNCTION</syntaxhighlight>
{{out}}
<pre>.8472131</pre>
 
==={{header|Quite BASIC}}===
{{trans|Commodore BASIC}}
<syntaxhighlight lang="qbasic">10 LET A = 1
20 LET G = 1 / SQR(2)
30 GOSUB 100
40 PRINT A
50 END
100 LET T = A
110 LET A = (A + G) / 2
120 LET G = SQR(T * G)
130 IF A < T THEN 100
140 RETURN</syntaxhighlight>
{{out}}
<pre>0.8472130847939792</pre>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">print agm(1, 1/sqr(2))
print agm(1,1/2^.5)
print using("#.############################",agm(1, 1/sqr(2)))
 
function agm(agm,g)
while agm
an = (agm + g)/2
gn = sqr(agm*g)
if abs(agm-g) <= abs(an-gn) then exit while
agm = an
g = gn
wend
end function</syntaxhighlight>{{out}}
<pre>0.847213085
0.847213085
0.8472130847939791165772005376</pre>
 
==={{header|Sinclair ZX81 BASIC}}===
{{trans|COBOL}}
Works with 1k of RAM.
 
The specification calls for a function. Sadly that is not available to us, so this program uses a subroutine: pass the arguments in the global variables <tt>A</tt> and <tt>G</tt>, and the result will be returned in <tt>AGM</tt>. The performance is quite acceptable. Note that the subroutine clobbers <tt>A</tt> and <tt>G</tt>, so you should save them if you want to use them again.
 
Better precision than this is not easily obtainable on the ZX81, unfortunately.
<syntaxhighlight lang="basic"> 10 LET A=1
20 LET G=1/SQR 2
30 GOSUB 100
40 PRINT AGM
50 STOP
100 LET A0=A
110 LET A=(A+G)/2
120 LET G=SQR (A0*G)
130 IF ABS(A-G)>.00000001 THEN GOTO 100
140 LET AGM=A
150 RETURN</syntaxhighlight>
{{out}}
<pre>0.84721309</pre>
 
==={{header|TI-83 BASIC}}===
<syntaxhighlight lang="ti83b">1→A:1/sqrt(2)→G
While abs(A-G)>e-15
(A+G)/2→B
sqrt(AG)→G:B→A
End
A</syntaxhighlight>
{{out}}
<pre>.8472130848</pre>
 
==={{header|True BASIC}}===
{{works with|QBasic}}
<langsyntaxhighlight lang="qbasic">FUNCTION AGM (a, g)
DO
LET ta = (a + g) / 2
Line 482 ⟶ 734:
LET AGM = a
END FUNCTION
 
 
PRINT AGM(1, 1 / SQR(2))
END</langsyntaxhighlight>
{{out}}
<pre>.84721308</pre>
 
.84721308
==={{header|VBA}}===
</pre>
<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
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub</syntaxhighlight>{{out}}
<pre> 0,853553390593274
0,847224902923494
0,847213084835193
0,847213084793979
0,847213084793979 </pre>
 
==={{header|VBScript}}===
{{trans|BBC BASIC}}
<syntaxhighlight lang="vb">Function agm(a,g)
Do Until a = tmp_a
tmp_a = a
a = (a + g)/2
g = Sqr(tmp_a * g)
Loop
agm = a
End Function
 
WScript.Echo agm(1,1/Sqr(2))</syntaxhighlight>
{{Out}}
<pre>0.847213084793979</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="vb">print AGM(1, 1 / sqrt(2))
end
 
sub AGM(a, g)
repeat
ta = (a + g) / 2
g = sqrt(a * g)
x = a
a = ta
ta = x
until a = ta
 
return a
end sub</syntaxhighlight>
{{out}}
<pre>0.847213</pre>
 
 
==={{header|Visual Basic .NET}}===
{{trans|C#}}
====Double, Decimal Versions====
<syntaxhighlight lang="vbnet">Imports System.Math
Imports System.Console
 
Module Module1
 
Function CalcAGM(ByVal a As Double, ByVal b As Double) As Double
Dim c As Double, d As Double = 0, ld As Double = 1
While ld <> d : c = a : a = (a + b) / 2 : b = Sqrt(c * b)
ld = d : d = a - b : End While : Return b
End Function
 
Function DecSqRoot(ByVal v As Decimal) As Decimal
Dim r As Decimal = CDec(Sqrt(CDbl(v))), t As Decimal = 0, d As Decimal = 0, ld As Decimal = 1
While ld <> d : t = v / r : r = (r + t) / 2
ld = d : d = t - r : End While : Return t
End Function
 
Function CalcAGM(ByVal a As Decimal, ByVal b As Decimal) As Decimal
Dim c As Decimal, d As Decimal = 0, ld As Decimal = 1
While ld <> d : c = a : a = (a + b) / 2 : b = DecSqRoot(c * b)
ld = d : d = a - b : End While : Return b
End Function
 
Sub Main(ByVal args As String())
WriteLine("Double result: {0}", CalcAGM(1.0, DecSqRoot(0.5)))
WriteLine("Decimal result: {0}", CalcAGM(1D, DecSqRoot(0.5D)))
If System.Diagnostics.Debugger.IsAttached Then ReadKey()
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre>Double result: 0.847213084793979
Decimal result: 0.8472130847939790866064991235</pre>
 
====System.Numerics====
{{trans|C#}}
{{Libheader|System.Numerics}}
<syntaxhighlight lang="vbnet">Imports System.Math
Imports System.Console
Imports BI = System.Numerics.BigInteger
Module Module1
Function BIP(ByVal leadDig As Char, ByVal numDigs As Integer) As BI
BIP = BI.Parse(leadDig & New String("0"c, numDigs))
End Function
Function IntSqRoot(ByVal v As BI, ByVal res As BI) As BI ' res is the initial guess of the square root
Dim d As BI = 0, dl As BI = 1
While dl <> d : IntSqRoot = v / res : res = (res + IntSqRoot) / 2
dl = d : d = IntSqRoot - res : End While
End Function
Function CalcByAGM(ByVal digits As Integer) As BI
Dim a As BI = BIP("1"c, digits), ' value is 1, extended to required number of digits
c as BI, ' a temporary variable for swapping a and b
diff As BI = 0, ldiff As BI = 1 ' difference of a and b, last difference
CalcByAGM = BI.Parse(String.Format("{0:0.00000000000000000}", ' initial value of square root of 0.5
Sqrt(0.5)).Substring(2) & New String("0"c, digits - 17))
CalcByAGM = IntSqRoot(BIP("5"c, (digits << 1) - 1), CalcByAGM) ' value is now the square root of 0.5
While ldiff <> diff : c = a : a = (a + CalcByAGM) >> 1 : CalcByAGM = IntSqRoot(c * CalcByAGM, a)
ldiff = diff : diff = a - CalcByAGM : End While
End Function
Sub Main(ByVal args As String())
Dim digits As Integer = 25000
If args.Length > 0 Then Integer.TryParse(args(0), digits) : _
If digits < 1 OrElse digits > 999999 Then digits = 25000
WriteLine("0.{0}", CalcByAGM(digits))
If System.Diagnostics.Debugger.IsAttached Then ReadKey()
End Sub
End Module</syntaxhighlight>
{{out}}
<pre style="height:64ex; overflow:scroll; white-space: pre-wrap;">0.8472130847939790866064991234821916364814459103269421850605793726597340048341347597232002939946112299421222856252334109630979626658308710596997136359833842511763268142890603897067686016166500482811887218977133094117674620199443929629021672891944995072316778973468639476066710579805578521731403493983042004221192160398395535950981936412937163406460295999679705994343516020318426487569502421748638554059819545816017424178878541927588041627190120855876856483268341404312184008040358092045594943138778151209265222545743971242868207663409547336745996217926655353486256861185433086262872872875630108355631935706687147856390889821151088363521476969796126218329432284178681137684451700181460219136940270209459966835135963278808042743454817445873632200251539529362658066141983656164916262596074347237066169023530800173753128478525584306319074542749341526857906552694060031475910203327467196861247963255105546489028208552974396512499400966255286606758044873538921857014011677169765350140849524768489932573213370289846689391946658618737529663875622660459147770442046810892565844083803204091061900315370673411959410100747433105990550582052432600995169279241747821697678106168369771411073927334392155014302200708736736596227214925877619285105238036702689046390962190766364423553808590294523406519001334234510583834171218051425500392370111132541114461262890625413355052664365359582455215629339751825147065013464104705697935568130660632937334503871097709729487591717901581732028157828848714993134081549334236779704471278593761859508514667736455467920161593422399714298407078888227903265675159652843581779572728480835648996350440414073422611018338354697596266333042208499985230074270393027724347497971797326455254654301983169496846109869074390506801376611925291977093844129970701588949316666116199459226501131118396635250253056164643158720845452298877547517727274765672164898291823923889520720764283971088470596035692199292183190154814128076659269829446445714923966632997307581390495762243896242317520950731901842446244237098642728114951118082282605386248461767518014098312749725765198375649235690280021617490553142720815343954059556357637112728165705973733744297003905604015638866307222570038923015911237696012158008177907786335124086243107357158376592650454665278733787444483440631024475703968125545398226643035341641303561380163416557526558975294452116687345122019122746673319157124076375382110696814107692639007483317574339675231966033086497357138387419609898383220288269488219130281936694995442224069727616862136951165783888501219909616065545461154325314816424933269479700415949147632311292059351651899794335004597628821729262591808940550843146639378254833513955019065337087206206402407705607584879649984365159272826453442863661541914258577710675618501727803328717519518930503180550524542602233552290077141812879865435118791800635627959362476826778641224946033812608262825409889531252767753465624327921451122955551603181843313369296172304178385515712556740498341666592696958000895372457305769454227537216020968719147039887846636724326270619112707171659082464004167994112040565710364083000241929439855307399465653967781049270105541035951333943219992506667620207839469555376055179640100974921885631130101781388857879381317209594806253920130098365028791769582798590527994772194179799702494306215841946888532811549772157996019440962347768614408507573928429882375939682322367058033413477462311289762585932437663177897491107726190970448952220450963072551559009382490402136480779203476721504856844602255440999282616317431264228578762898338065072202301037175314926350463106018857377256700661838129058063895450812703131137104371613583348806583395543121790134839883321641305763524471251153947206667033010134871651632411382881763983962952612114126321979596509865678675525076076042409590751752302194610453256433324961490125353332922372386894812788502013596630537605584935892839163046940388785496002747148719780145765957904958580226006609952496736432496683346176010660815670697514238186650361083885220976165500251607311499216129477579019972924868963822060380876027628167237016681910663358577515465038133423672234764202655856558846416010210540489855618711473588497637840648642679818650448631907747038228671143515112300360708657429886477146674733750114345818852797006056211724692174847180694866251199472893444270378304620707354938052872720621560630718828685805645211106967080285699069825769177220998671959968507790681443494932804976811543680463259938693076235070999518295129581121235707245383354826190752395158273098248180549665897909168867984071707793705959045775840910473413109604194111357756620727337797833203797301137672658535747710279781409721309612142393854737462769615041307952837372882050658719152259765084027796991761175393006725492491229845082362975568722711065849435533850494532638736489804606655979954360169503092790092450057856477235876198848986034412195340795369002996411974549060741600978859537660722905160772428590070901156639138364299041220826769629797867649032356499981990765997439870548648769091024911927099968275697011368762244046402960383700066212734577664709711326374656811502985863032260337383421358423937896114617192083071953915643782093641496780334152464507396683173198363362743392555311712019454146844880895622417898031894341231284027858378289009624209541345002101072736323285272576209646851994468240550629391742053301706461917215178844296705314335503772310709716080285145314144106105023117310877779933248932087727229897821330120834074305604998159963202687793307156940302439156118926767517249511766526248547096041991473113657920697330996088897286789780735587578500623575157123771653042063631002703129296694025421967877168846655727580898306467662007014679585693082220620905330827782226503112520278733512519159918893900284319218166686548434879621972211763904959895793607330943697457628943200384117552941594754747183936381144125610351023459581080768558985657007445308909428669251190101718122826689349269528261052518556736045877702288147821446968500918347219741420546128072347950059811766364526150190788545471193803557145930744635656260752787518824386409506964649815131170591457990619376560858650175616864501924098327235724333688813080022186368700209641119724303603558649793773314916749593151188673535025505982303047060284740458456676849620934506396302909441632516408692889814507247877727673378033828929504978384342943766566737297587430575141036417476861639624198941904730996100228428079444920026904845254139188246001559089131943255610365769362364161784646693141456109984038312265504115251494445380042090428718182468431624610552637677520970104063944687837375017436089751693486887651283453677552786547090231542029453873076141196649767521919808902105772633472397958968722923357769041244458682297806209887089816018179521454920370956252850733023255060096611329479148443416687429872654204083552056456404421174124065041932362831296643126330768715450444950733554418200793669701331244638824360062439816712409346806322169771701563590417609841261977801052586956634654144702511135382841010278579543061802357275500930513955637771043922799597114118278203358118398952338720119626666828781215343331193353019800652511924103594315072427251589774226901431325149775220621148653209528291784172678852791825950189428306645453380829438548491390660090152646315666940813051689857738445716110134773528439558663918031477128997248977232695083095920860316390860179422146804892537147135669490647597566350405076105930300153453613446834614136284840473063909580064862482211399539962122107992774053203059756987131501429238941821989218445861496845306346078287058864262560349767113385390753047360747520569725532663517964059488138127648519130232826129551720747594498863925111049785977410104647258831744969489273332281068408949475978706769012216951869658194406136694310323411619613160554381608728305543504819071159752742665917363693001980988797627218662628543311906086034280619151845297823703639898449414417889008602782220998390227472837967411429578924346545640402855167478372538831386154780508035236893583332887355879794886804980971406868936719416711504307402575102269081707385928535837390976424975922421061832372517021428320986753744507133218963666908565634963306077455683011837149400258404997766113525532847665618870592978212729899729592794781820428719807102278646183807006401083138975677112754136221127444534535584959769252575758312999039536959893249951324106784265611556743660088737484274038234811784911002123537108015334407708175281579422928548731689863980071896268684985779061942582000173178473797975815609269087287850270024414741281953578873964745859459899535543412801653553049058528794674398220606230386688852700505218904927782197514115595435549125326115087432280435609563176116321811794164884206928474315699133677787956913705592704959893911100786224112449931719539890308215307126971807352814294437374058180589784287101566325873726600012296180403780429093175160473979931236882466314524590792512088916974765430245705320638670468411054034201437664442213212750799846299157010147106552946146746392249574530619682203425444816247545977269653430250686824205288099692448923652171403817749282935917315481284919621433304080904306867233682060716291289398517406255904282247558159509102324206160816363511440953267967974466214658121897383725705201831800678505181233270743236051760236565304605919728246762046497950757124332306210615236617229324468286251110577832854712371857906482302429199129753477340618812393224405123793229248698239302094605799468502209356458018864737205798950819968285087908120645175464792846657029993496146354533816989879012073959534299458051884682918835631136138879631316173442207506218212945047503433730640140356614106403320867621443183928438969994268286836082535591242751488383392264668222963323657488981599104902374571278077062853236895690028469742954774248422335523859049299225453318270693966088603518491166875108552006265340966412611220069290556369052744064893640087015171662929356529921474420793873710647399136453402185931518201576110059405556600166318190916348212818643068418256991194316266715898588673650488980580832972145195811525832974358064432698289209364284959616975339927502383832695801109608954786457256109785378297307074918168744735731189049849490781632210127110919398357638892753131749978321368280932894349330930087868884127092076359007648065118301317440813138170776478562086983456849957696333241556699085937149528437303782174166781012624737754844959408277598042857813775448446192929537153359741871355556678028606484917974827559022377376189703770332489774349235376523557139076431488967144133099539679871046284747721772185865851985971282165739148574494328320308464163956096301047370473988450307936956928683464113764226308568695688152053749196294562881085987015910764955019272667378276517237450013662421051146709184898952269727656206976263055094938932099216377529415335060027109430018977339221845390337351007942764665232509045377940478212355620488638969640291029182673024368888013982750049655688955540362739754118359277009094291839958396298535952123465573707751680432023872401008786292362558484920221296055948232317635214207117650427699747801290249150914873347204981208353486521246233538858471700470120592394582541522312967601307268280232044633644234100026474341568399123881048049819491200940244895720301881220640996997340843736095812449945913231793359333819197360248853375641030435643732302001328359990615298394916710687997693926699033522064083729586994304357670917169796698442332656830732550000321312902706719106342428311390049478179307304556219943912072209495471916547109605404919944186051724981471812994063119290173738101176617356976495636675620278895592099504686163440305250658681735840269428736633431167832903837475658050990783985384926064721246565130660487673608585790218386643241627198210378772796337736742692945663985470529377745854692207002046330357343505517537014050310355526578082729897049230547545589009275410944504014157125357682801074915174627928533783099570631952876838237806368177841661186334747789420166190186143388804514884174361681454810362321037643274595653364629397295294049952661691181657740018116146497654407589150912557599100855273107733703213603505619407350405223414533224306604743600257212590127202517146952605462439215815151732661454812243619860357386922465403688559787750083268386930674253759349376972691382532780570135683441862315010318955128705494038594760949278590520009881447715839714713971813720554960331191642239195313230213875992717401904622413925914800620171561815889352945121978193704745708538695427900233080410588007250947512318930796844637224171170594606197614751977323896101315556406372309310279476973938229476346893933755946893665094049910252612163538072005644241026471164639800490998535570282059396054554479255558624918709232180130454102936332893619326596350851413637207293142767763267817840066780089558654877782630822818446508158509625695020697797889664140551101421185533444015948880284701657904464926309216120238068566472631611326995533585414320547442896728173291714010643730593960222482733969720865809194288803963344344876467583385597351333330628439786357062196382217705500672607607570202305548328439335937369624085404957344415141889143812206076832329063384332685935928226648361622876815670931303789678327741487845287838232474038340893449427806045589018183673133602271167285304427194507315740913600066356089181219040305019319028163972135790696025211929562455952835850442627787993214468221041325612271290302469610374855134599106662606082143546126463790846952338680559237822828610361386416013753920426888371192602742087474507782730180882648297991489233434653363930327991816476995529468892904060335470265188317825821391915073117022336839564945335630414192442838503954209073337511117053790819768061378846157004292392264788138228486672543415580694421193506836000488465561599083339184724263183698928130695654949153165010313216361224018298711517222401523368101476246169896417259748838727189598765602350324828709741468793415378708814573190327920453219231685852735108372055942456601545647944675449566859142997988233179819059574125368681032194798082603876241044848730208905065871934264174092007936669883601462309762759844113071525758916288010581709353072588887654386253201848624931923638568216562603110434528313030704972291334873033240933736956347974889824930017415805659182123288343858101250171537305398462043432455721482088547523494730467761429282915391485852688505423074450548192619166975975031503447208211845313907683486006908772752077246485706597636740936173143436990399498908375710246545650814962015988805204483379491707040848303909417512426275869868668644293498242419667403627076032399201407183071270759837132000712447159523642782162488472933913713634046138974088894178399320090051543608421618891328957740354384456107645016010462709579098652495342014766016330458293537653454523438667413798731255017029554582809547897542497367109038598264606895622241257303208140890607025206140457815282368504505765710043804228592032720729190222134651835930255942940875306994701101153416476785623543575023993736414532895773499876167502240919794121893188059017977444329403624038551082491954751841177014150820554999148803286500065069030165028455616533514890711974194172310029663247936640825364542104897640445108081123906368188594908660418340025631562661211506365309297219580687177632051461355581309500814563826112416521487163593643553646268872746276680368630680088231249970572706496265335285424273723449757482776061300818063419639083097882249478922949525891665782610044424440110326748539620120023397129834624242363283711074267309902126029110038109050751840523266273905031934856015485510632624318778970878895198168073096354223096005536267735905099473408744371024816727970009494589707630185344952680106730984246828848883760016695887137355969244555238536396178788134209309376484848406842940499731494663578455826688245825356635393289729316700066238128368519670627697889769929009597838069557440769080950069594659578325366066060213000525012998145215099629307110700615796004759918829827472751877492472674770755413679265775060149528336859838085353420874215682758801259992855903410097963019943741001394975591822918846705741010634931594527954742032057295356596869586863097328488381174243827058441735659667485315202886191192125286398739560928127513223214119754229343092375569339614672740517569529376699061052365448344078610425576694541873486379356070861240473688356773437140126350120823765176390562050604076894729400293162079760342896846897639867830553941515230713725560502914671175123451932131962571791940911728951123948113598860588062424037835751996487088330150679210175429060531418836978611027896830689666851868410470182364780700615529883149883111601949965815038674390467105247175993726709203381051984777006122752302698038537619917731907133105816779008651480172440446403764720673784583395382889380902941273987910475254258486561698048543296782281040453997661165123290729161619992628751086519341731116513305659182981762584769428708454819029344222186027977405519291266188948708010515922860149238393490889782166965109499761673179583522105791358724355029782111425280584380959770472177893827382916471882671437865821461326011263516554280516418422188264141890686619186492751718984735037496602686033671961304915922609442146773092074476794711917820209913226872184947548378003848726148872742881265579174794634151444545105599464567614478293387968015412886418098284885525959617399177657635267081989985408930744564199296902459275405143647525648661932959903068323866757518479741015342911416508753572892479684280248440220211898390243430190746592470563991910024225814399068391457857458095344096826158489731615822039837691005171654390590093326827586419753439483771905973079465029210363641972615923872187876095687197681934481955852567024141433671590889694204781798936556351775101591005026585947279448642317311892727153525046034081896227383114600546852406398855471859684088277722162250586368419379964112646321070639818773794369650252104438622320671517228411475433482803041707675438555447584321271846396281391925884972509051040944134450429845346071848875654240709690138592611645519676563708429710676494635766201285381926791204110977805857352062737510466943591592074904378966129808716274322385039032007477854211063899544954185997641428116395197239708078986048758264126544825149923227286176571389697334537835963603962709038002668921324389159009375225033651171937770657226295341257068980907793198879997076783263303670667342657925395849950582363998610492878479976185891384024744790742355981796013254960652684988733518397287191251899388324341602608356164496670902390042273216221931567939944001215159910054381084520081133103207553492484487369268314444466610780275891777468369344585045949963237156043800258227618908603074550819931892899703285549507330240121766349515315827830897786432254556221744305752825143708087184314470811004510108612122699931396969361066523608721126359012344828262284427191281973187269761974740398071778378188160519801862257232970224762494767912932684020188061795236229174601398576604233579094407723017353015337974435643738584248250538061547193075224429309117207447677149522141919390974201716026970557825836923707297811545552570788004955666915477901830719591663516687057984336951611189153751912396714116378197000784953115386326766369269172016978409040396969804861828436417776804088449208439901095951205751340861060375353408155737087188313898337656322533650946010308686111901241541794900659835366926383515058402026098259570385429145865025692157987309807064597082326377138235585737704225628144262793497769429358804020882742028263786443615935817930817858306265712263479452174065216410798029333573961137404301928294367884626832432449078812684787281988676202931062510264948586549463964789154366240635570346688477784815271412470430646040615614277320107003575855033995279377529716156628381118518085523414187577256025217995103662771477552291036839539792329375184700131215428652464111526297830742328651189481978920924682746392250346179819781021313400022272303222234731521016033826145645816472110340883197207109422849637006090510260943044730126801795349152894613046101033061811314821366141874985466628809585678299308824993966655499624380015821082410781190328189506855057581990908848597095494573176672201417764187253816862426293852974092626551536758155537683368451820154793964862810533857810979434793077956125541240828563089647076354827276586047900779183041806574320855302776686899978897939486987950729652971448050889517660684386673056662911929857913206598752762097197279390208473846210277152094212386266930256260451209117402079233658157593274696841906354187366092529138116574357045728290417433832596884391356956442617823006949118156994294295529170211353842468704890572313005646106202029653246628477843902025194715815133791174898257040115532858624973690714844800747184719290671002133191274834310662201874141841328708920709275866745037664169280121112867057832132585948539987132879098472640550013972043153470930436509718084070853723316111111611632600262171748813737621046013600544051850633175245231989785291065646466038278748870331134307620041356514295482843502245454400571392386492526283423907951705366640483826875013469850263767974528926285288366544314868036628329638912254207094687335597669512007687507292940623176435604796651807847095408991068514998003358735387989422028901542800717906482276185298683079286137204396993726503610285463352157718364571843381950031926272352293654343387522809514152498052577486366048613580539162662183475105825647260311633442002377527140625112075332294909525522330744664115572260242435895269482927435844022622001466247093866533879048392320516224276433339282642640953964341822416705658461244760448817737705782669080880834418822622611342632727419248415651121035047131961583094994438779439078380664656207143187309895280874153167621657602227990850199615587578332393883365169478142077533262283694526612005465820771400826060398839255150948861553177333447506822679211849690448880479070102043288205874672361672971246062341973369704807867768609989464712379097525706498042381815865399434983035941162258347729020489356838477197804973214911448748749915616679253857438010864500220134843719609727912761136925035123155282535741655826107266099467657016111855684257826878422197833994329148734893923892153298966294232703135845615804723993624827409373966761563257981994036006655039613941881183164267144485664874468348587099434743710128859267552473831462181434321232124758618476925803128913233878664527525204324484796532776273320171351979849530142473805976430318655810403609897537469226336015596525652284888167037460054235043655813438329870872734142062859147847007274999414885129441657918212383876056572545671794085637289277002790218604788423519924573051811976377731594412994393860534559159658127123862955315918182841923881357245009246238507097741891437575676886206936433608263660374355173185026954239766173038826275043838965247160428689739548061640664606565379050539422795708801840829664956978192406737307076253014257542221763860230431809477056758905681723033326311408802886092880151777469082375063137750925275331638009836786645991949881018108222446858443984865972449621097999331605268587810061927125889694400669979755648800940895626242917531834388920035663113368763931463847812763130237825562198311791061780856687903309789539747505239545316630638169559777653347655949908779202359718666623572487055558216484036084925217803431104356647417600193631613474196113126657206064282217690428541246560204561459484317744683213906021267727411189443675804442911583757423572500214191467493342871160840582639470485636370375679604797073490813681083838562113841391587052553615073991983125473434527404596547926972539542447555990332809716643578039646945749813368621152410490288581779206318208255069166455507840899628333174744873951607229399258854694188637978240144635295264982572856632103053550891057171748674115218494774077589151115819489068851971959768129214023511454382738867557288320426608338030759515727545577639726238470674634011626346953231815229549718996906470438903536574430644436472716449550868519871817092814068746449470806856174570885105064766494332205391085097539987897980672278869943134632799032372604933150163386774039430519493297142505321117669011820293604482694166301309801111227443654953271242388534939973277749999335296667138307969441135719079969506099821923206878892624416110175909254904610286553512032488285673735148429324009831633211264460376172046209384270528903772251057643968938983722779640468452705694321085455273829462711022737243290606294601651732654594463569861350966095209962038508010899673666470073918705760679801337058347046567503369379598928154437380765511031719081985901371088639600700705631873099251480947989238619052479230983309717938226245725600119571130722386790431255742179135633111146646083268382596762356018472772209198013121983224179079476134977421748168833934278876403014334318798493417716613256506422668264638388429786875443810986754386459491846082078633346046469418429778813833857755519670005669840456587642130852057050148314568259387702428619224671173187370822224627538313365937868201435535126600146246249435880806572693573084485615073901842761167215162204840459913839674251648</pre>
 
==={{header|ZX Spectrum Basic}}===
{{trans|ERRE}}
<syntaxhighlight lang="zxbasic">10 LET a=1: LET g=1/SQR 2
20 LET ta=a
30 LET a=(a+g)/2
40 LET g=SQR (ta*g)
50 IF a<ta THEN GO TO 20
60 PRINT a
</syntaxhighlight>
{{out}}
<pre>0.84721309</pre>
 
=={{header|bc}}==
<langsyntaxhighlight lang="bc">/* Calculate the arithmethic-geometric mean of two positive
* numbers x and y.
* Result will have d digits after the decimal point.
Line 516 ⟶ 909:
 
scale = 20
m(1, 1 / sqrt(2), 20)</langsyntaxhighlight>
 
{{Out}}
Line 522 ⟶ 915:
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">AGM ← {
(|𝕨-𝕩) ≤ 1e¯15? 𝕨;
(0.5×𝕨+𝕩) 𝕊 √𝕨×𝕩
}
 
1 AGM 1÷√2</langsyntaxhighlight>
{{out}}
<pre>0.8472130847939792</pre>
Line 533 ⟶ 926:
=={{header|C}}==
===Basic===
<langsyntaxhighlight lang="c">#include<math.h>
#include<stdio.h>
#include<stdlib.h>
Line 565 ⟶ 958:
return 0;
}
</syntaxhighlight>
</lang>
 
Original output:
Line 579 ⟶ 972:
 
===GMP===
<langsyntaxhighlight lang="cpp">/*Arithmetic Geometric Mean of 1 and 1/sqrt(2)
 
Nigel_Galloway
Line 612 ⟶ 1,005:
 
return 0;
}</langsyntaxhighlight>
 
The first couple of iterations produces:
Line 628 ⟶ 1,021:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">namespace RosettaCode.ArithmeticGeometricMean
{
using System;
Line 705 ⟶ 1,098:
}
}
}</langsyntaxhighlight>
Output:
<pre>0.847213084835193</pre>
Note that the last 5 digits are spurious, as ''maximumRelativeDifference'' was only specified to be 1e-5. Using 1e-11 instead will give the result 0.847213084793979, which is as far as ''double'' can take it.
===Using Decimal Type===
<langsyntaxhighlight lang="csharp">using System;
 
class Program {
Line 727 ⟶ 1,120:
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
}
}</langsyntaxhighlight>
{{Out}}
<pre>0.8472130847939790866064991235</pre>
Line 734 ⟶ 1,127:
{{Libheader|System.Numerics}}
Even though the System.Numerics library directly supports only '''BigInteger''' (and not big rationals or big floating point numbers), it can be coerced into making this calculation. One just has to keep track of the decimal place and multiply by a very large constant.
<langsyntaxhighlight lang="csharp">using static System.Math;
using static System.Console;
using BI = System.Numerics.BigInteger;
Line 763 ⟶ 1,156:
WriteLine("0.{0}", CalcByAGM(digits));
if (System.Diagnostics.Debugger.IsAttached) ReadKey(); }
}</langsyntaxhighlight>
{{out}}
<pre style="height:64ex; overflow:scroll; white-space:pre-wrap;">0.8472130847939790866064991234821916364814459103269421850605793726597340048341347597232002939946112299421222856252334109630979626658308710596997136359833842511763268142890603897067686016166500482811887218977133094117674620199443929629021672891944995072316778973468639476066710579805578521731403493983042004221192160398395535950981936412937163406460295999679705994343516020318426487569502421748638554059819545816017424178878541927588041627190120855876856483268341404312184008040358092045594943138778151209265222545743971242868207663409547336745996217926655353486256861185433086262872872875630108355631935706687147856390889821151088363521476969796126218329432284178681137684451700181460219136940270209459966835135963278808042743454817445873632200251539529362658066141983656164916262596074347237066169023530800173753128478525584306319074542749341526857906552694060031475910203327467196861247963255105546489028208552974396512499400966255286606758044873538921857014011677169765350140849524768489932573213370289846689391946658618737529663875622660459147770442046810892565844083803204091061900315370673411959410100747433105990550582052432600995169279241747821697678106168369771411073927334392155014302200708736736596227214925877619285105238036702689046390962190766364423553808590294523406519001334234510583834171218051425500392370111132541114461262890625413355052664365359582455215629339751825147065013464104705697935568130660632937334503871097709729487591717901581732028157828848714993134081549334236779704471278593761859508514667736455467920161593422399714298407078888227903265675159652843581779572728480835648996350440414073422611018338354697596266333042208499985230074270393027724347497971797326455254654301983169496846109869074390506801376611925291977093844129970701588949316666116199459226501131118396635250253056164643158720845452298877547517727274765672164898291823923889520720764283971088470596035692199292183190154814128076659269829446445714923966632997307581390495762243896242317520950731901842446244237098642728114951118082282605386248461767518014098312749725765198375649235690280021617490553142720815343954059556357637112728165705973733744297003905604015638866307222570038923015911237696012158008177907786335124086243107357158376592650454665278733787444483440631024475703968125545398226643035341641303561380163416557526558975294452116687345122019122746673319157124076375382110696814107692639007483317574339675231966033086497357138387419609898383220288269488219130281936694995442224069727616862136951165783888501219909616065545461154325314816424933269479700415949147632311292059351651899794335004597628821729262591808940550843146639378254833513955019065337087206206402407705607584879649984365159272826453442863661541914258577710675618501727803328717519518930503180550524542602233552290077141812879865435118791800635627959362476826778641224946033812608262825409889531252767753465624327921451122955551603181843313369296172304178385515712556740498341666592696958000895372457305769454227537216020968719147039887846636724326270619112707171659082464004167994112040565710364083000241929439855307399465653967781049270105541035951333943219992506667620207839469555376055179640100974921885631130101781388857879381317209594806253920130098365028791769582798590527994772194179799702494306215841946888532811549772157996019440962347768614408507573928429882375939682322367058033413477462311289762585932437663177897491107726190970448952220450963072551559009382490402136480779203476721504856844602255440999282616317431264228578762898338065072202301037175314926350463106018857377256700661838129058063895450812703131137104371613583348806583395543121790134839883321641305763524471251153947206667033010134871651632411382881763983962952612114126321979596509865678675525076076042409590751752302194610453256433324961490125353332922372386894812788502013596630537605584935892839163046940388785496002747148719780145765957904958580226006609952496736432496683346176010660815670697514238186650361083885220976165500251607311499216129477579019972924868963822060380876027628167237016681910663358577515465038133423672234764202655856558846416010210540489855618711473588497637840648642679818650448631907747038228671143515112300360708657429886477146674733750114345818852797006056211724692174847180694866251199472893444270378304620707354938052872720621560630718828685805645211106967080285699069825769177220998671959968507790681443494932804976811543680463259938693076235070999518295129581121235707245383354826190752395158273098248180549665897909168867984071707793705959045775840910473413109604194111357756620727337797833203797301137672658535747710279781409721309612142393854737462769615041307952837372882050658719152259765084027796991761175393006725492491229845082362975568722711065849435533850494532638736489804606655979954360169503092790092450057856477235876198848986034412195340795369002996411974549060741600978859537660722905160772428590070901156639138364299041220826769629797867649032356499981990765997439870548648769091024911927099968275697011368762244046402960383700066212734577664709711326374656811502985863032260337383421358423937896114617192083071953915643782093641496780334152464507396683173198363362743392555311712019454146844880895622417898031894341231284027858378289009624209541345002101072736323285272576209646851994468240550629391742053301706461917215178844296705314335503772310709716080285145314144106105023117310877779933248932087727229897821330120834074305604998159963202687793307156940302439156118926767517249511766526248547096041991473113657920697330996088897286789780735587578500623575157123771653042063631002703129296694025421967877168846655727580898306467662007014679585693082220620905330827782226503112520278733512519159918893900284319218166686548434879621972211763904959895793607330943697457628943200384117552941594754747183936381144125610351023459581080768558985657007445308909428669251190101718122826689349269528261052518556736045877702288147821446968500918347219741420546128072347950059811766364526150190788545471193803557145930744635656260752787518824386409506964649815131170591457990619376560858650175616864501924098327235724333688813080022186368700209641119724303603558649793773314916749593151188673535025505982303047060284740458456676849620934506396302909441632516408692889814507247877727673378033828929504978384342943766566737297587430575141036417476861639624198941904730996100228428079444920026904845254139188246001559089131943255610365769362364161784646693141456109984038312265504115251494445380042090428718182468431624610552637677520970104063944687837375017436089751693486887651283453677552786547090231542029453873076141196649767521919808902105772633472397958968722923357769041244458682297806209887089816018179521454920370956252850733023255060096611329479148443416687429872654204083552056456404421174124065041932362831296643126330768715450444950733554418200793669701331244638824360062439816712409346806322169771701563590417609841261977801052586956634654144702511135382841010278579543061802357275500930513955637771043922799597114118278203358118398952338720119626666828781215343331193353019800652511924103594315072427251589774226901431325149775220621148653209528291784172678852791825950189428306645453380829438548491390660090152646315666940813051689857738445716110134773528439558663918031477128997248977232695083095920860316390860179422146804892537147135669490647597566350405076105930300153453613446834614136284840473063909580064862482211399539962122107992774053203059756987131501429238941821989218445861496845306346078287058864262560349767113385390753047360747520569725532663517964059488138127648519130232826129551720747594498863925111049785977410104647258831744969489273332281068408949475978706769012216951869658194406136694310323411619613160554381608728305543504819071159752742665917363693001980988797627218662628543311906086034280619151845297823703639898449414417889008602782220998390227472837967411429578924346545640402855167478372538831386154780508035236893583332887355879794886804980971406868936719416711504307402575102269081707385928535837390976424975922421061832372517021428320986753744507133218963666908565634963306077455683011837149400258404997766113525532847665618870592978212729899729592794781820428719807102278646183807006401083138975677112754136221127444534535584959769252575758312999039536959893249951324106784265611556743660088737484274038234811784911002123537108015334407708175281579422928548731689863980071896268684985779061942582000173178473797975815609269087287850270024414741281953578873964745859459899535543412801653553049058528794674398220606230386688852700505218904927782197514115595435549125326115087432280435609563176116321811794164884206928474315699133677787956913705592704959893911100786224112449931719539890308215307126971807352814294437374058180589784287101566325873726600012296180403780429093175160473979931236882466314524590792512088916974765430245705320638670468411054034201437664442213212750799846299157010147106552946146746392249574530619682203425444816247545977269653430250686824205288099692448923652171403817749282935917315481284919621433304080904306867233682060716291289398517406255904282247558159509102324206160816363511440953267967974466214658121897383725705201831800678505181233270743236051760236565304605919728246762046497950757124332306210615236617229324468286251110577832854712371857906482302429199129753477340618812393224405123793229248698239302094605799468502209356458018864737205798950819968285087908120645175464792846657029993496146354533816989879012073959534299458051884682918835631136138879631316173442207506218212945047503433730640140356614106403320867621443183928438969994268286836082535591242751488383392264668222963323657488981599104902374571278077062853236895690028469742954774248422335523859049299225453318270693966088603518491166875108552006265340966412611220069290556369052744064893640087015171662929356529921474420793873710647399136453402185931518201576110059405556600166318190916348212818643068418256991194316266715898588673650488980580832972145195811525832974358064432698289209364284959616975339927502383832695801109608954786457256109785378297307074918168744735731189049849490781632210127110919398357638892753131749978321368280932894349330930087868884127092076359007648065118301317440813138170776478562086983456849957696333241556699085937149528437303782174166781012624737754844959408277598042857813775448446192929537153359741871355556678028606484917974827559022377376189703770332489774349235376523557139076431488967144133099539679871046284747721772185865851985971282165739148574494328320308464163956096301047370473988450307936956928683464113764226308568695688152053749196294562881085987015910764955019272667378276517237450013662421051146709184898952269727656206976263055094938932099216377529415335060027109430018977339221845390337351007942764665232509045377940478212355620488638969640291029182673024368888013982750049655688955540362739754118359277009094291839958396298535952123465573707751680432023872401008786292362558484920221296055948232317635214207117650427699747801290249150914873347204981208353486521246233538858471700470120592394582541522312967601307268280232044633644234100026474341568399123881048049819491200940244895720301881220640996997340843736095812449945913231793359333819197360248853375641030435643732302001328359990615298394916710687997693926699033522064083729586994304357670917169796698442332656830732550000321312902706719106342428311390049478179307304556219943912072209495471916547109605404919944186051724981471812994063119290173738101176617356976495636675620278895592099504686163440305250658681735840269428736633431167832903837475658050990783985384926064721246565130660487673608585790218386643241627198210378772796337736742692945663985470529377745854692207002046330357343505517537014050310355526578082729897049230547545589009275410944504014157125357682801074915174627928533783099570631952876838237806368177841661186334747789420166190186143388804514884174361681454810362321037643274595653364629397295294049952661691181657740018116146497654407589150912557599100855273107733703213603505619407350405223414533224306604743600257212590127202517146952605462439215815151732661454812243619860357386922465403688559787750083268386930674253759349376972691382532780570135683441862315010318955128705494038594760949278590520009881447715839714713971813720554960331191642239195313230213875992717401904622413925914800620171561815889352945121978193704745708538695427900233080410588007250947512318930796844637224171170594606197614751977323896101315556406372309310279476973938229476346893933755946893665094049910252612163538072005644241026471164639800490998535570282059396054554479255558624918709232180130454102936332893619326596350851413637207293142767763267817840066780089558654877782630822818446508158509625695020697797889664140551101421185533444015948880284701657904464926309216120238068566472631611326995533585414320547442896728173291714010643730593960222482733969720865809194288803963344344876467583385597351333330628439786357062196382217705500672607607570202305548328439335937369624085404957344415141889143812206076832329063384332685935928226648361622876815670931303789678327741487845287838232474038340893449427806045589018183673133602271167285304427194507315740913600066356089181219040305019319028163972135790696025211929562455952835850442627787993214468221041325612271290302469610374855134599106662606082143546126463790846952338680559237822828610361386416013753920426888371192602742087474507782730180882648297991489233434653363930327991816476995529468892904060335470265188317825821391915073117022336839564945335630414192442838503954209073337511117053790819768061378846157004292392264788138228486672543415580694421193506836000488465561599083339184724263183698928130695654949153165010313216361224018298711517222401523368101476246169896417259748838727189598765602350324828709741468793415378708814573190327920453219231685852735108372055942456601545647944675449566859142997988233179819059574125368681032194798082603876241044848730208905065871934264174092007936669883601462309762759844113071525758916288010581709353072588887654386253201848624931923638568216562603110434528313030704972291334873033240933736956347974889824930017415805659182123288343858101250171537305398462043432455721482088547523494730467761429282915391485852688505423074450548192619166975975031503447208211845313907683486006908772752077246485706597636740936173143436990399498908375710246545650814962015988805204483379491707040848303909417512426275869868668644293498242419667403627076032399201407183071270759837132000712447159523642782162488472933913713634046138974088894178399320090051543608421618891328957740354384456107645016010462709579098652495342014766016330458293537653454523438667413798731255017029554582809547897542497367109038598264606895622241257303208140890607025206140457815282368504505765710043804228592032720729190222134651835930255942940875306994701101153416476785623543575023993736414532895773499876167502240919794121893188059017977444329403624038551082491954751841177014150820554999148803286500065069030165028455616533514890711974194172310029663247936640825364542104897640445108081123906368188594908660418340025631562661211506365309297219580687177632051461355581309500814563826112416521487163593643553646268872746276680368630680088231249970572706496265335285424273723449757482776061300818063419639083097882249478922949525891665782610044424440110326748539620120023397129834624242363283711074267309902126029110038109050751840523266273905031934856015485510632624318778970878895198168073096354223096005536267735905099473408744371024816727970009494589707630185344952680106730984246828848883760016695887137355969244555238536396178788134209309376484848406842940499731494663578455826688245825356635393289729316700066238128368519670627697889769929009597838069557440769080950069594659578325366066060213000525012998145215099629307110700615796004759918829827472751877492472674770755413679265775060149528336859838085353420874215682758801259992855903410097963019943741001394975591822918846705741010634931594527954742032057295356596869586863097328488381174243827058441735659667485315202886191192125286398739560928127513223214119754229343092375569339614672740517569529376699061052365448344078610425576694541873486379356070861240473688356773437140126350120823765176390562050604076894729400293162079760342896846897639867830553941515230713725560502914671175123451932131962571791940911728951123948113598860588062424037835751996487088330150679210175429060531418836978611027896830689666851868410470182364780700615529883149883111601949965815038674390467105247175993726709203381051984777006122752302698038537619917731907133105816779008651480172440446403764720673784583395382889380902941273987910475254258486561698048543296782281040453997661165123290729161619992628751086519341731116513305659182981762584769428708454819029344222186027977405519291266188948708010515922860149238393490889782166965109499761673179583522105791358724355029782111425280584380959770472177893827382916471882671437865821461326011263516554280516418422188264141890686619186492751718984735037496602686033671961304915922609442146773092074476794711917820209913226872184947548378003848726148872742881265579174794634151444545105599464567614478293387968015412886418098284885525959617399177657635267081989985408930744564199296902459275405143647525648661932959903068323866757518479741015342911416508753572892479684280248440220211898390243430190746592470563991910024225814399068391457857458095344096826158489731615822039837691005171654390590093326827586419753439483771905973079465029210363641972615923872187876095687197681934481955852567024141433671590889694204781798936556351775101591005026585947279448642317311892727153525046034081896227383114600546852406398855471859684088277722162250586368419379964112646321070639818773794369650252104438622320671517228411475433482803041707675438555447584321271846396281391925884972509051040944134450429845346071848875654240709690138592611645519676563708429710676494635766201285381926791204110977805857352062737510466943591592074904378966129808716274322385039032007477854211063899544954185997641428116395197239708078986048758264126544825149923227286176571389697334537835963603962709038002668921324389159009375225033651171937770657226295341257068980907793198879997076783263303670667342657925395849950582363998610492878479976185891384024744790742355981796013254960652684988733518397287191251899388324341602608356164496670902390042273216221931567939944001215159910054381084520081133103207553492484487369268314444466610780275891777468369344585045949963237156043800258227618908603074550819931892899703285549507330240121766349515315827830897786432254556221744305752825143708087184314470811004510108612122699931396969361066523608721126359012344828262284427191281973187269761974740398071778378188160519801862257232970224762494767912932684020188061795236229174601398576604233579094407723017353015337974435643738584248250538061547193075224429309117207447677149522141919390974201716026970557825836923707297811545552570788004955666915477901830719591663516687057984336951611189153751912396714116378197000784953115386326766369269172016978409040396969804861828436417776804088449208439901095951205751340861060375353408155737087188313898337656322533650946010308686111901241541794900659835366926383515058402026098259570385429145865025692157987309807064597082326377138235585737704225628144262793497769429358804020882742028263786443615935817930817858306265712263479452174065216410798029333573961137404301928294367884626832432449078812684787281988676202931062510264948586549463964789154366240635570346688477784815271412470430646040615614277320107003575855033995279377529716156628381118518085523414187577256025217995103662771477552291036839539792329375184700131215428652464111526297830742328651189481978920924682746392250346179819781021313400022272303222234731521016033826145645816472110340883197207109422849637006090510260943044730126801795349152894613046101033061811314821366141874985466628809585678299308824993966655499624380015821082410781190328189506855057581990908848597095494573176672201417764187253816862426293852974092626551536758155537683368451820154793964862810533857810979434793077956125541240828563089647076354827276586047900779183041806574320855302776686899978897939486987950729652971448050889517660684386673056662911929857913206598752762097197279390208473846210277152094212386266930256260451209117402079233658157593274696841906354187366092529138116574357045728290417433832596884391356956442617823006949118156994294295529170211353842468704890572313005646106202029653246628477843902025194715815133791174898257040115532858624973690714844800747184719290671002133191274834310662201874141841328708920709275866745037664169280121112867057832132585948539987132879098472640550013972043153470930436509718084070853723316111111611632600262171748813737621046013600544051850633175245231989785291065646466038278748870331134307620041356514295482843502245454400571392386492526283423907951705366640483826875013469850263767974528926285288366544314868036628329638912254207094687335597669512007687507292940623176435604796651807847095408991068514998003358735387989422028901542800717906482276185298683079286137204396993726503610285463352157718364571843381950031926272352293654343387522809514152498052577486366048613580539162662183475105825647260311633442002377527140625112075332294909525522330744664115572260242435895269482927435844022622001466247093866533879048392320516224276433339282642640953964341822416705658461244760448817737705782669080880834418822622611342632727419248415651121035047131961583094994438779439078380664656207143187309895280874153167621657602227990850199615587578332393883365169478142077533262283694526612005465820771400826060398839255150948861553177333447506822679211849690448880479070102043288205874672361672971246062341973369704807867768609989464712379097525706498042381815865399434983035941162258347729020489356838477197804973214911448748749915616679253857438010864500220134843719609727912761136925035123155282535741655826107266099467657016111855684257826878422197833994329148734893923892153298966294232703135845615804723993624827409373966761563257981994036006655039613941881183164267144485664874468348587099434743710128859267552473831462181434321232124758618476925803128913233878664527525204324484796532776273320171351979849530142473805976430318655810403609897537469226336015596525652284888167037460054235043655813438329870872734142062859147847007274999414885129441657918212383876056572545671794085637289277002790218604788423519924573051811976377731594412994393860534559159658127123862955315918182841923881357245009246238507097741891437575676886206936433608263660374355173185026954239766173038826275043838965247160428689739548061640664606565379050539422795708801840829664956978192406737307076253014257542221763860230431809477056758905681723033326311408802886092880151777469082375063137750925275331638009836786645991949881018108222446858443984865972449621097999331605268587810061927125889694400669979755648800940895626242917531834388920035663113368763931463847812763130237825562198311791061780856687903309789539747505239545316630638169559777653347655949908779202359718666623572487055558216484036084925217803431104356647417600193631613474196113126657206064282217690428541246560204561459484317744683213906021267727411189443675804442911583757423572500214191467493342871160840582639470485636370375679604797073490813681083838562113841391587052553615073991983125473434527404596547926972539542447555990332809716643578039646945749813368621152410490288581779206318208255069166455507840899628333174744873951607229399258854694188637978240144635295264982572856632103053550891057171748674115218494774077589151115819489068851971959768129214023511454382738867557288320426608338030759515727545577639726238470674634011626346953231815229549718996906470438903536574430644436472716449550868519871817092814068746449470806856174570885105064766494332205391085097539987897980672278869943134632799032372604933150163386774039430519493297142505321117669011820293604482694166301309801111227443654953271242388534939973277749999335296667138307969441135719079969506099821923206878892624416110175909254904610286553512032488285673735148429324009831633211264460376172046209384270528903772251057643968938983722779640468452705694321085455273829462711022737243290606294601651732654594463569861350966095209962038508010899673666470073918705760679801337058347046567503369379598928154437380765511031719081985901371088639600700705631873099251480947989238619052479230983309717938226245725600119571130722386790431255742179135633111146646083268382596762356018472772209198013121983224179079476134977421748168833934278876403014334318798493417716613256506422668264638388429786875443810986754386459491846082078633346046469418429778813833857755519670005669840456587642130852057050148314568259387702428619224671173187370822224627538313365937868201435535126600146246249435880806572693573084485615073901842761167215162204840459913839674251648</pre>
 
=={{header|C++}}==
<langsyntaxhighlight lang="c++">
#include<bits/stdc++.h>
using namespace std;
Line 797 ⟶ 1,190:
return 0;
}
</syntaxhighlight>
</lang>
 
 
Line 806 ⟶ 1,199:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(ns agmcompute
(:gen-class))
 
Line 831 ⟶ 1,224:
 
(println (agm one isqrt2))
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 838 ⟶ 1,231:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol">IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
Line 870 ⟶ 1,263:
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).</langsyntaxhighlight>
{{out}}
<pre>0.8472130847939792</pre>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun agm (a0 g0 &optional (tolerance 1d-8))
(loop for a = a0 then (* (+ a g) 5d-1)
and g = g0 then (sqrt (* a g))
until (< (abs (- a g)) tolerance)
finally (return a)))
</syntaxhighlight>
</lang>
 
{{out}}
Line 891 ⟶ 1,284:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.math, std.meta, std.typecons;
 
real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe {
Line 903 ⟶ 1,296:
void main() @safe {
writefln("%0.19f", agm(1, 1 / sqrt(2.0)));
}</langsyntaxhighlight>
{{out}}
<pre>0.8472130847939790866</pre>
Line 910 ⟶ 1,303:
{{libheader| System.SysUtils}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program geometric_mean;
 
Line 954 ⟶ 1,347:
writeln(format('The arithmetic-geometric mean is %.6f', [agm(x, y)]));
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>Enter two numbers:1
2
The arithmetic-geometric mean is 1,456791</pre>
 
=={{header|dc}}==
<syntaxhighlight lang="dc">>>> 200 k ? sbsa [lalb +2/ lalb *vsb dsa lb - 0!=:]ds:xlap
?> 1 1 2 v /</syntaxhighlight>
 
{{out}}
<pre>
.8472130847939790866064991234821916364814459103269421850605793726597\
34004834134759723200293994611229942122285625233410963097962665830871\
05969971363598338425117632681428906038970676860161665004828118868
</pre>
 
You can change the precision (200 by default)
 
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight lang=easylang>
func agm a g .
repeat
a0 = a
a = (a0 + g) / 2
g = sqrt (a0 * g)
until abs (a0 - a) < abs (a) * 1e-15
.
return a
.
numfmt 16 0
print agm 1 sqrt 0.5
</syntaxhighlight>
 
=={{header|EchoLisp}}==
We use the '''(~= a b)''' operator which tests for |a - b| < ε = (math-precision).
<langsyntaxhighlight lang="scheme">
(lib 'math)
 
Line 977 ⟶ 1,399:
(agm 1 (/ 1 (sqrt 2)))
→ 0.8472130847939792
</syntaxhighlight>
</lang>
 
=={{header|Elixir}}==
 
<langsyntaxhighlight Elixirlang="elixir">defmodule ArithhGeom do
def mean(a,g,tol) when abs(a-g) <= tol, do: a
def mean(a,g,tol) do
Line 988 ⟶ 1,410:
end
 
IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)</langsyntaxhighlight>
 
{{out}}
Line 996 ⟶ 1,418:
 
=={{header|Erlang}}==
<langsyntaxhighlight Erlanglang="erlang">%% Arithmetic Geometric Mean of 1 and 1 / sqrt(2)
%% Author: Abhay Jain
 
Line 1,014 ⟶ 1,436:
A1 = (A+B) / 2,
B1 = math:pow(A*B, 0.5),
agm(A1, B1).</langsyntaxhighlight>
Output:
<langsyntaxhighlight Erlanglang="erlang">AGM = 0.8472130848351929</langsyntaxhighlight>
 
=={{header|ERRE}}==
<syntaxhighlight lang="text">
PROGRAM AGM
 
Line 1,041 ⟶ 1,463:
PRINT(A)
END PROGRAM
</syntaxhighlight>
</lang>
 
=={{header|F_Sharp|F#}}==
{{trans|OCaml}}
<langsyntaxhighlight lang="fsharp">let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
 
printfn "%g" (agm 1. (sqrt(0.5)) 1e-15)</langsyntaxhighlight>
Output
<pre>0.847213</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
 
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
 
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .</langsyntaxhighlight>
{{out}}
<pre>
Line 1,066 ⟶ 1,488:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: agm ( a g -- m )
begin
fover fover f+ 2e f/
Line 1,074 ⟶ 1,496:
fdrop ;
 
1e 2e -0.5e f** agm f. \ 0.847213084793979</langsyntaxhighlight>
 
=={{header|Fortran}}==
A '''Fortran 77''' implementation
<langsyntaxhighlight lang="fortran"> function agm(a,b)
implicit none
double precision agm,a,b,eps,c
Line 1,092 ⟶ 1,514:
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
end</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<lang freebasic>' version 16-09-2015
' compile with: fbc -s console
 
Function agm(a As Double, g As Double) As Double
Dim As Double t_a
Do
t_a = (a + g) / 2
g = Sqr(a * g)
Swap a, t_a
Loop Until a = t_a
Return a
End Function
 
' ------=< MAIN >=------
 
Print agm(1, 1 / Sqr(2) )
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
{{out}}
<pre> 0.8472130847939792</pre>
 
=={{header|Futhark}}==
{{incorrect|Futhark|Futhark's syntax has changed, so this example will not compile}}
 
<syntaxhighlight lang="futhark">
<lang Futhark>
import "futlib/math"
 
Line 1,139 ⟶ 1,531:
fun main(x: f64, y: f64): f64 =
agm(x,y)
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,160 ⟶ 1,552:
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,169 ⟶ 1,561:
{{trans|Java}}
Solution:
<langsyntaxhighlight lang="groovy">double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}</langsyntaxhighlight>
 
Test:
<langsyntaxhighlight lang="groovy">println "agm(1, 0.5**0.5) = agm(1, ${0.5**0.5}) = ${agm(1, 0.5**0.5)}"
assert (0.8472130847939792 - agm(1, 0.5**0.5)).abs() <= 10.0**-14</langsyntaxhighlight>
 
Output:
Line 1,183 ⟶ 1,575:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">-- Return an approximation to the arithmetic-geometric mean of two numbers.
-- The result is considered accurate when two successive approximations are
-- sufficiently close, as determined by "eq".
Line 1,202 ⟶ 1,594:
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal</langsyntaxhighlight>
{{out}}
<pre>0.8472130847527654</pre>
Line 1,208 ⟶ 1,600:
=={{header|Icon}} and {{header|Unicon}}==
 
<syntaxhighlight lang="text">procedure main(A)
a := real(A[1]) | 1.0
g := real(A[2]) | (1 / 2^0.5)
Line 1,223 ⟶ 1,615:
}
return an
end</langsyntaxhighlight>
 
Output:
Line 1,239 ⟶ 1,631:
First, the basic approach (with display precision set to 16 digits, which slightly exceeds the accuracy of 64 bit IEEE floating point arithmetic):
 
<langsyntaxhighlight lang="j">mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791</langsyntaxhighlight>
 
This is the limit -- it stops when values are within a small epsilon of previous calculations. We can ask J for unique values (which also means -- unless we specify otherwise -- values within a small epsilon of each other, for floating point values):
 
<langsyntaxhighlight lang="j"> ~.(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792</langsyntaxhighlight>
 
Another variation would be to show intermediate values, in the limit process:
 
<langsyntaxhighlight lang="j"> (mean, */ %:~ #)^:a: 1,%%:2
1 0.7071067811865475
0.8535533905932737 0.8408964152537145
0.8472249029234942 0.8472012667468915
0.8472130848351929 0.8472130847527654
0.8472130847939792 0.8472130847939791</langsyntaxhighlight>
 
=== Arbitrary Precision ===
Line 1,263 ⟶ 1,655:
Borrowing routines from that page, but going with a default of approximately 100 digits of precision:
 
<langsyntaxhighlight Jlang="j">DP=:101
 
round=: DP&$: : (4 : 0)
Line 1,292 ⟶ 1,684:
n=. e (>i.1:) a (^%!@]) i.>.a^.e [ a=. |y-m*^.2
(2x^m) * 1++/*/\d%1+i.n
)</langsyntaxhighlight>
 
We are also going to want a routine to display numbers with this precision, and we are going to need to manage epsilon manually, and we are going to need an arbitrary root routine:
 
<langsyntaxhighlight Jlang="j">fmt=:[: ;:inv DP&$: : (4 :0)&.>
x{.deb (x*2j1)":y
)
Line 1,302 ⟶ 1,694:
root=: ln@] exp@% [
 
epsilon=: 1r9^DP</langsyntaxhighlight>
 
Some example uses:
 
<langsyntaxhighlight Jlang="j"> fmt sqrt 2
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572
fmt *~sqrt 2
Line 1,313 ⟶ 1,705:
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000418
fmt 2 root 2
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572</langsyntaxhighlight>
 
Note that 2 root 2 is considerably slower than sqrt 2. The price of generality. So, while we could define geometric mean generally, a desire for good performance pushes us to use a routine specialized for two numbers:
 
<langsyntaxhighlight Jlang="j">geomean=: */ root~ #
geomean2=: [: sqrt */</langsyntaxhighlight>
 
A quick test to make sure these can be equivalent:
 
<langsyntaxhighlight Jlang="j"> fmt geomean 3 5
3.872983346207416885179265399782399610832921705291590826587573766113483091936979033519287376858673517
fmt geomean2 3 5
3.872983346207416885179265399782399610832921705291590826587573766113483091936979033519287376858673517</langsyntaxhighlight>
 
Now for our task example:
 
<langsyntaxhighlight Jlang="j"> fmt (mean, geomean2)^:(epsilon <&| -/)^:a: 1,%sqrt 2
1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0.707106781186547524400844362104849039284835937688474036588339868995366239231053519425193767163820786
0.853553390593273762200422181052424519642417968844237018294169934497683119615526759712596883581910393 0.840896415253714543031125476233214895040034262356784510813226085974924754953902239814324004199292536
Line 1,337 ⟶ 1,729:
0.847213084793979086606499123482191636481445984459557704232275241670533381126169243513557113565344075 0.847213084793979086606499123482191636481445836194326665888883503648934628542100275932846717790147361
0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723201915677745718 0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723198672311476741
0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723200293994611229 0.847213084793979086606499123482191636481445910326942185060579372659734004834134759723200293994611229</langsyntaxhighlight>
 
We could of course extract out only a representative final value, but it's obvious enough, and showing how rapidly this converges is fun.
Line 1,343 ⟶ 1,735:
=={{header|Java}}==
 
<syntaxhighlight lang="java">/*
<lang Java>/*
* Arithmetic-Geometric Mean of 1 & 1/sqrt(2)
* Brendan Shaklovitz
Line 1,365 ⟶ 1,757:
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}</langsyntaxhighlight>
{{out}}
<pre>0.8472130847939792</pre>
Line 1,372 ⟶ 1,764:
 
===ES5===
<langsyntaxhighlight JavaScriptlang="javascript">function agm(a0, g0) {
var an = (a0 + g0) / 2,
gn = Math.sqrt(a0 * g0);
Line 1,381 ⟶ 1,773:
}
 
agm(1, 1 / Math.sqrt(2));</langsyntaxhighlight>
 
===ES6===
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 1,426 ⟶ 1,818:
return agm(1, 1 / Math.sqrt(2));
 
})();</langsyntaxhighlight>
 
{{Out}}
<syntaxhighlight lang JavaScript="javascript">0.8472130848351929</langsyntaxhighlight>
 
=={{header|jq}}==
{{works with|jq|1.4}}
Naive version that assumes tolerance is appropriately specified:
<langsyntaxhighlight lang="jq">def naive_agm(a; g; tolerance):
def abs: if . < 0 then -. else . end;
def _agm:
Line 1,442 ⟶ 1,834:
else .
end;
[a, g] | _agm | .[0] ;</langsyntaxhighlight>
This version avoids an infinite loop if the requested tolerance is too small:
<langsyntaxhighlight lang="jq">def agm(a; g; tolerance):
def abs: if . < 0 then -. else . end;
def _agm:
Line 1,459 ⟶ 1,851:
# Example:
agm(1; 1/(2|sqrt); 1e-100)</langsyntaxhighlight>
{{Out}}
$ jq -n -f Arithmetic-geometric_mean.jq
Line 1,466 ⟶ 1,858:
=={{header|Julia}}==
{{works with|Julia|1.2}}
<langsyntaxhighlight Julialang="julia">function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
Line 1,485 ⟶ 1,877:
println("# Using ", precision(BigFloat), "-bit float numbers:")
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)</langsyntaxhighlight>
The &epsilon; for this calculation is given as a positive integer multiple of the machine &epsilon; for <tt>x</tt>.
 
Line 1,498 ⟶ 1,890:
=={{header|Klingphix}}==
{{trans|Oforth}}
<langsyntaxhighlight Klingphixlang="klingphix">include ..\Utilitys.tlhy
 
:agm [ over over + 2 / rot rot * sqrt ] [ over over tostr swap tostr # ] while drop ;
Line 1,506 ⟶ 1,898:
pstack
 
" " input</langsyntaxhighlight>
{{trans|F#}}
<langsyntaxhighlight Klingphixlang="klingphix">include ..\Utilitys.tlhy
 
:agm %a %g %p !p !g !a
Line 1,517 ⟶ 1,909:
pstack
 
" " input</langsyntaxhighlight>
{{out}}
<pre>(0.847213)</pre>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.5-2
 
fun agm(a: Double, g: Double): Double {
Line 1,540 ⟶ 1,932:
fun main(args: Array<String>) {
println(agm(1.0, 1.0 / Math.sqrt(2.0)))
}</langsyntaxhighlight>
 
{{out}}
Line 1,546 ⟶ 1,938:
0.8472130847939792
</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="Scheme">
{def eps 1e-15}
-> eps
 
{def agm
{lambda {:a :g}
{if {> {abs {- :a :g}} {eps}}
then {agm {/ {+ :a :g} 2}
{sqrt {* :a :g}}}
else :a }}}
-> agm
 
{agm 1 {/ 1 {sqrt 2}}}
-> 0.8472130847939792
 
Multi-precision version using the lib_BN library
 
{BN.DEC 70}
-> 70 digits
{def EPS {BN./ 1 {BN.pow 10 45}}}
-> EPS
{def AGM
{lambda {:a :g}
{if {= {BN.compare {BN.abs {BN.- :a :g}} {EPS}} 1}
then {AGM {BN./ {BN.+ :a :g} 2}
{BN.sqrt {BN.* :a :g}}}
else :a }}}
-> AGM
 
{AGM 1 {BN./ 1 {BN.sqrt 2}}}
-> 0.8472130847939790866064991234821916364814459103269421850605793726597339
</syntaxhighlight>
 
=={{header|LFE}}==
 
<langsyntaxhighlight lang="lisp">
(defun agm (a g)
(agm a g 1.0e-15))
Line 1,565 ⟶ 1,992:
(defun next-g (a g)
(math:sqrt (* a g)))
</syntaxhighlight>
</lang>
 
Usage:
Line 1,573 ⟶ 2,000:
0.8472130847939792
</pre>
 
=={{header|Liberty BASIC}}==
<lang lb>
print agm(1, 1/sqr(2))
print using("#.#################",agm(1, 1/sqr(2)))
 
function agm(a,g)
do
absdiff = abs(a-g)
an=(a+g)/2
gn=sqr(a*g)
a=an
g=gn
loop while abs(an-gn)< absdiff
agm = a
end function
</lang>
 
=={{header|LiveCode}}==
<langsyntaxhighlight LiveCodelang="livecode">function agm aa,g
put abs(aa-g) into absdiff
put (aa+g)/2 into aan
Line 1,605 ⟶ 2,014:
end repeat
return aa
end agm</langsyntaxhighlight>
Example
<langsyntaxhighlight LiveCodelang="livecode">put agm(1, 1/sqrt(2))
-- ouput
-- 0.847213</langsyntaxhighlight>
 
=={{header|LLVM}}==
<langsyntaxhighlight lang="llvm">; This is not strictly LLVM, as it uses the C library function "printf".
; LLVM does not provide a way to print values, so the alternative would be
; to just load the string into memory, and that would be boring.
Line 1,714 ⟶ 2,123:
attributes #2 = { nounwind readnone speculatable }
attributes #4 = { nounwind }
attributes #6 = { noreturn }</langsyntaxhighlight>
{{out}}
<pre>The arithmetic-geometric mean is 0.8472130847939791654</pre>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">to about :a :b
output and [:a - :b < 1e-15] [:a - :b > -1e-15]
end
Line 1,728 ⟶ 2,137:
 
show agm 1 1/sqrt 2
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
 
<langsyntaxhighlight lang="lua">function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
Line 1,742 ⟶ 2,151:
end
 
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))</langsyntaxhighlight>
 
'''Output:'''
Line 1,749 ⟶ 2,158:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
Function Agm {
Line 1,764 ⟶ 2,173:
}
Checkit
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
Maple provides this function under the name GaussAGM. To compute a floating point approximation, use evalf.
<syntaxhighlight lang="maple">
<lang Maple>
> evalf( GaussAGM( 1, 1 / sqrt( 2 ) ) ); # default precision is 10 digits
0.8472130847
Line 1,775 ⟶ 2,184:
0.847213084793979086606499123482191636481445910326942185060579372659\
7340048341347597232002939946112300
</syntaxhighlight>
</lang>
Alternatively, if one or both arguments is already a float, Maple will compute a floating point approximation automatically.
<syntaxhighlight lang="maple">
<lang Maple>
> GaussAGM( 1.0, 1 / sqrt( 2 ) );
0.8472130847
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
To any arbitrary precision, just increase PrecisionDigits
<langsyntaxhighlight Mathematicalang="mathematica">PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛</langsyntaxhighlight>
 
<pre>AGMean[1, 1/Sqrt[2]]
Line 1,791 ⟶ 2,200:
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight MATLABlang="matlab">function [a,g]=agm(a,g)
%%arithmetic_geometric_mean(a,g)
while (1)
Line 1,799 ⟶ 2,208:
if (abs(a0-a) < a*eps) break; end;
end;
end</langsyntaxhighlight>
<pre>octave:26> agm(1,1/sqrt(2))
ans = 0.84721
Line 1,805 ⟶ 2,214:
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">agm(a, b) := %pi/4*(a + b)/elliptic_kc(((a - b)/(a + b))^2)$
 
agm(1, 1/sqrt(2)), bfloat, fpprec: 85;
/* 8.472130847939790866064991234821916364814459103269421850605793726597340048341347597232b-1 */</langsyntaxhighlight>
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">П1 <-> П0 1 ВП 8 /-/ П2 ИП0 ИП1
- ИП2 - /-/ x<0 31 ИП1 П3 ИП0 ИП1
* КвКор П1 ИП0 ИП3 + 2 / П0 БП
08 ИП0 С/П</langsyntaxhighlight>
 
=={{header|Modula-2}}==
{{trans|C}}
<langsyntaxhighlight lang="modula2">MODULE AGM;
FROM EXCEPTIONS IMPORT AllocateSource,ExceptionSource,GetMessage,RAISE;
FROM LongConv IMPORT ValueReal;
Line 1,887 ⟶ 2,296:
WriteReal(AGM(x, y));
WriteLn
END AGM.</langsyntaxhighlight>
{{out}}
<pre>Enter two numbers: 1.0
Line 1,898 ⟶ 2,307:
=={{header|NetRexx}}==
{{trans|Java}}
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 1,922 ⟶ 2,331:
end
return a1 + 0
</syntaxhighlight>
</lang>
'''Output:'''
<pre>
Line 1,929 ⟶ 2,338:
 
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">
<lang NewLISP>
(define (a-next a g) (mul 0.5 (add a g)))
 
Line 1,951 ⟶ 2,360:
(amg 1.0 root-reciprocal-2 quadrillionth)
)
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import math
 
proc agm(a, g: float,delta: float = 1.0e-15): float =
Line 1,967 ⟶ 2,376:
result = aOld
 
echo agm(1.0,1.0/sqrt(2.0))</langsyntaxhighlight>
 
Output:<br/>
Line 1,976 ⟶ 2,385:
See first 24 iterations:
 
<langsyntaxhighlight lang="nim">from math import sqrt
from strutils import parseFloat, formatFloat, ffDecimal
 
Line 1,995 ⟶ 2,404:
 
echo("Result A: " & formatFloat(t.resA, ffDecimal, 24))
echo("Result G: " & formatFloat(t.resG, ffDecimal, 24))</langsyntaxhighlight>
 
=={{header|Oberon-2}}==
{{works with|oo2c}}
<langsyntaxhighlight lang="oberon2">
MODULE Agm;
IMPORT
Line 2,025 ⟶ 2,434:
Out.LongReal(Of(1,1 / Math.sqrt(2)),0,0);Out.Ln
END Agm.
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,033 ⟶ 2,442:
=={{header|Objeck}}==
{{trans|Java}}
<langsyntaxhighlight lang="objeck">
class ArithmeticMean {
function : Amg(a : Float, g : Float) ~ Nil {
Line 2,050 ⟶ 2,459:
}
}
</syntaxhighlight>
</lang>
 
Output:
Line 2,056 ⟶ 2,465:
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
 
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)</langsyntaxhighlight>
Output
<pre>0.8472130847939792</pre>
Line 2,066 ⟶ 2,475:
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">: agm \ a b -- m
while( 2dup <> ) [ 2dup + 2 / -rot * sqrt ] drop ;</langsyntaxhighlight>
 
Usage :
<syntaxhighlight lang Oforth="oforth">1 2 sqrt inv agm</langsyntaxhighlight>
 
{{out}}
Line 2,078 ⟶ 2,487:
 
=={{header|OOC}}==
<langsyntaxhighlight lang="ooc">
import math // import for sqrt() function
 
Line 2,098 ⟶ 2,507:
"%.16f" printfln(agm(1., sqrt(0.5)))
}
</syntaxhighlight>
</lang>
Output
<pre>0.8472130847939792</pre>
 
=={{header|ooRexx}}==
<langsyntaxhighlight ooRexxlang="oorexx">numeric digits 20
say agm(1, 1/rxcalcsqrt(2,16))
 
Line 2,120 ⟶ 2,529:
return a1+0
 
::requires rxmath LIBRARY</langsyntaxhighlight>
{{out}}
<pre>0.8472130847939791968</pre>
Line 2,126 ⟶ 2,535:
=={{header|PARI/GP}}==
Built-in:
<langsyntaxhighlight lang="parigp">agm(1,1/sqrt(2))</langsyntaxhighlight>
 
Iteration:
<langsyntaxhighlight lang="parigp">agm2(x,y)=if(x==y,x,agm2((x+y)/2,sqrt(x*y))</langsyntaxhighlight>
 
=={{header|Pascal}}==
Line 2,135 ⟶ 2,544:
{{libheader|GMP}}
Port of the C example:
<langsyntaxhighlight lang="pascal">Program ArithmeticGeometricMean;
 
uses
Line 2,169 ⟶ 2,578:
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.</langsyntaxhighlight>
Output is as long as the C example.
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl -w
 
my ($a0, $g0, $a1, $g1);
Line 2,189 ⟶ 2,598:
}
 
print agm(1, 1/sqrt(2))."\n";</langsyntaxhighlight>
Output:
<pre>0.847213084793979</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">agm</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">tolerance</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1.0e-15</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">-</span><span style="color: #000000;">g</span><span style="color: #0000FF;">)></span><span style="color: #000000;">tolerance</span> <span style="color: #008080;">do</span>
Line 2,203 ⟶ 2,612:
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">agm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- (rounds to 10 d.p.)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,214 ⟶ 2,623:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight Phixmontilang="phixmonti">include ..\Utilitys.pmt
 
1.0e-15 var tolerance
Line 2,228 ⟶ 2,637:
enddef
 
1 1 2 sqrt / agm tostr ?</langsyntaxhighlight>
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">
define('PRECISION', 13);
 
Line 2,254 ⟶ 2,663:
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
</syntaxhighlight>
</lang>
{{out}}
<pre>
0.8472130848350
</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">main =>
println(agm(1.0, 1/sqrt(2))).
 
agm(A,G) = A, A-G < 1.0e-10 => true.
agm(A,G) = agm((A+G)/2, sqrt(A*G)).
</syntaxhighlight>
 
{{out}}
<pre>
0.847213084835193
</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(scl 80)
 
(de agm (A G)
Line 2,270 ⟶ 2,692:
(round
(agm 1.0 (*/ 1.0 1.0 (sqrt 2.0 1.0)))
70 )</langsyntaxhighlight>
Output:
<pre>-> "0.8472130847939790866064991234821916364814459103269421850605793726597340"</pre>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
arithmetic_geometric_mean: /* 31 August 2012 */
procedure options (main);
Line 2,289 ⟶ 2,711:
put skip list ('The result is:', a);
end arithmetic_geometric_mean;
</syntaxhighlight>
</lang>
Results:
<pre>
Line 2,302 ⟶ 2,724:
=={{header|Potion}}==
Input values should be floating point
<langsyntaxhighlight lang="potion">sqrt = (x) :
xi = 1
7 times :
Line 2,318 ⟶ 2,740:
.
x
.</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
Line 2,336 ⟶ 2,758:
}
agm 1 (1/[Math]::Sqrt(2))
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 2,345 ⟶ 2,767:
 
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">
<lang Prolog>
agm(A,G,A) :- abs(A-G) < 1.0e-15, !.
agm(A,G,Res) :- A1 is (A+G)/2.0, G1 is sqrt(A*G),!, agm(A1,G1,Res).
Line 2,351 ⟶ 2,773:
?- agm(1,1/sqrt(2),Res).
Res = 0.8472130847939792.
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<lang purebasic>Procedure.d AGM(a.d, g.d, ErrLim.d=1e-15)
Protected.d ta=a+1, tg
While ta <> a
ta=a: tg=g
a=(ta+tg)*0.5
g=Sqr(ta*tg)
Wend
ProcedureReturn a
EndProcedure
 
If OpenConsole()
PrintN(StrD(AGM(1, 1/Sqr(2)), 16))
Input()
CloseConsole()
EndIf</lang>
 
0.8472130847939792
 
=={{header|Python}}==
Line 2,376 ⟶ 2,779:
 
===Basic Version===
<langsyntaxhighlight lang="python">from math import sqrt
 
def agm(a0, g0, tolerance=1e-10):
Line 2,391 ⟶ 2,794:
return an
 
print agm(1, 1 / sqrt(2))</langsyntaxhighlight>
{{out}}
<pre> 0.847213084835</pre>
===Multi-Precision Version===
<langsyntaxhighlight lang="python">from decimal import Decimal, getcontext
 
def agm(a, g, tolerance=Decimal("1e-65")):
Line 2,404 ⟶ 2,807:
 
getcontext().prec = 70
print agm(Decimal(1), 1 / Decimal(2).sqrt())</langsyntaxhighlight>
{{out}}
<pre>0.847213084793979086606499123482191636481445910326942185060579372659734</pre>
Line 2,411 ⟶ 2,814:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ $ "bigrat.qky" loadfile ] now!
 
[ temp put
Line 2,428 ⟶ 2,831:
125 point$ echo$ cr cr
swap say "Num: " echo cr
say "Den: " echo</langsyntaxhighlight>
 
{{out}}
Line 2,441 ⟶ 2,844:
 
=={{header|R}}==
<langsyntaxhighlight lang="r">arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
 
Line 2,454 ⟶ 2,857:
 
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))</langsyntaxhighlight>
{{out}}
<pre> agm rel_error
1 0.8472130847939792 1.310441309927519e-16</pre>
This function also works on vectors a and b (following the spirit of R):
<langsyntaxhighlight lang="r">a <- c(1, 1, 1)
b <- c(1/sqrt(2), 1/sqrt(3), 1/2)
agm <- arithmeticGeometricMean(a, b)
print(format(agm, digits=16))</langsyntaxhighlight>
{{out}}
<pre> agm rel_error
Line 2,471 ⟶ 2,874:
=={{header|Racket}}==
This version uses Racket's normal numbers:
<langsyntaxhighlight lang="racket">
#lang racket
(define (agm a g [ε 1e-15])
Line 2,479 ⟶ 2,882:
 
(agm 1 (/ 1 (sqrt 2)))
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,486 ⟶ 2,889:
 
This alternative version uses arbitrary precision floats:
<langsyntaxhighlight lang="racket">
#lang racket
(require math/bigfloat)
(bf-precision 200)
(bfagm 1.bf (bf/ (bfsqrt 2.bf)))
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,499 ⟶ 2,902:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub agm( $a is copy, $g is copy ) {
($a, $g) = ($a + $g)/2, sqrt $a * $g until $a ≅ $g;
return $a;
}
say agm 1, 1/sqrt 2;</langsyntaxhighlight>
{{out}}
<pre>0.84721308479397917</pre>
 
It's also possible to write it recursively:
<syntaxhighlight lang="raku" perl6line>sub agm( $a, $g ) {
$a ≅ $g ?? $a !! agm(|@$_)
given ($a + $g)/2, sqrt $a * $g;
}
 
say agm 1, 1/sqrt 2;</langsyntaxhighlight>
 
We can also get a bit fancy and use a converging sequence of complex numbers:
 
<syntaxhighlight lang=raku>sub agm {
($^z, {(.re+.im)/2 + (.re*.im).sqrt*1i} ... * ≅ *)
.tail.re
}
say agm 1 + 1i/2.sqrt</syntaxhighlight>
 
=={{header|Raven}}==
<langsyntaxhighlight Ravenlang="raven">define agm use $a, $g, $errlim
# $errlim $g $a "%d %g %d\n" print
$a 1.0 + as $t
Line 2,528 ⟶ 2,940:
 
 
16 1 2 sqrt / 1 agm "agm: %.15g\n" print</langsyntaxhighlight>
{{out}}
<pre>t: 0.853553 a: 0.853553 g: 0.840896
Line 2,537 ⟶ 2,949:
 
=={{header|Relation}}==
<syntaxhighlight lang="relation">
<lang Relation>
function agm(x,y)
set a = x
Line 2,556 ⟶ 2,968:
echo sqrt(x+y)
echo agm(x,y)
</syntaxhighlight>
</lang>
 
<pre>
Line 2,568 ⟶ 2,980:
 
REXX supports arbitrary precision, so the default digits can be changed if desired.
<langsyntaxhighlight lang="rexx">/*REXX program calculates the AGM (arithmetic─geometric mean) of two (real) numbers. */
parse arg a b digs . /*obtain optional numbers from the C.L.*/
if digs=='' | digs=="," then digs= 120 /*No DIGS specified? Then use default.*/
Line 2,599 ⟶ 3,011:
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_ % 2
do j=0 while h>9; m.j=h; h=h % 2 + 1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/; return g</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 2,608 ⟶ 3,020:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
decimals(9)
see agm(1, 1/sqrt(2)) + nl
Line 2,622 ⟶ 3,034:
end
return gn
</syntaxhighlight>
</lang>
 
=={{header|RPL}}==
≪ 1E-10 → epsilon
≪ '''WHILE''' DUP2 - ABS epsilon > '''REPEAT'''
DUP2 + 2 / ROT ROT * √
'''END''' DROP
≫ ≫ ‘'''AGM'''’ STO
{{in}}
<pre>
1 2 / √ AGM
</pre>
{{out}}
<pre>
1: .847213084835
</pre>
 
=={{header|Ruby}}==
===Flt Version===
The thing to note about this implementation is that it uses the [http://flt.rubyforge.org/ Flt] library for high-precision math. This lets you adapt context (including precision and epsilon) to a ridiculous-in-real-life degree.
<langsyntaxhighlight lang="ruby"># The flt package (http://flt.rubyforge.org/) is useful for high-precision floating-point math.
# It lets us control 'context' of numbers, individually or collectively -- including precision
# (which adjusts the context's value of epsilon accordingly).
Line 2,647 ⟶ 3,074:
end
 
puts agm(1, 1 / BinNum(2).sqrt)</langsyntaxhighlight>
{{out}}
<pre>0.84721308479397908660649912348219163648144591032694218506057937265973400483413475972320029399461122994212228562523341096309796266583087105969971363598338426</pre>
Line 2,654 ⟶ 3,081:
===BigDecimal Version===
Ruby has a BigDecimal class in standard library
<langsyntaxhighlight lang="ruby">require 'bigdecimal'
 
PRECISION = 100
Line 2,669 ⟶ 3,096:
a = BigDecimal(1)
g = 1 / BigDecimal(2).sqrt(PRECISION)
puts agm(a, g)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,675 ⟶ 3,102:
0.8472130847939790866064991234821916364814459103269421850605793726597340048341347597231986723114767413E0
</pre>
 
=={{header|Run BASIC}}==
<lang runbasic>print agm(1, 1/sqr(2))
print agm(1,1/2^.5)
print using("#.############################",agm(1, 1/sqr(2)))
 
function agm(agm,g)
while agm
an = (agm + g)/2
gn = sqr(agm*g)
if abs(agm-g) <= abs(an-gn) then exit while
agm = an
g = gn
wend
end function</lang>Output:
<pre>0.847213085
0.847213085
0.8472130847939791165772005376</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">// Accepts two command line arguments
 
<lang rust>// Accepts two command line arguments
// cargo run --name agm arg1 arg2
 
Line 2,726 ⟶ 3,134:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,735 ⟶ 3,143:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">
def agm(a: Double, g: Double, eps: Double): Double = {
if (math.abs(a - g) < eps) (a + g) / 2
Line 2,742 ⟶ 3,150:
 
agm(1, math.sqrt(2)/2, 1e-15)
</syntaxhighlight>
</lang>
 
=={{header|Scheme}}==
 
<langsyntaxhighlight lang="scheme">
(define agm
(case-lambda
Line 2,757 ⟶ 3,165:
 
(display (agm 1 (/ 1 (sqrt 2)))) (newline)
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,765 ⟶ 3,173:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
Line 2,794 ⟶ 3,202:
writeln(agm(1.0, 2.0) digits 6);
writeln(agm(1.0, 1.0 / sqrt(2.0)) digits 6);
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,803 ⟶ 3,211:
 
=={{header|SequenceL}}==
<langsyntaxhighlight lang="sequencel">import <Utilities/Math.sl>;
 
agm(a, g) :=
Line 2,815 ⟶ 3,223:
agm(arithmeticMean, geometricMean);
 
main := agm(1.0, 1.0 / sqrt(2));</langsyntaxhighlight>
 
{{out}}
Line 2,823 ⟶ 3,231:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func agm(a, g) {
loop {
var (a1, g1) = ((a+g)/2, sqrt(a*g))
Line 2,831 ⟶ 3,239:
}
 
say agm(1, 1/sqrt(2))</langsyntaxhighlight>
{{out}}
<pre>0.8472130847939790866064991234821916364814</pre>
 
=={{header|Sinclair ZX81 BASIC}}==
{{trans|COBOL}}
Works with 1k of RAM.
 
The specification calls for a function. Sadly that is not available to us, so this program uses a subroutine: pass the arguments in the global variables <tt>A</tt> and <tt>G</tt>, and the result will be returned in <tt>AGM</tt>. The performance is quite acceptable. Note that the subroutine clobbers <tt>A</tt> and <tt>G</tt>, so you should save them if you want to use them again.
 
Better precision than this is not easily obtainable on the ZX81, unfortunately.
<lang basic> 10 LET A=1
20 LET G=1/SQR 2
30 GOSUB 100
40 PRINT AGM
50 STOP
100 LET A0=A
110 LET A=(A+G)/2
120 LET G=SQR (A0*G)
130 IF ABS(A-G)>.00000001 THEN GOTO 100
140 LET AGM=A
150 RETURN</lang>
{{out}}
<pre>0.84721309</pre>
 
=={{header|Smalltalk}}==
{{works with|Smalltalk/X}}
That is simply a copy/paste of the already existing agm method in the Number class:
<langsyntaxhighlight lang="smalltalk">agm:y
"return the arithmetic-geometric mean agm(x, y)
of the receiver (x) and the argument, y.
Line 2,877 ⟶ 3,264:
gi := gn.
] doUntil:[ delta < epsilon ].
^ ai</langsyntaxhighlight>
 
<langsyntaxhighlight lang="smalltalk">Transcript showCR: (24 agm:6).
Transcript showCR: ( (1/2) agm:(1/6) ).
Transcript showCR: (1 agm:(1 / 2 sqrt)).</langsyntaxhighlight>
{{out}}
<pre>13.4581714817256
Line 2,890 ⟶ 3,277:
{{works with|oracle|11.2 and higher}}
The solution uses recursive WITH clause (aka recursive CTE, recursive query, recursive factored subquery). Some, perhaps many, but not all SQL dialects support recursive WITH clause. The solution below was written and tested in Oracle SQL - Oracle has supported recursive WITH clause since version 11.2.
<langsyntaxhighlight lang="sql">with
rec (rn, a, g, diff) as (
select 1, 1, 1/sqrt(2), 1 - 1/sqrt(2)
Line 2,902 ⟶ 3,289:
from rec
where diff <= 1e-38
;</langsyntaxhighlight>
 
 
Line 2,912 ⟶ 3,299:
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">
fun agm(a, g) = let
fun agm'(a, g, eps) =
Line 2,921 ⟶ 3,308:
in agm'(a, g, 1e~15)
end;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,928 ⟶ 3,315:
 
=={{header|Stata}}==
<langsyntaxhighlight lang="stata">mata
 
real scalar agm(real scalar a, real scalar b) {
Line 2,941 ⟶ 3,328:
 
agm(1,1/sqrt(2))
end</langsyntaxhighlight>
{{out}}
<pre>.8472130848</pre>
 
=={{header|Swift}}==
<langsyntaxhighlight Swiftlang="swift">import Darwin
 
enum AGRError : Error {
Line 2,976 ⟶ 3,363:
} catch {
print("agr is undefined when a * g < 0")
}</langsyntaxhighlight>
{{out}}
<pre>0.847213084835193</pre>
Line 2,982 ⟶ 3,369:
=={{header|Tcl}}==
The tricky thing about this implementation is that despite the finite precision available to IEEE doubles (which Tcl uses in its implementation of floating point arithmetic, in common with many other languages) the sequence of values does not ''quite'' converge to a single value; it gets to within a ULP and then errors prevent it from getting closer. This means that an additional termination condition is required: once a value does not change (hence the <code>old_b</code> variable) we have got as close as we can. Note also that we are using exact equality with floating point; this is reasonable because this is a rapidly converging sequence (it only takes 4 iterations in this case).
<langsyntaxhighlight lang="tcl">proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
Line 2,991 ⟶ 3,378:
}
 
puts [agm 1 [expr 1/sqrt(2)]]</langsyntaxhighlight>
Output:
<pre>0.8472130847939792</pre>
 
=={{header|TI-83 BASICSR-56}}==
{| class="wikitable"
<lang ti83b>1→A:1/sqrt(2)→G
|+ Texas Instruments SR-56 Program Listing for "Arithmetic-geometric mean"
While abs(A-G)>e-15
|-
(A+G)/2→B
! Display !! Key !! Display !! Key !! Display !! Key !! Display !! Key
sqrt(AG)→G:B→A
|-
End
| 00 33 || STO || 25 03 || 3 || 50 || || 75 ||
A</lang>
|-
| 01 02 || 2 || 26 12 || INV || 51 || || 76 ||
|-
| 02 32 || x<>t || 27 44 || EE || 52 || || 77 ||
|-
| 03 64 || × || 28 41 || R/S || 53 || || 78 ||
|-
| 04 32 || x<>t || 29 || || 54 || || 79 ||
|-
| 05 94 || = || 30 || || 55 || || 80 ||
|-
| 06 48 || *√x || 31 || || 56 || || 81 ||
|-
| 07 32 || x<>t || 32 || || 57 || || 82 ||
|-
| 08 84 || + || 33 || || 58 || || 83 ||
|-
| 09 34 || RCL || 34 || || 59 || || 84 ||
|-
| 10 02 || 2 || 35 || || 60 || || 85 ||
|-
| 11 94 || = || 36 || || 61 || || 86 ||
|-
| 12 54 || ÷ || 37 || || 62 || || 87 ||
|-
| 13 02 || 2 || 38 || || 63 || || 88 ||
|-
| 14 94 || = || 39 || || 64 || || 89 ||
|-
| 15 33 || STO || 40 || || 65 || || 90 ||
|-
| 16 02 || 2 || 41 || || 66 || || 91 ||
|-
| 17 44 || EE || 42 || || 67 || || 92 ||
|-
| 18 94 || = || 43 || || 68 || || 93 ||
|-
| 19 32 || x<>t || 44 || || 69 || || 94 ||
|-
| 20 44 || EE || 45 || || 70 || || 95 ||
|-
| 21 94 || = || 46 || || 71 || || 96 ||
|-
| 22 12 || INV || 47 || || 72 || || 97 ||
|-
| 23 37 || *x=t || 48 || || 73 || || 98 ||
|-
| 24 00 || 0 || 49 || || 74 || || 99 ||
|}
 
Asterisk denotes 2nd function key.
 
{| class="wikitable"
|+ Register allocation
|-
| 0: Unused || 1: Unused || 2: Previous Term || 3: Unused || 4: Unused
|-
| 5: Unused || 6: Unused || 7: Unused || 8: Unused || 9: Unused
|}
 
Annotated listing:
<syntaxhighlight lang="text">
STO 2 x<>t // x := term a, t := R2 := term g
× x<>t = √x // Calculate term g'
x<>t + RCL 2 = / 2 = STO 2 // Calculate term a'
EE = x<>t EE = // Round terms to ten digits
INV x=t 0 3 // Loop if unequal
INV EE // Exit scientific notation
R/S // End
</syntaxhighlight>
 
'''Usage:'''
 
Enter term a, press x<>t, then enter term g. Finally, press RST R/S to run the program.
 
{{in}}
 
<pre>
1 x<>t 2 √x 1/x RST R/S
</pre>
 
{{out}}
 
<pre>.8472130848</pre>
<pre>
.8472130848
</pre>
 
=={{header|UNIX Shell}}==
{{works with|ksh93}}
ksh is one of the few unix shells that can do floating point arithmetic (bash does not).
<langsyntaxhighlight lang="bash">function agm {
float a=$1 g=$2 eps=${3:-1e-11} tmp
while (( abs(a-g) > eps )); do
Line 3,019 ⟶ 3,490:
}
 
agm $((1/sqrt(2))) 1</langsyntaxhighlight>
 
{{output}}
Line 3,030 ⟶ 3,501:
0.8472130848</pre>
 
You can get a more approximate convergence by changing the while condition to compare the numbers as strings: change <langsyntaxhighlight lang="bash">while (( abs(a-g) > eps ))</langsyntaxhighlight> to <langsyntaxhighlight lang="bash">while [[ $a != $g ]]</langsyntaxhighlight>
 
=={{header|VBAV (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import math
<lang vb>Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub</lang>{{out}}
<pre> 0,853553390593274
0,847224902923494
0,847213084835193
0,847213084793979
0,847213084793979 </pre>
 
=={{header|VBScript}}==
{{trans|BBC BASIC}}
<lang vb>
Function agm(a,g)
Do Until a = tmp_a
tmp_a = a
a = (a + g)/2
g = Sqr(tmp_a * g)
Loop
agm = a
End Function
 
WScript.Echo agm(1,1/Sqr(2))
</lang>
 
{{Out}}
<pre>0.847213084793979</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
===Double, Decimal Versions===
<lang vbnet>Imports System.Math
Imports System.Console
 
Module Module1
 
Function CalcAGM(ByVal a As Double, ByVal b As Double) As Double
Dim c As Double, d As Double = 0, ld As Double = 1
While ld <> d : c = a : a = (a + b) / 2 : b = Sqrt(c * b)
ld = d : d = a - b : End While : Return b
End Function
 
Function DecSqRoot(ByVal v As Decimal) As Decimal
Dim r As Decimal = CDec(Sqrt(CDbl(v))), t As Decimal = 0, d As Decimal = 0, ld As Decimal = 1
While ld <> d : t = v / r : r = (r + t) / 2
ld = d : d = t - r : End While : Return t
End Function
 
Function CalcAGM(ByVal a As Decimal, ByVal b As Decimal) As Decimal
Dim c As Decimal, d As Decimal = 0, ld As Decimal = 1
While ld <> d : c = a : a = (a + b) / 2 : b = DecSqRoot(c * b)
ld = d : d = a - b : End While : Return b
End Function
 
Sub Main(ByVal args As String())
WriteLine("Double result: {0}", CalcAGM(1.0, DecSqRoot(0.5)))
WriteLine("Decimal result: {0}", CalcAGM(1D, DecSqRoot(0.5D)))
If System.Diagnostics.Debugger.IsAttached Then ReadKey()
End Sub
 
End Module</lang>
{{out}}
<pre>Double result: 0.847213084793979
Decimal result: 0.8472130847939790866064991235</pre>
 
===System.Numerics===
{{trans|C#}}
{{Libheader|System.Numerics}}
<lang vbnet>Imports System.Math
Imports System.Console
Imports BI = System.Numerics.BigInteger
const ep = 1e-14
Module Module1
fn agm(aa f64, gg f64) f64 {
Function BIP(ByVal leadDig As Char, ByVal numDigs As Integer) As BI
mut a, mut g := aa, gg
BIP = BI.Parse(leadDig & New String("0"c, numDigs))
for math.abs(a-g) > math.abs(a)*ep {
End Function
t := a
a, g = (a+g)*.5, math.sqrt(t*g)
}
return a
}
fn main() {
Function IntSqRoot(ByVal v As BI, ByVal res As BI) As BI ' res is the initial guess of the square root
println(agm(1.0, 1.0/math.sqrt2))
Dim d As BI = 0, dl As BI = 1
}</syntaxhighlight>
While dl <> d : IntSqRoot = v / res : res = (res + IntSqRoot) / 2
Using standard math module
dl = d : d = IntSqRoot - res : End While
<syntaxhighlight lang="vlang">import math.stats
End Function
import math
 
Function CalcByAGM(ByVal digits As Integer) As BI
fn main() {
Dim a As BI = BIP("1"c, digits), ' value is 1, extended to required number of digits
println(stats.geometric_mean<f64>([1.0, 1.0/math.sqrt2]))
c as BI, ' a temporary variable for swapping a and b
}</syntaxhighlight>
diff As BI = 0, ldiff As BI = 1 ' difference of a and b, last difference
CalcByAGM = BI.Parse(String.Format("{0:0.00000000000000000}", ' initial value of square root of 0.5
Sqrt(0.5)).Substring(2) & New String("0"c, digits - 17))
CalcByAGM = IntSqRoot(BIP("5"c, (digits << 1) - 1), CalcByAGM) ' value is now the square root of 0.5
While ldiff <> diff : c = a : a = (a + CalcByAGM) >> 1 : CalcByAGM = IntSqRoot(c * CalcByAGM, a)
ldiff = diff : diff = a - CalcByAGM : End While
End Function
Sub Main(ByVal args As String())
Dim digits As Integer = 25000
If args.Length > 0 Then Integer.TryParse(args(0), digits) : _
If digits < 1 OrElse digits > 999999 Then digits = 25000
WriteLine("0.{0}", CalcByAGM(digits))
If System.Diagnostics.Debugger.IsAttached Then ReadKey()
End Sub
End Module</lang>
{{out}}
<pre>0.8408964152537145
<pre style="height:64ex; overflow:scroll; white-space: pre-wrap;">0.8472130847939790866064991234821916364814459103269421850605793726597340048341347597232002939946112299421222856252334109630979626658308710596997136359833842511763268142890603897067686016166500482811887218977133094117674620199443929629021672891944995072316778973468639476066710579805578521731403493983042004221192160398395535950981936412937163406460295999679705994343516020318426487569502421748638554059819545816017424178878541927588041627190120855876856483268341404312184008040358092045594943138778151209265222545743971242868207663409547336745996217926655353486256861185433086262872872875630108355631935706687147856390889821151088363521476969796126218329432284178681137684451700181460219136940270209459966835135963278808042743454817445873632200251539529362658066141983656164916262596074347237066169023530800173753128478525584306319074542749341526857906552694060031475910203327467196861247963255105546489028208552974396512499400966255286606758044873538921857014011677169765350140849524768489932573213370289846689391946658618737529663875622660459147770442046810892565844083803204091061900315370673411959410100747433105990550582052432600995169279241747821697678106168369771411073927334392155014302200708736736596227214925877619285105238036702689046390962190766364423553808590294523406519001334234510583834171218051425500392370111132541114461262890625413355052664365359582455215629339751825147065013464104705697935568130660632937334503871097709729487591717901581732028157828848714993134081549334236779704471278593761859508514667736455467920161593422399714298407078888227903265675159652843581779572728480835648996350440414073422611018338354697596266333042208499985230074270393027724347497971797326455254654301983169496846109869074390506801376611925291977093844129970701588949316666116199459226501131118396635250253056164643158720845452298877547517727274765672164898291823923889520720764283971088470596035692199292183190154814128076659269829446445714923966632997307581390495762243896242317520950731901842446244237098642728114951118082282605386248461767518014098312749725765198375649235690280021617490553142720815343954059556357637112728165705973733744297003905604015638866307222570038923015911237696012158008177907786335124086243107357158376592650454665278733787444483440631024475703968125545398226643035341641303561380163416557526558975294452116687345122019122746673319157124076375382110696814107692639007483317574339675231966033086497357138387419609898383220288269488219130281936694995442224069727616862136951165783888501219909616065545461154325314816424933269479700415949147632311292059351651899794335004597628821729262591808940550843146639378254833513955019065337087206206402407705607584879649984365159272826453442863661541914258577710675618501727803328717519518930503180550524542602233552290077141812879865435118791800635627959362476826778641224946033812608262825409889531252767753465624327921451122955551603181843313369296172304178385515712556740498341666592696958000895372457305769454227537216020968719147039887846636724326270619112707171659082464004167994112040565710364083000241929439855307399465653967781049270105541035951333943219992506667620207839469555376055179640100974921885631130101781388857879381317209594806253920130098365028791769582798590527994772194179799702494306215841946888532811549772157996019440962347768614408507573928429882375939682322367058033413477462311289762585932437663177897491107726190970448952220450963072551559009382490402136480779203476721504856844602255440999282616317431264228578762898338065072202301037175314926350463106018857377256700661838129058063895450812703131137104371613583348806583395543121790134839883321641305763524471251153947206667033010134871651632411382881763983962952612114126321979596509865678675525076076042409590751752302194610453256433324961490125353332922372386894812788502013596630537605584935892839163046940388785496002747148719780145765957904958580226006609952496736432496683346176010660815670697514238186650361083885220976165500251607311499216129477579019972924868963822060380876027628167237016681910663358577515465038133423672234764202655856558846416010210540489855618711473588497637840648642679818650448631907747038228671143515112300360708657429886477146674733750114345818852797006056211724692174847180694866251199472893444270378304620707354938052872720621560630718828685805645211106967080285699069825769177220998671959968507790681443494932804976811543680463259938693076235070999518295129581121235707245383354826190752395158273098248180549665897909168867984071707793705959045775840910473413109604194111357756620727337797833203797301137672658535747710279781409721309612142393854737462769615041307952837372882050658719152259765084027796991761175393006725492491229845082362975568722711065849435533850494532638736489804606655979954360169503092790092450057856477235876198848986034412195340795369002996411974549060741600978859537660722905160772428590070901156639138364299041220826769629797867649032356499981990765997439870548648769091024911927099968275697011368762244046402960383700066212734577664709711326374656811502985863032260337383421358423937896114617192083071953915643782093641496780334152464507396683173198363362743392555311712019454146844880895622417898031894341231284027858378289009624209541345002101072736323285272576209646851994468240550629391742053301706461917215178844296705314335503772310709716080285145314144106105023117310877779933248932087727229897821330120834074305604998159963202687793307156940302439156118926767517249511766526248547096041991473113657920697330996088897286789780735587578500623575157123771653042063631002703129296694025421967877168846655727580898306467662007014679585693082220620905330827782226503112520278733512519159918893900284319218166686548434879621972211763904959895793607330943697457628943200384117552941594754747183936381144125610351023459581080768558985657007445308909428669251190101718122826689349269528261052518556736045877702288147821446968500918347219741420546128072347950059811766364526150190788545471193803557145930744635656260752787518824386409506964649815131170591457990619376560858650175616864501924098327235724333688813080022186368700209641119724303603558649793773314916749593151188673535025505982303047060284740458456676849620934506396302909441632516408692889814507247877727673378033828929504978384342943766566737297587430575141036417476861639624198941904730996100228428079444920026904845254139188246001559089131943255610365769362364161784646693141456109984038312265504115251494445380042090428718182468431624610552637677520970104063944687837375017436089751693486887651283453677552786547090231542029453873076141196649767521919808902105772633472397958968722923357769041244458682297806209887089816018179521454920370956252850733023255060096611329479148443416687429872654204083552056456404421174124065041932362831296643126330768715450444950733554418200793669701331244638824360062439816712409346806322169771701563590417609841261977801052586956634654144702511135382841010278579543061802357275500930513955637771043922799597114118278203358118398952338720119626666828781215343331193353019800652511924103594315072427251589774226901431325149775220621148653209528291784172678852791825950189428306645453380829438548491390660090152646315666940813051689857738445716110134773528439558663918031477128997248977232695083095920860316390860179422146804892537147135669490647597566350405076105930300153453613446834614136284840473063909580064862482211399539962122107992774053203059756987131501429238941821989218445861496845306346078287058864262560349767113385390753047360747520569725532663517964059488138127648519130232826129551720747594498863925111049785977410104647258831744969489273332281068408949475978706769012216951869658194406136694310323411619613160554381608728305543504819071159752742665917363693001980988797627218662628543311906086034280619151845297823703639898449414417889008602782220998390227472837967411429578924346545640402855167478372538831386154780508035236893583332887355879794886804980971406868936719416711504307402575102269081707385928535837390976424975922421061832372517021428320986753744507133218963666908565634963306077455683011837149400258404997766113525532847665618870592978212729899729592794781820428719807102278646183807006401083138975677112754136221127444534535584959769252575758312999039536959893249951324106784265611556743660088737484274038234811784911002123537108015334407708175281579422928548731689863980071896268684985779061942582000173178473797975815609269087287850270024414741281953578873964745859459899535543412801653553049058528794674398220606230386688852700505218904927782197514115595435549125326115087432280435609563176116321811794164884206928474315699133677787956913705592704959893911100786224112449931719539890308215307126971807352814294437374058180589784287101566325873726600012296180403780429093175160473979931236882466314524590792512088916974765430245705320638670468411054034201437664442213212750799846299157010147106552946146746392249574530619682203425444816247545977269653430250686824205288099692448923652171403817749282935917315481284919621433304080904306867233682060716291289398517406255904282247558159509102324206160816363511440953267967974466214658121897383725705201831800678505181233270743236051760236565304605919728246762046497950757124332306210615236617229324468286251110577832854712371857906482302429199129753477340618812393224405123793229248698239302094605799468502209356458018864737205798950819968285087908120645175464792846657029993496146354533816989879012073959534299458051884682918835631136138879631316173442207506218212945047503433730640140356614106403320867621443183928438969994268286836082535591242751488383392264668222963323657488981599104902374571278077062853236895690028469742954774248422335523859049299225453318270693966088603518491166875108552006265340966412611220069290556369052744064893640087015171662929356529921474420793873710647399136453402185931518201576110059405556600166318190916348212818643068418256991194316266715898588673650488980580832972145195811525832974358064432698289209364284959616975339927502383832695801109608954786457256109785378297307074918168744735731189049849490781632210127110919398357638892753131749978321368280932894349330930087868884127092076359007648065118301317440813138170776478562086983456849957696333241556699085937149528437303782174166781012624737754844959408277598042857813775448446192929537153359741871355556678028606484917974827559022377376189703770332489774349235376523557139076431488967144133099539679871046284747721772185865851985971282165739148574494328320308464163956096301047370473988450307936956928683464113764226308568695688152053749196294562881085987015910764955019272667378276517237450013662421051146709184898952269727656206976263055094938932099216377529415335060027109430018977339221845390337351007942764665232509045377940478212355620488638969640291029182673024368888013982750049655688955540362739754118359277009094291839958396298535952123465573707751680432023872401008786292362558484920221296055948232317635214207117650427699747801290249150914873347204981208353486521246233538858471700470120592394582541522312967601307268280232044633644234100026474341568399123881048049819491200940244895720301881220640996997340843736095812449945913231793359333819197360248853375641030435643732302001328359990615298394916710687997693926699033522064083729586994304357670917169796698442332656830732550000321312902706719106342428311390049478179307304556219943912072209495471916547109605404919944186051724981471812994063119290173738101176617356976495636675620278895592099504686163440305250658681735840269428736633431167832903837475658050990783985384926064721246565130660487673608585790218386643241627198210378772796337736742692945663985470529377745854692207002046330357343505517537014050310355526578082729897049230547545589009275410944504014157125357682801074915174627928533783099570631952876838237806368177841661186334747789420166190186143388804514884174361681454810362321037643274595653364629397295294049952661691181657740018116146497654407589150912557599100855273107733703213603505619407350405223414533224306604743600257212590127202517146952605462439215815151732661454812243619860357386922465403688559787750083268386930674253759349376972691382532780570135683441862315010318955128705494038594760949278590520009881447715839714713971813720554960331191642239195313230213875992717401904622413925914800620171561815889352945121978193704745708538695427900233080410588007250947512318930796844637224171170594606197614751977323896101315556406372309310279476973938229476346893933755946893665094049910252612163538072005644241026471164639800490998535570282059396054554479255558624918709232180130454102936332893619326596350851413637207293142767763267817840066780089558654877782630822818446508158509625695020697797889664140551101421185533444015948880284701657904464926309216120238068566472631611326995533585414320547442896728173291714010643730593960222482733969720865809194288803963344344876467583385597351333330628439786357062196382217705500672607607570202305548328439335937369624085404957344415141889143812206076832329063384332685935928226648361622876815670931303789678327741487845287838232474038340893449427806045589018183673133602271167285304427194507315740913600066356089181219040305019319028163972135790696025211929562455952835850442627787993214468221041325612271290302469610374855134599106662606082143546126463790846952338680559237822828610361386416013753920426888371192602742087474507782730180882648297991489233434653363930327991816476995529468892904060335470265188317825821391915073117022336839564945335630414192442838503954209073337511117053790819768061378846157004292392264788138228486672543415580694421193506836000488465561599083339184724263183698928130695654949153165010313216361224018298711517222401523368101476246169896417259748838727189598765602350324828709741468793415378708814573190327920453219231685852735108372055942456601545647944675449566859142997988233179819059574125368681032194798082603876241044848730208905065871934264174092007936669883601462309762759844113071525758916288010581709353072588887654386253201848624931923638568216562603110434528313030704972291334873033240933736956347974889824930017415805659182123288343858101250171537305398462043432455721482088547523494730467761429282915391485852688505423074450548192619166975975031503447208211845313907683486006908772752077246485706597636740936173143436990399498908375710246545650814962015988805204483379491707040848303909417512426275869868668644293498242419667403627076032399201407183071270759837132000712447159523642782162488472933913713634046138974088894178399320090051543608421618891328957740354384456107645016010462709579098652495342014766016330458293537653454523438667413798731255017029554582809547897542497367109038598264606895622241257303208140890607025206140457815282368504505765710043804228592032720729190222134651835930255942940875306994701101153416476785623543575023993736414532895773499876167502240919794121893188059017977444329403624038551082491954751841177014150820554999148803286500065069030165028455616533514890711974194172310029663247936640825364542104897640445108081123906368188594908660418340025631562661211506365309297219580687177632051461355581309500814563826112416521487163593643553646268872746276680368630680088231249970572706496265335285424273723449757482776061300818063419639083097882249478922949525891665782610044424440110326748539620120023397129834624242363283711074267309902126029110038109050751840523266273905031934856015485510632624318778970878895198168073096354223096005536267735905099473408744371024816727970009494589707630185344952680106730984246828848883760016695887137355969244555238536396178788134209309376484848406842940499731494663578455826688245825356635393289729316700066238128368519670627697889769929009597838069557440769080950069594659578325366066060213000525012998145215099629307110700615796004759918829827472751877492472674770755413679265775060149528336859838085353420874215682758801259992855903410097963019943741001394975591822918846705741010634931594527954742032057295356596869586863097328488381174243827058441735659667485315202886191192125286398739560928127513223214119754229343092375569339614672740517569529376699061052365448344078610425576694541873486379356070861240473688356773437140126350120823765176390562050604076894729400293162079760342896846897639867830553941515230713725560502914671175123451932131962571791940911728951123948113598860588062424037835751996487088330150679210175429060531418836978611027896830689666851868410470182364780700615529883149883111601949965815038674390467105247175993726709203381051984777006122752302698038537619917731907133105816779008651480172440446403764720673784583395382889380902941273987910475254258486561698048543296782281040453997661165123290729161619992628751086519341731116513305659182981762584769428708454819029344222186027977405519291266188948708010515922860149238393490889782166965109499761673179583522105791358724355029782111425280584380959770472177893827382916471882671437865821461326011263516554280516418422188264141890686619186492751718984735037496602686033671961304915922609442146773092074476794711917820209913226872184947548378003848726148872742881265579174794634151444545105599464567614478293387968015412886418098284885525959617399177657635267081989985408930744564199296902459275405143647525648661932959903068323866757518479741015342911416508753572892479684280248440220211898390243430190746592470563991910024225814399068391457857458095344096826158489731615822039837691005171654390590093326827586419753439483771905973079465029210363641972615923872187876095687197681934481955852567024141433671590889694204781798936556351775101591005026585947279448642317311892727153525046034081896227383114600546852406398855471859684088277722162250586368419379964112646321070639818773794369650252104438622320671517228411475433482803041707675438555447584321271846396281391925884972509051040944134450429845346071848875654240709690138592611645519676563708429710676494635766201285381926791204110977805857352062737510466943591592074904378966129808716274322385039032007477854211063899544954185997641428116395197239708078986048758264126544825149923227286176571389697334537835963603962709038002668921324389159009375225033651171937770657226295341257068980907793198879997076783263303670667342657925395849950582363998610492878479976185891384024744790742355981796013254960652684988733518397287191251899388324341602608356164496670902390042273216221931567939944001215159910054381084520081133103207553492484487369268314444466610780275891777468369344585045949963237156043800258227618908603074550819931892899703285549507330240121766349515315827830897786432254556221744305752825143708087184314470811004510108612122699931396969361066523608721126359012344828262284427191281973187269761974740398071778378188160519801862257232970224762494767912932684020188061795236229174601398576604233579094407723017353015337974435643738584248250538061547193075224429309117207447677149522141919390974201716026970557825836923707297811545552570788004955666915477901830719591663516687057984336951611189153751912396714116378197000784953115386326766369269172016978409040396969804861828436417776804088449208439901095951205751340861060375353408155737087188313898337656322533650946010308686111901241541794900659835366926383515058402026098259570385429145865025692157987309807064597082326377138235585737704225628144262793497769429358804020882742028263786443615935817930817858306265712263479452174065216410798029333573961137404301928294367884626832432449078812684787281988676202931062510264948586549463964789154366240635570346688477784815271412470430646040615614277320107003575855033995279377529716156628381118518085523414187577256025217995103662771477552291036839539792329375184700131215428652464111526297830742328651189481978920924682746392250346179819781021313400022272303222234731521016033826145645816472110340883197207109422849637006090510260943044730126801795349152894613046101033061811314821366141874985466628809585678299308824993966655499624380015821082410781190328189506855057581990908848597095494573176672201417764187253816862426293852974092626551536758155537683368451820154793964862810533857810979434793077956125541240828563089647076354827276586047900779183041806574320855302776686899978897939486987950729652971448050889517660684386673056662911929857913206598752762097197279390208473846210277152094212386266930256260451209117402079233658157593274696841906354187366092529138116574357045728290417433832596884391356956442617823006949118156994294295529170211353842468704890572313005646106202029653246628477843902025194715815133791174898257040115532858624973690714844800747184719290671002133191274834310662201874141841328708920709275866745037664169280121112867057832132585948539987132879098472640550013972043153470930436509718084070853723316111111611632600262171748813737621046013600544051850633175245231989785291065646466038278748870331134307620041356514295482843502245454400571392386492526283423907951705366640483826875013469850263767974528926285288366544314868036628329638912254207094687335597669512007687507292940623176435604796651807847095408991068514998003358735387989422028901542800717906482276185298683079286137204396993726503610285463352157718364571843381950031926272352293654343387522809514152498052577486366048613580539162662183475105825647260311633442002377527140625112075332294909525522330744664115572260242435895269482927435844022622001466247093866533879048392320516224276433339282642640953964341822416705658461244760448817737705782669080880834418822622611342632727419248415651121035047131961583094994438779439078380664656207143187309895280874153167621657602227990850199615587578332393883365169478142077533262283694526612005465820771400826060398839255150948861553177333447506822679211849690448880479070102043288205874672361672971246062341973369704807867768609989464712379097525706498042381815865399434983035941162258347729020489356838477197804973214911448748749915616679253857438010864500220134843719609727912761136925035123155282535741655826107266099467657016111855684257826878422197833994329148734893923892153298966294232703135845615804723993624827409373966761563257981994036006655039613941881183164267144485664874468348587099434743710128859267552473831462181434321232124758618476925803128913233878664527525204324484796532776273320171351979849530142473805976430318655810403609897537469226336015596525652284888167037460054235043655813438329870872734142062859147847007274999414885129441657918212383876056572545671794085637289277002790218604788423519924573051811976377731594412994393860534559159658127123862955315918182841923881357245009246238507097741891437575676886206936433608263660374355173185026954239766173038826275043838965247160428689739548061640664606565379050539422795708801840829664956978192406737307076253014257542221763860230431809477056758905681723033326311408802886092880151777469082375063137750925275331638009836786645991949881018108222446858443984865972449621097999331605268587810061927125889694400669979755648800940895626242917531834388920035663113368763931463847812763130237825562198311791061780856687903309789539747505239545316630638169559777653347655949908779202359718666623572487055558216484036084925217803431104356647417600193631613474196113126657206064282217690428541246560204561459484317744683213906021267727411189443675804442911583757423572500214191467493342871160840582639470485636370375679604797073490813681083838562113841391587052553615073991983125473434527404596547926972539542447555990332809716643578039646945749813368621152410490288581779206318208255069166455507840899628333174744873951607229399258854694188637978240144635295264982572856632103053550891057171748674115218494774077589151115819489068851971959768129214023511454382738867557288320426608338030759515727545577639726238470674634011626346953231815229549718996906470438903536574430644436472716449550868519871817092814068746449470806856174570885105064766494332205391085097539987897980672278869943134632799032372604933150163386774039430519493297142505321117669011820293604482694166301309801111227443654953271242388534939973277749999335296667138307969441135719079969506099821923206878892624416110175909254904610286553512032488285673735148429324009831633211264460376172046209384270528903772251057643968938983722779640468452705694321085455273829462711022737243290606294601651732654594463569861350966095209962038508010899673666470073918705760679801337058347046567503369379598928154437380765511031719081985901371088639600700705631873099251480947989238619052479230983309717938226245725600119571130722386790431255742179135633111146646083268382596762356018472772209198013121983224179079476134977421748168833934278876403014334318798493417716613256506422668264638388429786875443810986754386459491846082078633346046469418429778813833857755519670005669840456587642130852057050148314568259387702428619224671173187370822224627538313365937868201435535126600146246249435880806572693573084485615073901842761167215162204840459913839674251648</pre>
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
<langsyntaxhighlight ecmascriptlang="wren">var eps = 1e-14
 
var agm = Fn.new { |a, g|
Line 3,161 ⟶ 3,544:
}
 
System.print(agm.call(1, 1/2.sqrt))</langsyntaxhighlight>
 
{{out}}
Line 3,169 ⟶ 3,552:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codesi;
real A, A1, G;
[Format(0, 16);
Line 3,178 ⟶ 3,561:
RlOut(0, A); RlOut(0, G); RlOut(0, A-G); CrLf(0);
until A=G;
]</langsyntaxhighlight>
 
Output:
Line 3,190 ⟶ 3,573:
=={{header|zkl}}==
{{trans|XPL0}}
<langsyntaxhighlight lang="zkl">a:=1.0; g:=1.0/(2.0).sqrt();
while(not a.closeTo(g,1.0e-15)){
a1:=(a+g)/2.0; g=(a*g).sqrt(); a=a1;
println(a," ",g," ",a-g);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,203 ⟶ 3,586:
</pre>
Or, using tail recursion
<langsyntaxhighlight lang="zkl">fcn(a=1.0, g=1.0/(2.0).sqrt()){ println(a," ",g," ",a-g);
if(a.closeTo(g,1.0e-15)) return(a) else return(self.fcn((a+g)/2.0, (a*g).sqrt()));
}()</langsyntaxhighlight>
{{out}}
<pre>
Line 3,214 ⟶ 3,597:
0.847213 0.847213 1.11022e-16
</pre>
 
=={{header|ZX Spectrum Basic}}==
{{trans|ERRE}}
<lang zxbasic>10 LET a=1: LET g=1/SQR 2
20 LET ta=a
30 LET a=(a+g)/2
40 LET g=SQR (ta*g)
50 IF a<ta THEN GO TO 20
60 PRINT a
</lang>
{{out}}
<pre>0.84721309</pre>
Anonymous user