Tuesday, October 10, 2006

yeah me!

just wanted to let y'all know that i have solved my recent perl problem. it was the eureka moment of my life. mostly because i got no outside help until afterwards. it was, as it always seems to be, quite easy once i thought of it correctly. i'll put the code below in case you're interested....i also wrote another one to accept whatever number you would want to add up to. that's there too.
thanks for the links julie, i really appreciate it.
yay!

#!/usr/bin/perl
# add999.pl
use warnings;
use strict;

my $upto;
my $sum;

$upto = 999;

$sum = $upto * (($upto + 1) / 2);
print $sum, "\n";

AND

#!/usr/bin/perl
# addwhatever.pl
use warnings;
use strict;

my $upto;
my $nums2;
my $sum;

print "i'll add the numbers from 1 to whatever you want. just tell me when to stop!\n";
$upto = ;
chomp($upto);
$nums2 = 1;

if ($upto%2 == 0){
$sum = ($upto + $nums2) * ($upto / 2);
} else {
$sum = $upto * (($upto + 1) / 2);
}
print $sum, "\n";

No comments: