Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Anonymous Subroutine

by NetWallah (Canon)
on Nov 27, 2017 at 02:28 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1204304]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-18 04:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found