Sum of a series: Difference between revisions

Content deleted Content added
→‎{{header|Perl 6}}: change last one to an inverse square operator
→‎{{header|Perl 6}}: update works with
Line 847: Line 847:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
(Some of these work with [[rakudo]], and others with [[niecza]]. Eventually they'll all work everywhere...)
{{works with|Rakudo|#22 "Thousand Oaks"}}


In general, the <code>$n</code>th partial sum of a series whose terms are given by a unary function <code>&f</code> is
In general, the <code>$n</code>th partial sum of a series whose terms are given by a unary function <code>&f</code> is
Line 868: Line 868:
<lang perl6>constant @x = [\+] 0, { 1 / ++(state $n) ** 2 } ... *;
<lang perl6>constant @x = [\+] 0, { 1 / ++(state $n) ** 2 } ... *;
say @x[1000]; # prints 1.64393456668156</lang>
say @x[1000]; # prints 1.64393456668156</lang>
Note that even constants can be lazily generated in Perl 6, or this wouldn't work so well...
Note that infinite constant sequences can be lazily generated in Perl 6, or this wouldn't work so well...


Finally, if list comprehensions are your hammer, you can nail it this way. Though defining a inverse square superscript operator is obviously just showing off. <tt>:-)</tt>
Finally, if list comprehensions are your hammer, you can nail it this way. Though defining a inverse square superscript operator is obviously just showing off. <tt>:-)</tt>