Created
December 28, 2012 22:29
-
-
Save fornext1119/4402575 to your computer and use it in GitHub Desktop.
Project Euler Problem 1 Perl Version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
#単純に加算 | |
my $sum = 0; | |
for (1..999) | |
{ | |
if (($_ % 3) == 0) { $sum += $_; } | |
elsif (($_ % 5) == 0) { $sum += $_; } | |
} | |
print "$sum\n"; | |
#等差数列の和 | |
$sum = 0; | |
#初項, 公差 | |
foreach my $a (3, 5, 15) | |
{ | |
my $n = int(999 / $a); #項数 | |
my $l = $n * $a; #末項 a+(n-1)d | |
if ($a == 3) { $sum += (($a + $l) * $n / 2); } | |
elsif ($a == 5) { $sum += (($a + $l) * $n / 2); } | |
elsif ($a == 15) { $sum -= (($a + $l) * $n / 2); } | |
} | |
print "$sum\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment