Wilson primes of order n: Difference between revisions

Content added Content deleted
(Added Prolog solution)
Line 350: Line 350:
11 | 17 2713
11 | 17 2713
</pre>
</pre>

=={{header|jq}}==
'''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` as used here.

(*) The C implementation of jq lacks the precision for handling the case p<11,000
so the output below is based on a run of gojq.

'''Preliminaries'''
<lang jq>def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;

# For 0 <= $n <= ., factorials[$n] is $n !
def factorials:
reduce range(1; .+1) as $n ([1];
.[$n] = $n * .[$n-1]);

def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;

def primes: 2, (range(3; infinite; 2) | select(is_prime));</lang>
'''Wilson primes'''
<lang jq># Input: the limit of $p
def wilson_primes:
def sgn: if . % 2 == 0 then 1 else -1 end;

. as $limit
| factorials as $facts
| " n: Wilson primes\n--------------------",
(range(1;12) as $n
| "\($n|lpad(2)) : \(
[emit_until( . >= $limit; primes)
| select(. as $p
| $p >= $n and
(($facts[$n - 1] * $facts[$p - $n] - ($n|sgn)) % ($p*$p) == 0 )) ])" );

11000 | wilson_primes</lang>
{{out}}
gojq -ncr -f rc-wilson-primes.jq
<pre>
n: Wilson primes
--------------------
1 : [5,13,563]
2 : [2,3,11,107,4931]
3 : [7]
4 : [10429]
5 : [5,7,47]
6 : [11]
7 : [17]
8 : []
9 : [541]
10 : [11,1109]
11 : [17,2713]
</pre>



=={{header|Julia}}==
=={{header|Julia}}==