Neighbour primes: Difference between revisions

(8 intermediate revisions by 4 users not shown)
Line 789:
[3,5,7,13,19,67,149,179,229,239,241,269,277,307,313,397,401,419,439,487]
</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j"> (#~ 1 p: {:"1) 2 (, 2 + */)\ i.&.(p:inv) 500
3 5 17
5 7 37
7 11 79
13 17 223
19 23 439
67 71 4759
149 151 22501
179 181 32401
229 233 53359
239 241 57601
241 251 60493
269 271 72901
277 281 77839
307 311 95479
313 317 99223
397 401 159199
401 409 164011
419 421 176401
439 443 194479
487 491 239119</syntaxhighlight>
 
=={{header|jq}}==
Line 1,100 ⟶ 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,329 ⟶ 1,390:
<pre>
[3, 5, 7, 13, 19, 67, 149, 179, 229, 239, 241, 269, 277, 307, 313, 397, 401, 419, 439, 487]
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
≪ → max
≪ { } 2
'''WHILE''' DUP max < '''REPEAT'''
DUP NEXTPRIME
'''IF''' DUP2 * 2 + ISPRIME? '''THEN''' UNROT + SWAP '''ELSE''' NIP '''END'''
'''END''' DROP
≫ ≫ '<span style="color:blue">NEIGHB</span>' STO
 
500 <span style="color:blue">NEIGHB</span>
{{out}}
<pre>
1: {3 5 7 13 19 67 149 179 229 239 241 269 277 307 313 397 401 419 439 487}
</pre>
 
Line 1,349 ⟶ 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,362 ⟶ 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