Semiprime: Difference between revisions

Content added Content deleted
(→‎{{header|jq}}: simplify)
Line 1,357: Line 1,357:
{{works with|jq}}
{{works with|jq}}
'''Works with gojq, the Go implementation of 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">
<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:
def is_semiprime:
. as $n
{i: 2, n: ., nf: 0}
| until( .i > .n or .result;
| any(proper_factors;
is_prime and (($n / .) | (. == $n or is_prime) );
until(.n % .i != 0 or .result;
if .nf == 2 then .result = 0
else .nf += 1
| .n /= .i
end)
| .i += 1)
| if .result == 0 then false else .nf == 2 end;
</syntaxhighlight>
</syntaxhighlight>
'''Examples'''
'''Examples'''