Semiprime: Difference between revisions

(→‎{{header|jq}}: simplify)
Line 1,357:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`.
 
<syntaxhighlight lang="jq">
# Output: a stream of proper factors (probably unsorted)
def proper_factors:
range(2; 1 + sqrt|floor) as $i
| if (. % $i) == 0
then (. / $i) as $r
| if $i == $r then $i else $i, $r end
else empty
end;
 
def is_semiprime:
.{i: as2, $n: ., nf: 0}
| until( .i > .n or .result;
| any(proper_factors;
is_prime and until(($.n /% .)i | (. =!= $n0 or is_prime) ).result;
if .nf == 2 then .result = 0
else empty.nf += 1
| .n /= .i
end;)
| if (. % $| .i) =+= 0 1)
| if $i.result == $r0 then $ifalse else $i,.nf == $r2 end;
</syntaxhighlight>
'''Examples'''
2,442

edits