Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Anonymous Subroutine

by NetWallah (Canon)
on Nov 27, 2017 at 02:28 UTC ( #1204304=note: print w/replies, xml ) Need Help??


in reply to Anonymous Subroutine

use strict; use warnings; my $n = shift or die "Need 'n' as param"; my $anon_sub = sub{ my $count=shift @_; my $sum_sqares=0; for my $i(1..$count){ $sum_sqares += $i**2; } return $sum_sqares; }; # CLose anon sub print "N=$n, sum squares=", $anon_sub->($n),"\n";
Update: See BillKSmith's (++) More algebra-savvy, and faster solution below.

                All power corrupts, but we need electricity.

Replies are listed 'Best First'.
Re^2: Anonymous Subroutine
by mimiv89 (Initiate) on Nov 27, 2017 at 03:07 UTC

    Thank you so much this worked perfectly.

Log In?
Username:
Password:

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

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