Arithmetic-geometric mean: Difference between revisions

no edit summary
(Arithmetic-geometric mean in Chipmunk Basic)
imported>Lacika7
No edit summary
 
(15 intermediate revisions by 9 users not shown)
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;
Line 397:
 
=={{header|BASIC}}==
==={{header|ANSI BASIC}}===
{{works with|QBasicDecimal BASIC}}
<syntaxhighlight lang="qbasicbasic">PRINT100 AGM(1,PROGRAM 1 / SQR(2))ArithmeticGeometricMean
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</syntaxhighlight>
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}}===
Line 432 ⟶ 436:
end function</syntaxhighlight>
{{out}}
<pre>0.84721308479</pre>
 
0.84721308479
==={{header|BBC BASIC}}===
</pre>
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> *FLOAT 64
@% = &1010
PRINT FNagm(1, 1/SQR(2))
END
DEF FNagm(a,g)
LOCAL ta
REPEAT
ta = a
a = (a+g)/2
g = SQR(ta*g)
UNTIL a = ta
= a
</syntaxhighlight>
{{out}}
<pre>0.8472130847939792</pre>
 
==={{header|Chipmunk Basic}}===
Line 465 ⟶ 486:
140 RETURN</syntaxhighlight>
 
==={{header|BBCCraft BASICBasic}}===
<syntaxhighlight lang="basic">let a = 1
{{works with|BBC BASIC for Windows}}
let g = 1 / sqrt(2)
<syntaxhighlight lang="bbcbasic"> *FLOAT 64
 
@% = &1010
do
PRINT FNagm(1, 1/SQR(2))
 
END
let t = (a + g) / 2
let g = sqrt(a * DEF FNagm(a,g)
let x = a
LOCAL ta
let a = t
REPEAT
let tat = ax
 
a = (a+g)/2
loopuntil a = t
g = SQR(ta*g)
 
UNTIL a = ta
print a</syntaxhighlight>
= a
{{out| Output}}
</syntaxhighlight>
<pre>0.85</pre>
Produces this output:
 
<pre>
==={{header|FreeBASIC}}===
0.8472130847939792
<syntaxhighlight lang="freebasic">' version 16-09-2015
</pre>
' 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}}===
Line 505 ⟶ 575:
160 LET AGM=A
170 END DEF</syntaxhighlight>
 
==={{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}}===
Line 519 ⟶ 734:
LET AGM = a
END FUNCTION
 
 
PRINT AGM(1, 1 / SQR(2))
END</syntaxhighlight>
{{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}}==
Line 996 ⟶ 1,352:
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}}==
Line 1,130 ⟶ 1,515:
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
end</syntaxhighlight>
 
=={{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|Futhark}}==
Line 1,645 ⟶ 2,000:
0.8472130847939792
</pre>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight 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
</syntaxhighlight>
 
=={{header|LiveCode}}==
Line 2,437 ⟶ 2,774:
Res = 0.8472130847939792.
</syntaxhighlight>
 
=={{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>
 
0.8472130847939792
 
=={{header|Python}}==
Line 2,600 ⟶ 2,918:
 
say agm 1, 1/sqrt 2;</syntaxhighlight>
 
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}}==
Line 2,708 ⟶ 3,035:
return gn
</syntaxhighlight>
 
=={{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}}==
Line 2,760 ⟶ 3,102:
0.8472130847939790866064991234821916364814459103269421850605793726597340048341347597231986723114767413E0
</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>Output:
<pre>0.847213085
0.847213085
0.8472130847939791165772005376</pre>
 
=={{header|Rust}}==
 
<syntaxhighlight lang="rust">// Accepts two command line arguments
// cargo run --name agm arg1 arg2
Line 2,919 ⟶ 3,242:
{{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.
<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|Smalltalk}}==
Line 3,173 ⟶ 3,475:
.8472130848
</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|UNIX Shell}}==
Line 3,210 ⟶ 3,502:
 
You can get a more approximate convergence by changing the while condition to compare the numbers as strings: change <syntaxhighlight lang="bash">while (( abs(a-g) > eps ))</syntaxhighlight> to <syntaxhighlight lang="bash">while [[ $a != $g ]]</syntaxhighlight>
 
=={{header|VBA}}==
<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|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|V (Vlang)}}==
Line 3,357 ⟶ 3,533:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">var eps = 1e-14
 
var agm = Fn.new { |a, g|
Line 3,421 ⟶ 3,597:
0.847213 0.847213 1.11022e-16
</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>
Anonymous user