Golden ratio/Convergence: Difference between revisions

Content added Content deleted
(Added Maxima.)
Line 792: Line 792:
<pre>1.6180
<pre>1.6180
11 iterations</pre>
11 iterations</pre>

=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
iterate(phi) := 1 + (1 / phi)$

phi0: 1$
phi1: iterate(phi0)$
for n: 1 step 1 while abs(phi1 - phi0) > 1e-5 do
block(phi0: phi1, phi1: iterate(phi0), iterations: n + 1)$

display (float(phi1))$
display (iterations)$
display (float(phi1 - (0.5 * (1 + sqrt(5)))))$</syntaxhighlight>

{{out}}
<pre>$ maxima -b golden_ratio_convergence.mac
Maxima 5.46.0 https://maxima.sourceforge.io
using Lisp SBCL 2.3.4
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) batch("golden_ratio_convergence.mac")

read and interpret /long-boring-pathname/golden_ratio_convergence.mac
(%i2) iterate(phi):=1+1/phi
(%i3) phi0:1
(%i4) phi1:iterate(phi0)
(%i5) for n while abs(phi1-phi0) > 1.0e-5 do
block(phi0:phi1,phi1:iterate(phi0),iterations:n+1)
(%i6) display(float(phi1))
987
float(---) = 1.618032786885246
610

(%i7) display(iterations)
iterations = 14

(%i8) display(float(phi1-0.5*(1+sqrt(5))))
987
float(--- - 0.5 (sqrt(5) + 1)) = - 1.201864648914253e-6
610

(%o9) /long-boring-pathname/golden_ratio_convergence.mac
</pre>


=={{header|Mercury}}==
=={{header|Mercury}}==