Anonymous user
Arithmetic-geometric mean: Difference between revisions
no edit summary
imported>Schraf (Add "dc" version) |
imported>Lacika7 No edit summary |
||
(6 intermediate revisions by 5 users not shown) | |||
Line 412:
210 REM ********************
220 PRINT AGM(1, 1 / SQR(2))
230 END</syntaxhighlight>
{{out}}
<pre> .84721308479398 </pre>
==={{header|Applesoft BASIC}}===
Same code as [[#Commodore_BASIC|Commodore BASIC]]
The [[#BASIC|BASIC]] solution works without any changes.
==={{header|BASIC256}}===
Line 435 ⟶ 436:
end function</syntaxhighlight>
{{out}}
<pre>0.84721308479</pre>
==={{header|BBC BASIC}}===
Line 456 ⟶ 455:
</syntaxhighlight>
{{out}}
<pre>0.8472130847939792</pre>
==={{header|Chipmunk Basic}}===
Line 536 ⟶ 533:
{{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 576 ⟶ 595:
</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}}===
Line 598 ⟶ 638:
EndIf</syntaxhighlight>
{{out}}
<pre> 0.8472130847939792</pre>
==={{header|QuickBASIC}}===
Line 617 ⟶ 655:
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}}===
Line 687 ⟶ 738:
END</syntaxhighlight>
{{out}}
<pre>.84721308</pre>
==={{header|VBA}}===
Line 712 ⟶ 761:
==={{header|VBScript}}===
{{trans|BBC BASIC}}
<syntaxhighlight lang="vb">Function agm(a,g)
Do Until a = tmp_a
tmp_a = a
Line 722 ⟶ 770:
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>
Line 1,289 ⟶ 1,354:
=={{header|dc}}==
<syntaxhighlight lang="dc">>>>
?> 1 1 2 v /</syntaxhighlight>
Line 1,296 ⟶ 1,360:
<pre>
.8472130847939790866064991234821916364814459103269421850605793726597\
34004834134759723200293994611229942122285625233410963097962665830871\
05969971363598338425117632681428906038970676860161665004828118868
</pre>
You can change the precision (
=={{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 2,837 ⟶ 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 3,443 ⟶ 3,533:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="
var agm = Fn.new { |a, g|
|