The following calculates the sum efficiently (sum(1..N) = N(N+1)/2), yet finds a legitimately use for foreach:
foreach my $n (@ARGV) { print($n*($n+1)/2, "\n"); }
In reply to Re: How do I write a program that calculates the sum of all numbers to a number, n?
by ikegami
in thread How do I write a program that calculates the sum of all numbers to a number, n?
by fmny94
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |