Arithmetic-geometric mean: Difference between revisions

Content added Content deleted
(→‎{{header|Raku}}: another solution)
(Added various BASIC dialects (Applesoft BASIC, Gambas, MSX Basic, Minimal BASIC, QuiteBASIC and Yabasic))
Line 412: Line 412:
210 REM ********************
210 REM ********************
220 PRINT AGM(1, 1 / SQR(2))
220 PRINT AGM(1, 1 / SQR(2))
230 END
230 END</syntaxhighlight>
</syntaxhighlight>
{{out}}
{{out}}
<pre> .84721308479398 </pre>
<pre>

.84721308479398
==={{header|Applesoft BASIC}}===
</pre>
Same code as [[#Commodore_BASIC|Commodore BASIC]]
The [[#BASIC|BASIC]] solution works without any changes.


==={{header|BASIC256}}===
==={{header|BASIC256}}===
Line 435: Line 436:
end function</syntaxhighlight>
end function</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>0.84721308479</pre>
0.84721308479
</pre>


==={{header|BBC BASIC}}===
==={{header|BBC BASIC}}===
Line 456: Line 455:
</syntaxhighlight>
</syntaxhighlight>
{{out}}
{{out}}
<pre>0.8472130847939792</pre>
<pre>
0.8472130847939792
</pre>


==={{header|Chipmunk Basic}}===
==={{header|Chipmunk Basic}}===
Line 536: Line 533:
{{out}}
{{out}}
<pre> 0.8472130847939792</pre>
<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}}===
==={{header|GW-BASIC}}===
Line 576: Line 595:
</syntaxhighlight>
</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>0.84721308
0.84721308479397904</pre>
0.84721308

0.84721308479397904
==={{header|Minimal BASIC}}===
</pre>
{{trans|Commodore 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}}===
==={{header|PureBasic}}===
Line 598: Line 637:
EndIf</syntaxhighlight>
EndIf</syntaxhighlight>
{{out}}
{{out}}
<pre> 0.8472130847939792</pre>
<pre>
0.8472130847939792
</pre>


==={{header|QuickBASIC}}===
==={{header|QuickBASIC}}===
Line 617: Line 654:
END FUNCTION</syntaxhighlight>
END FUNCTION</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>.8472131</pre>

.8472131
==={{header|Quite BASIC}}===
</pre>
{{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}}===
==={{header|Run BASIC}}===
Line 687: Line 737:
END</syntaxhighlight>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>.84721308</pre>
.84721308
</pre>


==={{header|VBA}}===
==={{header|VBA}}===
Line 712: Line 760:
==={{header|VBScript}}===
==={{header|VBScript}}===
{{trans|BBC BASIC}}
{{trans|BBC BASIC}}
<syntaxhighlight lang="vb">
<syntaxhighlight lang="vb">Function agm(a,g)
Function agm(a,g)
Do Until a = tmp_a
Do Until a = tmp_a
tmp_a = a
tmp_a = a
Line 722: Line 769:
End Function
End Function


WScript.Echo agm(1,1/Sqr(2))
WScript.Echo agm(1,1/Sqr(2))</syntaxhighlight>
</syntaxhighlight>
{{Out}}
{{Out}}
<pre>0.847213084793979</pre>
<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>