Golden ratio/Convergence: Difference between revisions
Content added Content deleted
m (→{{header|Shen}}: Reformat.) |
(Added Hy) |
||
Line 690: | Line 690: | ||
Error (approx) : -0.00000120186465 |
Error (approx) : -0.00000120186465 |
||
</pre> |
</pre> |
||
=={{header|Hy}}== |
|||
{{trans|Scheme}} |
|||
<syntaxhighlight lang="hy"> |
|||
(import math) |
|||
(defn iterate [phi n] |
|||
(setv phi1 (+ 1.0 (/ 1.0 phi))) |
|||
(setv n1 (+ n 1)) |
|||
(if (<= (abs (- phi1 phi)) 1e-5) |
|||
[phi1 n1] |
|||
(iterate phi1 n1))) |
|||
(setv [phi n] (iterate 1.0 0)) |
|||
(print "Result:" phi "after" n "iterations") |
|||
(print "The error is approximately" |
|||
(- phi (* 0.5 (+ 1 (math.sqrt 5))))) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Result: 1.6180327868852458 after 14 iterations |
|||
The error is approximately -1.2018646491362972e-06</pre> |
|||
=={{header|Icon}}== |
=={{header|Icon}}== |