Jump to content

Primality by trial division: Difference between revisions

jq
(jq)
Line 1,127:
while
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}}==
2,489

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.