Thursday, February 28, 2008

2008 Scripting Games, Solution 6

# Advanced Event 6: Prime Time
# http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/aevent6.mspx

# By default stop at 200
param($max=200)
2 # by definition
# Main loop, step by two as only odd numbers can be primes
for($i=3;$i -le $max; $i+=2) {
# Init prime boolean
$prime=$true
# Walk through divisors up to the number at hand
for($d=3; $d -lt $i; $d+=2) {
write-verbose "$i $d $($i % $d)"
if (!($i % $d)) {
# Divisor found, break for and look at next value
$prime=$false
break
}
}
#
if ($prime) {$i}
}

No comments: