Golden ratio/Convergence: Difference between revisions

Restored FreeTom|'s Lua sample an tweaked it to itterate to the task's specified limit
(Removed Lua version)
Tag: Manual revert
(Restored FreeTom|'s Lua sample an tweaked it to itterate to the task's specified limit)
Line 1,166:
Number of iterations : 61
Error (approx) : 2.722811719173566624681571006109388407808876983723400587864054809516456794284321e-26
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">local phi, phi0, expected, iters = 1, 0, (1 + math.sqrt(5)) / 2, 0
repeat
phi0, phi = phi, 1 + 1 / phi
iters = iters + 1
until math.abs(phi0 - phi) < 1e-5
io.write( "after ", iters, " iterations, phi = ", phi )
io.write( ", expected value: ", expected, ", diff: ", math.abs( expected - phi ), "\n" )
</syntaxhighlight>
{{out}}
<pre>after 14 iterations, phi = 1.6180327868852, expected value: 1.6180339887499, diff: 1.2018646491363e-06
</pre>
 
3,028

edits