Created
August 2, 2012 01:10
-
-
Save swuecho/3232140 to your computer and use it in GitHub Desktop.
plain sum of several serials
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 1+2+3+..+n | |
| sub sum_natural($n) { | |
| my ($total,$k)=0,1; | |
| while $k<=$n { | |
| $total=$total+$k; | |
| $k=$k+1; | |
| } | |
| return $total; | |
| } | |
| #1^3+2^3+..+n^3 | |
| sub sum_cube($n) { | |
| my ($total,$k)=0,1; | |
| while $k<=$n { | |
| $total=$total+$k**3; | |
| $k=$k+1; | |
| } | |
| return $total; | |
| } | |
| # | |
| sub sum_pi($n) { | |
| my ($total,$k)=0,1; | |
| while $k<=$n { | |
| $total=$total+8/($k*($k+2)); | |
| $k=$k+4; | |
| } | |
| return $total; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment