Neighbour primes: Difference between revisions

m (→‎J: simplify)
(3 intermediate revisions by 2 users not shown)
Line 1,123:
439
487
</pre>
 
=={{header|Prolog}}==
for swi prolog (© 2024)
<syntaxhighlight lang="prolog">
primes(2, Limit):- 2 =< Limit.
primes(P, Limit):-
between(3, Limit, P),
P /\ 1 > 0, % odd
M is floor(sqrt(P)) - 1, % reverse 2*I+1
Max is M div 2,
forall(between(1, Max, I), P mod (2*I+1) > 0).
 
isPrime(P):-
primes(P, inf).
 
primeProd(PList, [P1, P2]):-
append([_, [P1, P2], _], PList),
Prod is P1 * P2 + 2,
isPrime(Prod).
 
showList(List):-
findnsols(10, _, (member(Pair, List), format('~|~t(~d,~d)~9+ ', Pair)), _),
nl,
fail.
showList(_).
 
do:-Limit is 500,
findall(P, primes(P, Limit), PrimeList),
findall(Pair, primeProd(PrimeList, Pair), P1P2List),
showList(P1P2List).
</syntaxhighlight>
{{out}}
<pre>
?- do.
(3,5) (5,7) (7,11) (13,17) (19,23) (67,71) (149,151) (179,181) (229,233) (239,241)
(241,251) (269,271) (277,281) (307,311) (313,317) (397,401) (401,409) (419,421) (439,443) (487,491)
true.
</pre>
 
Line 1,388 ⟶ 1,426:
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./seqfmt" for LstFmt
import "/fmt" for Fmt
 
var primes = Int.primeSieve(504)
Line 1,401 ⟶ 1,437:
if (Int.isPrime(p)) nprimes.add(primes[i])
}
Fmt.tprint("$3d", nprimes, 10)
for (chunk in Lst.chunks(nprimes, 10)) Fmt.print("$3d", chunk)
System.print("\nFound %(nprimes.count) such primes.")</syntaxhighlight>
 
64

edits