mimiv89 has asked for the wisdom of the Perl Monks concerning the following question:
How can I create an anonymous subroutine that can calculate the sum of 1^2 + 2^2 + 3^2 + …. + n^2,(these are exponents) where n is passed by a reference, $p. so far I have this but don't know if this is correct or what i need to change/modify: please help
use CGI qw(:standard); print header; print start_html; print "<form action='$ENV{SCRIPT_NAME}' method='post'>"; print "Enter an integer: <input type='text' name='n'>"; print "<input type='submit' value='Submit'>"; print "</form>"; if (param) { $n = param('n'); $p = sub { $sum = 1**2+2**2+3**2+4**2+5**2+6**2+7**+$n**2; for ('$i=0; <= $n; $i++') { $sum += $i; } return $sum; }; print $p->($n); } print end_html;
I have no errors but I feel like my calculation is wrong the results is not what it should be, why?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Anonymous Subroutine
by BillKSmith (Monsignor) on Nov 27, 2017 at 04:34 UTC | |
|
Re: Anonymous Subroutine
by NetWallah (Canon) on Nov 27, 2017 at 02:28 UTC | |
by mimiv89 (Initiate) on Nov 27, 2017 at 03:07 UTC | |
|
Re: Anonymous Subroutine
by AnomalousMonk (Archbishop) on Nov 27, 2017 at 02:59 UTC |