Created
December 28, 2012 22:32
-
-
Save fornext1119/4402603 to your computer and use it in GitHub Desktop.
Project Euler Problem 1 PowerShell 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
#単純に加算 | |
$sum = 0 | |
foreach ($i in 1..999) | |
{ | |
if (($i % 3) -eq 0) | |
{ | |
$sum += $i | |
} | |
elseif (($i % 5) -eq 0) | |
{ | |
$sum += $i | |
} | |
} | |
Write-Host $sum | |
#等差数列の和 | |
$sum = 0 | |
#初項, 公差 | |
foreach($a in @(3, 5, 15)) | |
{ | |
$n = [math]::floor(999 / $a) #項数 | |
$l = $n * $a #末項 a+(n-1)d | |
switch ($a) | |
{ | |
3 {$sum += (($a + $l) * $n / 2)} | |
5 {$sum += (($a + $l) * $n / 2)} | |
default {$sum -= (($a + $l) * $n / 2)} | |
} | |
} | |
Write-Host $sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment