Primality by trial division: Difference between revisions
Content added Content deleted
(jq) |
|||
Line 1,127: | Line 1,127: | ||
while |
while |
||
dup * < .</lang> |
dup * < .</lang> |
||
=={{header|jq}}== |
|||
<tt> |
|||
def is_prime: |
|||
if . == 2 then true |
|||
else 2 < . and . % 2 == 1 and |
|||
. as $in |
|||
| (($in + 1) | sqrt) as $m |
|||
| (((($m - 1) / 2) | floor) + 1) as $max |
|||
| all( range(1; $max) ; $in % ((2 * .) + 1) > 0 ) |
|||
end; |
|||
</tt> |
|||
Example -- the command line is followed by alternating lines of input and output: |
|||
<tt> |
|||
$ jq -f is_prime.jq |
|||
-2 |
|||
false |
|||
1 |
|||
false |
|||
2 |
|||
true |
|||
100 |
|||
false |
|||
</tt> |
|||
=={{header|K}}== |
=={{header|K}}== |