in reply to How do I write a program that calculates the sum of all numbers to a number, n?

"We don't do homework for you..." (Anonymous Monk)

This ain't true, as you can see - but it may be the provided solutions don't meet your specs sometimes ;-)

So i feel free to put in my 2 ˘:

Edit: In German this sounds much better: Ich bin so frei, meinen Senf dazu zu geben.

#!/usr/bin/env perl use strict; use warnings; use List::Util qw(reduce); use feature qw (say); my $n = shift || die qq(Usage $0 [number]); say reduce { $a + $b } 1 .. $n; __END__

Please see also There's more than one way to do it as well as List::Util.

Best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

  • Comment on Re: How do I write a program that calculates the sum of all numbers to a number, n?
  • Download Code