in reply to How do I write a program that calculates the sum of all numbers to a number, n?
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"); }
|
|---|