Quick script to find primes in PowerShell. I know it would be faster to use C/C++, and this script actually converts to C++ pretty well. Hey if you can write something more efficent I would love to see it. I’m going to a comparison between this, the compiled C and something similar in SQL Server in the next few days. I think we know what will be the fastest, but what will be the second fastest and by how much?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # Get Start Time $startTime = (Get-Date); #-------------------------------------------------------------------- #Script Starts #-------------------------------------------------------------------- [Int[]]$primes=3; 1; 2; 3; for ($x=5;$x -lt 10000; $x+=2) { for ($y=0;$y -lt $primes.length; $y++){ if ($x%$primes[$y] -eq 0) { $Add=$False; break; }elseif ($primes[$y]*2 -gt $x){ $Add=$True; break; }else{ $Add=$True; } }if ($Add -eq $True) { #$x; $primes+=$x; } }; #-------------------------------------------------------------------- #Script Ends #-------------------------------------------------------------------- $endTime = (Get-Date); "Time: $(($endTime-$startTime).totalseconds) seconds" |
Category: Code Snippet(PowerShell), Jibberish