Golden ratio/Convergence: Difference between revisions
Content added Content deleted
(Added solution for EDSAC) |
m (julia example) |
||
Line 1,118: | Line 1,118: | ||
Number of iterations : 14 |
Number of iterations : 14 |
||
Error (approx) : -0.00000120186465 |
Error (approx) : -0.00000120186465 |
||
</pre> |
|||
=={{header|Julia}}== |
|||
<syntaxhighlight lang="julia">function iterate_phi(limit::T) where T <: Real |
|||
phi, oldphi, iters = one(limit), one(limit), 0 |
|||
while true |
|||
phi = 1 + 1 / oldphi |
|||
iters += 1 |
|||
abs(phi - oldphi) <= limit && break |
|||
oldphi = phi |
|||
end |
|||
println("Final value of phi : $phi") |
|||
println("Number of iterations : $iters") |
|||
println("Error (approx) : $(phi - (1 + sqrt(T(5))) / 2)") |
|||
end |
|||
iterate_phi(1 / 10^5) |
|||
iterate_phi(1 / big(10)^25) |
|||
</syntaxhighlight>{{out}} |
|||
<pre> |
|||
Final value of phi : 1.6180327868852458 |
|||
Number of iterations : 14 |
|||
Error (approx) : -1.2018646491362972e-6 |
|||
Final value of phi : 1.618033988749894848204586861593755309455975426621472923229332700794030300052916 |
|||
Number of iterations : 61 |
|||
Error (approx) : 2.722811719173566624681571006109388407808876983723400587864054809516456794284321e-26 |
|||
</pre> |
</pre> |
||