Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Anonymous Subroutine

by BillKSmith (Prior)
on Nov 27, 2017 at 04:34 UTC ( #1204309=note: print w/replies, xml ) Need Help??


in reply to Anonymous Subroutine

It is faster and less error prone to use the summation formula rather than doing the brute force summation.
use strict; use warnings; my $number = $ARGV[0]; my $sum_of_squares = sub{ my $n = shift; return $n * ($n + 1) * (2*$n +1) / 6; }; print $sum_of_squares->($number);

This formula is available in many places. I first found it at https://brilliant.org/wiki/sum-of-n-n2-or-n3/.

Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1204309]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2020-01-23 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?