Scripting Games 2008 Advanced Windows PowerShell Event 6
My solution to Advanced event 6
2;2..200 |? {$i=$_;(2..($i -1) |% {$i%$_}) -notcontains 0}
Warning, I really went Brute Force here, this is probably the most ineffective way to do this :
PoSH> 2;2..200 |? {$i=$_;(2..($i -1) |% {$i%$_}) -notcontains 0}
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
PoSH> (measure-command {2;2..200 |? {$i=$_;(2..($i -1) |% {$i%$_}) -notcontains 0}}).tostring()
00:00:02.9077541
PoSH>
As you can see this took almost 3 seconds, this solution is very slow because I check EVERY modulus possible for every number.
This is a nice event to compare performance, as 3 seconds might not look that bad try performance for bigger numbers, here is mine for the primes until 5000 :
(measure-command {./event6.ps1}).tostring()
00:56:02.3067842
I'm sure most solutions will be much faster, I'm curious for Chris warwick 's solution I think he mentioned 10000 in a minute on the #powershell IRC Channel , surly better as my almost an hour for 5000 ;-)
And to how other solutions compare, I'm sure Jaykul also will come with a fast solution
Enjoy,
Greetings /\/\o\/\/