Golden ratio/Convergence: Difference between revisions

Added Perl
m (→‎{{header|Raku}}: Capitalize language name)
(Added Perl)
Line 1,201:
Error (approx) : -0.000001201864649
Number of iterations: 14
</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl" line>
use strict; use warnings;
use constant phi => (1 + sqrt 5) / 2;
 
sub GR { my $n = shift; $n == 1 ? 2 : 1 + 1 / GR($n - 1) }
 
my $i;
while (++$i) {
my $dev = abs phi - GR($i);
(printf "%d iterations: %8.6f vs %8.6f (%8.6f)\n", $i, phi, GR($i), $dev and last) if $dev < 1e-5;
}
</syntaxhighlight>
{{out}}
<pre>
12 iterations: 1.618034 vs 1.618026 (0.000008)
</pre>
 
2,392

edits