Thicket data repository for the EEG
1{
2 "id": "https://www.tunbury.org/2017/05/01/prime-numbers-in-powershell",
3 "title": "Prime Numbers in PowerShell",
4 "link": "https://www.tunbury.org/2017/05/01/prime-numbers-in-powershell/",
5 "updated": "2017-05-01T12:41:29",
6 "published": "2017-05-01T12:41:29",
7 "summary": "Dylan was using a number square to calculate prime numbers so it amused me to code up a couple of algorithms to show just how quick the sieve method actually is. I’ve done these in PowerShell because … reasons.",
8 "content": "<p>Dylan was using a number square to calculate prime numbers so it amused me to code up a couple of algorithms to show just how quick the sieve method actually is. I’ve done these in PowerShell because … reasons.</p>\n\n<p>So as a baseline, here’s a basic way to calculate a prime. Start with a number and try to divide it by every number starting from 2 up to the square root of the number. I’ve used <code>throw</code> in a <code>try</code>/<code>catch</code> block to move to the next iteration of the outer loop without executing the <code>Write-Host</code> line.</p>\n\n<div><div><pre><code>for ($n = 3; $n -lt 100000; $n++) {\n try {\n for ($d = 2; $d -le [Math]::Sqrt($n); $d++) {\n if ($n % $d -eq 0) {\n throw\n }\n }\n Write-Host -NoNewLine \"$n \"\n }\n catch { }\n}\n</code></pre></div></div>\n\n<p>Interestingly, all those exceptions add quite an overhead because this same algorithm using a local variable ran three times quicker on my machine (27 seconds for the first and 9 seconds for this)</p>\n\n<div><div><pre><code>for ($n = 3; $n -lt 100000; $n++) {\n $prime = $true\n for ($d = 2; $d -le [Math]::Sqrt($n); $d++) {\n if ($n % $d -eq 0) {\n $prime = $false\n break;\n }\n }\n if ($prime) {\n Write-Host -NoNewLine \"$n \"\n }\n}\n</code></pre></div></div>\n\n<p>Obviously we should optimise this by removing even numbers as below and this, as you’d expect, halves the run time.</p>\n\n<div><div><pre><code>for ($n = 3; $n -lt 100000; $n += 2) {\n $prime = $true\n for ($d = 3; $d -le [Math]::Sqrt($n); $d += 2) {\n if ($n % $d -eq 0) {\n $prime = $false\n break;\n }\n }\n if ($prime) {\n }\n}\n</code></pre></div></div>\n\n<p>Anyway, the sieve is all done in 0.75 seconds:</p>\n\n<div><div><pre><code>$ints = 0..100000\nfor ($i = 2; $i -lt [Math]::Sqrt($ints.length); $i++) {\n if ($ints[$i] -eq 0) {\n continue\n }\n for ($j = $i * $i; $j -lt $ints.length; $j += $i) {\n $ints[$j] = 0\n }\n}\n$ints | foreach { if ($_) { Write-Host -NoNewLine \"$_ \" } }\n</code></pre></div></div>\n\n<p>As the maximum number increases the differences become even more stark. At 1,000,000 the sieve completed in 11 seconds but the simple method took 129 seconds</p>\n\n<p>For my timings, I used <code>measure-command</code> and removed the <code>Write-Host</code> lines.</p>",
9 "content_type": "html",
10 "author": {
11 "name": "Mark Elvers",
12 "email": "mark.elvers@tunbury.org",
13 "uri": null
14 },
15 "categories": [
16 "powershell"
17 ],
18 "source": "https://www.tunbury.org/atom.xml"
19}