Perl - gregorymorrison/euler1 GitHub Wiki
Perl was at one time a revolutionary language, but language fashion has left this workhorse, introduced in 1987, somewhat behind. More recent advances, such as the JVM, mainstream OOP, and Python/Ruby with their fancy-schmancy stack traces and REPLs, have caught our attention, but I thought I'd try my hand at some old-skool coding. I'm glad I did, because this version of Euler1 is clear, concise, and took maybe an hour to write:
#!/usr/bin/perl
# Euler1 in Perl
sub euler1 {
my @array = (0..shift);
my $sum = 0;
foreach (@array) {
if ($_ % 3 == 0 or $_ % 5 == 0) {
$sum += $_;
}
}
return $sum;
}
print euler1(999);
Perl has some weird constructs. Variables are defined with keyword my, and variables passed in are contained in the array shift. Also, I hate sigils...
Perl should already be installed on any Linux machine. And Perl is easy to execute - simply call your script:
$ ./euler1.pl
233168