If you aren't already in the know, these ten problems are from the 2008 Winter Scripting games.

For each problem, I'm going to quickly sum up the interesting (interesting TO ME) bits of each problem, then I'm going to post the full source.

Event 6: Prime Time

Event: calculate primes up to 200. Solution: boring brute-force method.

Source

#PROBLEM #6


#As simple as possible, calculation of primes. This
#routine is inefficient.
function Is-Prime ($n)
{
    if ($n -le 1)
    {
        return $FALSE
    }
   
    $largestPossibleCoefficient = [int]([Math]::Floor($n/2))
    for ($i = 2; $i -le $largestPossibleCoefficient; $i++)
    {
        if ( ($n/$i) -eq ([Math]::Floor($n/$i)) )
        {
            return $FALSE
        }
    }
   
    return $TRUE;
}


function Solve-Problem6
{
    $range = 2..200
    foreach ($candidatePrime in $range)
    {
        if (Is-Prime $candidatePrime)
        {
            Write-Host $candidatePrime
        }
    }
}


Solve-Problem6

2008 Winter Scripting Game Events: Index