Wednesday, April 23, 2008 3:45:50 AM UTC #

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

Categories: PowerShell
Technorati:
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
Syndication

Search
Posts on this page
Categories
Sites I visit regularly
About

Powered by: newtelligence dasBlog 2.1.8102.813

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008, Peter Seale

Send mail to the author(s) E-mail



Sign In