Primality by trial division: Difference between revisions

(→‎{{header|Haskell}}: used in emirps.)
Line 1,949:
 
=={{header|PowerShell}}==
{{works with|PowerShell|4.0}}
<lang powershell>function isPrime ($n) {
<lang PowerShell>
if ($n -eq 1) {
<lang powershell>function isPrime ($n) {
return $false
}if else($n -eq 1) {$false}
ifelseif ($n -eq 12) {$true}
return (@(2..[Math]::Sqrt($n) | Where-Object { $n % $_ -eq 0 }).Length -eq 0)
elseif ($n -eq 3) {$true}
else{
$m = [Math]::Floor([Math]::Sqrt($n))
return (@(2..[Math]::Sqrt$m | where {($_ -lt $n) | Where-Object {and ($n % $_ -eq 0) }).LengthCount -eq 0)
}
}
}</lang>
<!-- Yes, I got a little carried away with the pipeline. This function checks whether the number of divisors between 2 and √n is zero and in that case it's a prime. -->
<b>Output:</b>
<pre>
isPrime 1 : False
isPrime 2 : True
isPrime 3 : True
isPrime 4 : False
isPrime 5 : True
isPrime 6 : False
isPrime 7 : True
isPrime 8 : False
isPrime 9 : False
isPrime 10 : False
</pre>
 
=={{header|Prolog}}==
678

edits