Neighbour primes: Difference between revisions

(Added 11l)
(20 intermediate revisions by 9 users not shown)
Line 5:
Find and show primes p such that p*q+2 is prime, where q is next prime after p and '''p < 500'''
<br><br>
See also:
* [[Special neighbor primes]]
 
 
=={{header|11l}}==
Line 53 ⟶ 56:
439 443 194479
487 491 239119
</pre>
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
Very similar to [[Special_neighbor_primes#ALGOL_68|The ALGOL 68 sample in the Special neighbor primes task]]
<syntaxhighlight lang="algol68">
BEGIN # find adjacent primes p1, p2 such that p1*p2 + 2 s also prime #
PR read "primes.incl.a68" PR
INT max prime = 500;
[]BOOL prime = PRIMESIEVE ( ( max prime * max prime ) + 2 ); # sieve the primes to max prime ^ 2 + 2 #
[]INT low prime = EXTRACTPRIMESUPTO max prime FROMPRIMESIEVE prime; # get a list of the primes up to max prime #
# find the adjacent primes p1, p2 such that p1*p2 + 2 is prime #
FOR i TO UPB low prime - 1 DO
IF INT p1 p2 plus 2 = ( low prime[ i ] * low prime[ i + 1 ] ) + 2;
prime[ p1 p2 plus 2 ]
THEN print( ( "(", whole( low prime[ i ], -3 )
, " *", whole( low prime[ i + 1 ], -3 )
, " ) + 2 = ", whole( p1 p2 plus 2, -6 )
, newline
)
)
FI
OD
END
</syntaxhighlight>
{{out}}
<pre>
( 3 * 5 ) + 2 = 17
( 5 * 7 ) + 2 = 37
( 7 * 11 ) + 2 = 79
( 13 * 17 ) + 2 = 223
( 19 * 23 ) + 2 = 439
( 67 * 71 ) + 2 = 4759
(149 *151 ) + 2 = 22501
(179 *181 ) + 2 = 32401
(229 *233 ) + 2 = 53359
(239 *241 ) + 2 = 57601
(241 *251 ) + 2 = 60493
(269 *271 ) + 2 = 72901
(277 *281 ) + 2 = 77839
(307 *311 ) + 2 = 95479
(313 *317 ) + 2 = 99223
(397 *401 ) + 2 = 159199
(401 *409 ) + 2 = 164011
(419 *421 ) + 2 = 176401
(439 *443 ) + 2 = 194479
(487 *491 ) + 2 = 239119
</pre>
 
Line 407 ⟶ 457:
17 found under 500 for " + 18 "
25 found under 500 for " + 20 "</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function IsPrime(N: int64): boolean;
{Fast, optimised prime test}
var I,Stop: int64;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N+0.0));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
 
 
 
 
function GetNextPrime(var Start: integer): integer;
{Get the next prime number after Start}
{Start is passed by "reference," so the
{original variable is incremented}
begin
repeat Inc(Start)
until IsPrime(Start);
Result:=Start;
end;
 
 
 
procedure ShowNeighborPrimes(Memo: TMemo);
var P1,P2,P3,Cnt: integer;
var S: string;
begin
Memo.Lines.Add('Count P Q PQ+2');
Memo.Lines.Add('-----------------------');
Cnt:=0; P1:=1; P2:=1; S:='';
While P1< 500 do
begin
GetNextPrime(P2);
P3:=P1 * P2 + 2;
if IsPrime(P3) then
begin
Inc(Cnt);
S:=S+Format('%5D %4D %4D %6D',[Cnt,P1,P2,P3]);
S:=S+#$0D#$0A;
end;
P1:=P2;
end;
Memo.Lines.Add(S);
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
Count P Q PQ+2
-----------------------
1 3 5 17
2 5 7 37
3 7 11 79
4 13 17 223
5 19 23 439
6 67 71 4759
7 149 151 22501
8 179 181 32401
9 229 233 53359
10 239 241 57601
11 241 251 60493
12 269 271 72901
13 277 281 77839
14 307 311 95479
15 313 317 99223
16 397 401 159199
17 401 409 164011
18 419 421 176401
19 439 443 194479
20 487 491 239119
</pre>
 
 
=={{header|F_Sharp|F#}}==
Line 518 ⟶ 660:
439 443 194479
487 491 239119
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn IsPrime( n as NSUInteger ) as BOOL
BOOL isPrime = YES
NSUInteger i
if n < 2 then exit fn = NO
if n = 2 then exit fn = YES
if n mod 2 == 0 then exit fn = NO
for i = 3 to int(n^.5) step 2
if n mod i == 0 then exit fn = NO
next
end fn = isPrime
 
local fn FindNeighborPrimes( searchLimit as long )
NSUInteger p, q
printf @"p q p*q+2"
printf @"----------------------"
for p = 2 to searchLimit
if ( fn IsPrime(p) == NO ) then continue
q = p + 1
while ( fn IsPrime(q) == NO )
q += 1
wend
if ( fn IsPrime( p * q + 2 ) == NO ) then continue
printf @"%lu\t\t%-6lu\t%-6lu", p, q, p * q + 2
next
end fn
 
fn FindNeighborPrimes( 499 )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre style="height:20ex;">
p q p*q+2
----------------------
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
 
</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Neighbour_primes}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
 
[[File:Fōrmulæ - Neighbour primes 01.png]]
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Neighbour primes 02.png]]
In '''[https://formulae.org/?example=Neighbour_primes this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Go}}==
Line 584 ⟶ 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 833 ⟶ 1,061:
</pre>
 
 
=={{header|PL/0}}==
Formatted output isn't PL/0's forté, so this sample just shows each p1 of the p1, p2 neighbours.
<br>
This is almost identical to the [[Special neighbor primes#PL/0|PL/0 sample in the Special Neighbor primes task]]
<syntaxhighlight lang="pascal">
var n, p1, p2, prime;
procedure isnprime;
var p;
begin
prime := 1;
if n < 2 then prime := 0;
if n > 2 then begin
prime := 0;
if odd( n ) then prime := 1;
p := 3;
while p * p <= n * prime do begin
if n - ( ( n / p ) * p ) = 0 then prime := 0;
p := p + 2;
end
end
end;
begin
p1 := 3;
p2 := 5;
while p2 < 500 do begin
n := ( p1 * p2 ) + 2;
call isnprime;
if prime = 1 then ! p1;
n := p2 + 2;
call isnprime;
while prime = 0 do begin
n := n + 2;
call isnprime;
end;
p1 := p2;
p2 := n;
end
end.
</syntaxhighlight>
{{out}}
<pre>
3
5
7
13
19
67
149
179
229
239
241
269
277
307
313
397
401
419
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>
 
=={{header|Python}}==
Line 879 ⟶ 1,208:
439 443 194479
487 491 239119</pre>
 
 
=={{header|Raku}}==
Line 1,062 ⟶ 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>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
p Prime.each(500).each_cons(2).select{|p, q| (p*q+2).prime? }</syntaxhighlight>
{{out}}
<pre>
[[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]]
</pre>
 
Line 1,073 ⟶ 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,086 ⟶ 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