Special neighbor primes: Difference between revisions

From Rosetta Code
Content added Content deleted
(add parigp)
m (→‎{{header|PL/M}}: tweak - Q must be odd)
 
(26 intermediate revisions by 14 users not shown)
Line 2: Line 2:


;Task:
;Task:
Let &nbsp; ('''p<sub>1</sub>''', &nbsp;'''p<sub>2</sub>''') &nbsp; are neighbor primes.
Let &nbsp; ('''p<sub>1</sub>''', &nbsp;'''p<sub>2</sub>''') &nbsp; are [[Neighbour_primes|neighbor primes]].


Find and show here in base ten if &nbsp; '''p<sub>1</sub>+&nbsp;p<sub>2</sub>&nbsp;-1''' &nbsp; is prime, &nbsp; where &nbsp; '''p<sub>1</sub>, &nbsp; p<sub>2</sub> &nbsp;&lt;&nbsp; 100'''.
Find and show here in base ten if &nbsp; '''p<sub>1</sub>+&nbsp;p<sub>2</sub>&nbsp;-1''' &nbsp; is prime, &nbsp; where &nbsp; '''p<sub>1</sub>, &nbsp; p<sub>2</sub> &nbsp;&lt;&nbsp; 100'''.
<br><br>
<br><br>

=={{header|11l}}==
{{trans|Nim}}

<syntaxhighlight lang="11l">F is_prime(n)
I n == 2
R 1B
I n < 2 | n % 2 == 0
R 0B
L(i) (3 .. Int(sqrt(n))).step(2)
I n % i == 0
R 0B
R 1B

V primes = (0.<100).filter(n -> is_prime(n))

L(i) 0 .< primes.len - 1
V p1 = primes[i]
V p2 = primes[i + 1]
I is_prime(p1 + p2 - 1)
print((p1, p2))</syntaxhighlight>

{{out}}
<pre>
(3, 5)
(5, 7)
(7, 11)
(11, 13)
(13, 17)
(19, 23)
(29, 31)
(31, 37)
(41, 43)
(43, 47)
(61, 67)
(67, 71)
(73, 79)
</pre>

=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT"

INT FUNC GetNextPrime(INT i BYTE ARRAY primes)
DO
i==+1
UNTIL primes(i)
OD
RETURN (i)

PROC Main()
DEFINE MAXPRIME="99"
DEFINE MAX="200"
BYTE ARRAY primes(MAX+1)
INT i,p

Put(125) PutE() ;clear the screen
Sieve(primes,MAX+1)
FOR i=2 TO MAXPRIME
DO
IF primes(i) THEN
p=GetNextPrime(i,primes)
IF p<=MAXPRIME AND primes(i+p-1)=1 THEN
PrintF("%I+%I-1=%I%E",i,p,i+p-1)
FI
FI
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Special_neighbor_primes.png Screenshot from Atari 8-bit computer]
<pre>
3+5-1=7
5+7-1=11
7+11-1=17
11+13-1=23
13+17-1=29
19+23-1=41
29+31-1=59
31+37-1=67
41+43-1=83
43+47-1=89
61+67-1=127
67+71-1=137
73+79-1=151
</pre>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
Very similar to [[Neighbour_primes#ALGOL_68|The ALGOL 68 sample in the Neighbour primes task]]
<lang algol68>BEGIN # find adjacent primes p1, p2 such that p1 + p2 - 1 is also prime #
<syntaxhighlight lang="algol68">BEGIN # find adjacent primes p1, p2 such that p1 + p2 - 1 is also prime #
PR read "primes.incl.a68" PR
PR read "primes.incl.a68" PR
INT max prime = 100;
INT max prime = 100;
[]BOOL prime = PRIMESIEVE ( max prime * 2 ); # sieve the primes to max prime * 2 #
[]BOOL prime = PRIMESIEVE ( max prime * 2 ); # sieve the primes to max prime * 2 #
[]INT low prime = EXTRACTPRIMESUPTO max prime FROMPRIMESIEVE prime; # construct a list of the primes up to max prime #
[]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 - 1 is prime #
# find the adjacent primes p1, p2 such that p1 + p2 - 1 is prime #
FOR i TO UPB low prime - 1 DO
FOR i TO UPB low prime - 1 DO
Line 26: Line 112:
FI
FI
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 43: Line 129:
( 73 + 79 ) - 1 = 151
( 73 + 79 ) - 1 = 151
</pre>
</pre>

=={{header|Arturo}}==
<syntaxhighlight lang="arturo">primesBelow100: select 1..100 => prime?

loop 1..dec size primesBelow100 'p [
p1: primesBelow100\[p-1]
p2: primesBelow100\[p]
if prime? dec p1 + p2 ->
print ["(" p1 "," p2 ")"]
]</syntaxhighlight>

{{out}}

<pre>( 3 , 5 )
( 5 , 7 )
( 7 , 11 )
( 11 , 13 )
( 13 , 17 )
( 19 , 23 )
( 29 , 31 )
( 31 , 37 )
( 41 , 43 )
( 43 , 47 )
( 61 , 67 )
( 67 , 71 )
( 73 , 79 )</pre>


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SPECIAL_NEIGHBOR_PRIMES.AWK
# syntax: GAWK -f SPECIAL_NEIGHBOR_PRIMES.AWK
BEGIN {
BEGIN {
Line 75: Line 187:
return(1)
return(1)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 93: Line 205:
Special neighbor primes 3-99: 13
Special neighbor primes 3-99: 13
</pre>
</pre>

=={{header|BASIC}}==
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">#include"isprime.bas"

function nextprime( n as uinteger ) as uinteger
'finds the next prime after n
if n = 0 then return 2
if n < 3 then return n + 1
dim as integer q = n + 2
while not isprime(q)
q+=2
wend
return q
end function

dim as uinteger p1, p2

for p1 = 3 to 100 step 2
p2 = nextprime(p1)
if isprime(p1) andalso p2<100 andalso isprime( p1 + p2 - 1 ) then
print p1, p2, p1 + p2 - 1
end if
next p1
</syntaxhighlight>
{{out}}<pre>
3 5 7
5 7 11
7 11 17
11 13 23
13 17 29
19 23 41
29 31 59
31 37 67
41 43 83
43 47 89
61 67 127
67 71 137
73 79 151
</pre>

==={{header|GW-BASIC}}===
{{works with|BASICA}}
<syntaxhighlight lang="gwbasic">10 FOR P = 3 TO 99 STEP 2
20 GOSUB 130
30 IF Q = 0 THEN GOTO 110
40 GOSUB 220
50 IF P2>100 THEN END
60 T = P
70 P = P2 + T - 1
80 GOSUB 130
90 IF Q = 1 THEN PRINT USING "## + ## - 1 = ###";T;P2;P
100 P=T
110 NEXT P
120 END
130 REM tests if a number is prime
140 Q=0
150 IF P=3 THEN Q=1:RETURN
160 I=1
170 I=I+1
180 IF INT(P/I)*I = P THEN RETURN
190 IF I*I<=P THEN GOTO 170
200 Q = 1
210 RETURN
220 REM finds the next prime after P, result in P2
230 IF P = 0 THEN P2 = 2: RETURN
240 IF P<3 THEN P2 = P + 1: RETURN
250 T = P
260 P = P + 1
270 GOSUB 130
280 IF Q = 1 THEN P2 = P: P = T: RETURN
290 GOTO 260</syntaxhighlight>
{{out}}
<pre> 3 + 5 - 1 = 7
5 + 7 - 1 = 11
7 + 11 - 1 = 17
11 + 13 - 1 = 23
13 + 17 - 1 = 29
19 + 23 - 1 = 41
29 + 31 - 1 = 59
31 + 37 - 1 = 67
41 + 43 - 1 = 83
43 + 47 - 1 = 89
61 + 67 - 1 = 127
67 + 71 - 1 = 137
73 + 79 - 1 = 151
</pre>

==={{header|Tiny BASIC}}===
<syntaxhighlight lang="tinybasic"> REM B = SECOND OF THE NEIGBOURING PRIMES
REM C = P + B - 1
REM I = index variable
REM P = INPUT TO NEXTPRIME ROUTINE AND ISPRIME ROUTINE, also first of the two primes
REM T = Temporary variable, multiple uses
REM Z = OUTPUT OF ISPRIME, 1=prime, 0=not

LET P = 1
20 LET P = P + 2
IF P > 100 THEN END
GOSUB 100
IF Z = 0 THEN GOTO 20
GOSUB 120
IF B > 100 THEN END
LET T = P
LET P = P + B - 1
GOSUB 100
LET C = P
LET P = T
IF Z = 0 THEN GOTO 20
PRINT P," + ",B," - 1 = ", C
GOTO 20

100 REM PRIMALITY BY TRIAL DIVISION
LET Z = 1
LET I = 2
110 IF (P/I)*I = P THEN LET Z = 0
IF Z = 0 THEN RETURN
LET I = I + 1
IF I*I <= P THEN GOTO 110
RETURN
120 REM next prime after P
IF P < 2 THEN LET B = 2
IF P = 2 THEN LET B = 3
IF P < 3 THEN RETURN
LET T = P
130 LET P = P + 1
GOSUB 100
IF Z = 1 THEN GOTO 140
GOTO 130
140 LET B = P
LET P = T
RETURN</syntaxhighlight>
{{out}}<pre>
3 + 5 - 1 = 7
5 + 7 - 1 = 11
7 + 11 - 1 = 17
11 + 13 - 1 = 23
13 + 17 - 1 = 29
19 + 23 - 1 = 41
29 + 31 - 1 = 59
31 + 37 - 1 = 67
41 + 43 - 1 = 83
43 + 47 - 1 = 89
61 + 67 - 1 = 127
67 + 71 - 1 = 137
73 + 79 - 1 = 151
</pre>

=={{header|C}}==
<syntaxhighlight lang="c">#include<stdio.h>
#include<stdlib.h>

int isprime( int p ) {
int i;
if(p==2) return 1;
if(!(p%2)) return 0;
for(i=3; i*i<=p; i+=2) {
if(!(p%i)) return 0;
}
return 1;
}

int nextprime( int p ) {
int i=0;
if(p==0) return 2;
if(p<3) return p+1;
while(!isprime(++i + p));
return i+p;
}

int main(void) {
int p1, p2;
for(p1=3;p1<=99;p1+=2) {
p2=nextprime(p1);
if(p2<100&&isprime(p1)&&isprime(p2+p1-1)) {
printf( "%d + %d - 1 = %d\n", p1, p2, p1+p2-1 );
}
}
return 0;
}</syntaxhighlight>
{{out}}<pre>3 + 5 - 1 = 7
5 + 7 - 1 = 11
7 + 11 - 1 = 17
11 + 13 - 1 = 23
13 + 17 - 1 = 29
19 + 23 - 1 = 41
29 + 31 - 1 = 59
31 + 37 - 1 = 67
41 + 43 - 1 = 83
43 + 47 - 1 = 89
61 + 67 - 1 = 127
67 + 71 - 1 = 137
73 + 79 - 1 = 151
</pre>

=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Uses the [[Extensible_prime_generator#Delphi|Delphi Prime-Generator Object]]


<syntaxhighlight lang="Delphi">
procedure SpecialNeighborPrimes(Memo: TMemo);
var I: integer;
var P1,P2: integer;
var Sieve: TPrimeSieve;
begin
Sieve:=TPrimeSieve.Create;
try
{Build more primes than we need}
Sieve.Intialize(200);
{Go through all primes}
for I:=1 to High(Sieve.Primes) do
begin
{Get neighbor primes}
P1:=Sieve.Primes[I-1];
P2:=Sieve.Primes[I];
{only test up to 100}
if P2>=100 then break;
{if P1+P2-1 is prime then display}
if Sieve.Flags[P1 + P2 - 1] then Memo.Lines.Add(Format('(%d, %d)',[P1,P2]));
end;
finally Sieve.Free; end;
end;


</syntaxhighlight>
{{out}}
<pre>
(3, 5)
(5, 7)
(7, 11)
(11, 13)
(13, 17)
(19, 23)
(29, 31)
(31, 37)
(41, 43)
(43, 47)
(61, 67)
(67, 71)
(73, 79)
Elapsed Time: 9.926 ms.

</pre>




=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Special neighbor primes. Nigel Galloway: August 6th., 2021
// Special neighbor primes. Nigel Galloway: August 6th., 2021
pCache|>Seq.pairwise|>Seq.takeWhile(snd>>(>)100)|>Seq.filter(fun(n,g)->isPrime(n+g-1))|>Seq.iter(printfn "%A")
pCache|>Seq.pairwise|>Seq.takeWhile(snd>>(>)100)|>Seq.filter(fun(n,g)->isPrime(n+g-1))|>Seq.iter(printfn "%A")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 119: Line 479:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: kernel lists lists.lazy math math.primes
<syntaxhighlight lang="factor">USING: kernel lists lists.lazy math math.primes
math.primes.lists prettyprint sequences ;
math.primes.lists prettyprint sequences ;


lprimes dup cdr lzip [ sum 1 - prime? ] lfilter
lprimes dup cdr lzip [ sum 1 - prime? ] lfilter
[ second 100 < ] lwhile [ . ] leach</lang>
[ second 100 < ] lwhile [ . ] leach</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 141: Line 501:
</pre>
</pre>


=={{header|FreeBASIC}}==
=={{header|Fermat}}==
<syntaxhighlight lang="fermat">Func Nextprime(p) =
<lang freebasic>#include"isprime.bas"
q:=1;

while not Isprime(p+q)=1 do
function nextprime( n as uinteger ) as uinteger
'finds the next prime after n
q:=q + 1;
od;
if n = 0 then return 2
p+q.;
if n < 3 then return n + 1
dim as integer q = n + 2
for p1 = 3 to 99 by 2 do
while not isprime(q)
p2:=Nextprime(p1);
q+=2
if p2<100 and Isprime(p1)=1 and Isprime(p1+p2-1) then
wend
!!(p1,' +',p2,' - 1 =',p1+p2-1);
return q
fi;
end function
od;</syntaxhighlight>

dim as uinteger p1, p2

for p1 = 3 to 100 step 2
p2 = nextprime(p1)
if isprime(p1) andalso p2<100 andalso isprime( p1 + p2 - 1 ) then
print p1, p2, p1 + p2 - 1
end if
next p1
</lang>
{{out}}<pre>
{{out}}<pre>
3 5 7
3 + 5 - 1 = 7
5 7 11
5 + 7 - 1 = 11
7 11 17
7 + 11 - 1 = 17
11 13 23
11 + 13 - 1 = 23
13 17 29
13 + 17 - 1 = 29
19 23 41
19 + 23 - 1 = 41
29 31 59
29 + 31 - 1 = 59
31 37 67
31 + 37 - 1 = 67
41 43 83
41 + 43 - 1 = 83
43 47 89
43 + 47 - 1 = 89
61 67 127
61 + 67 - 1 = 127
67 71 137
67 + 71 - 1 = 137
73 79 151
73 + 79 - 1 = 151
</pre>
</pre>


Line 183: Line 534:
{{trans|Wren}}
{{trans|Wren}}
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 225: Line 576:
pow *= 10
pow *= 10
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 256: Line 607:
Found 103,611 special neighbor primes under 10,000,000.
Found 103,611 special neighbor primes under 10,000,000.
</pre>
</pre>

=={{header|J}}==
<syntaxhighlight lang="j"> (#~ 1 p: {:"1) 2 (, _1 + +/)\ i.&.(p:inv) 100
3 5 7
5 7 11
7 11 17
11 13 23
13 17 29
19 23 41
29 31 59
31 37 67
41 43 83
43 47 89
61 67 127
67 71 137
73 79 151</syntaxhighlight>


=={{header|jq}}==
=={{header|jq}}==
Line 262: Line 629:


This entry uses `is_prime` as defined at [[Erd%C5%91s-primes#jq]].
This entry uses `is_prime` as defined at [[Erd%C5%91s-primes#jq]].
<lang jq># Assumes . > 2
<syntaxhighlight lang="jq"># Assumes . > 2
def next_prime:
def next_prime:
first(range(.+2; infinite) | select(is_prime));
first(range(.+2; infinite) | select(is_prime));
Line 280: Line 647:
| if $savePairs then {pcount, neighbors} else {pcount} end;
| if $savePairs then {pcount, neighbors} else {pcount} end;


100|specialNP(true)</lang>
100|specialNP(true)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
{"pcount":13,"neighbors":[[3,5],[5,7],[7,11],[11,13],[13,17],[19,23],[29,31],[31,37],[41,43],[43,47],[61,67],[67,71],[73,79]]}
{"pcount":13,"neighbors":[[3,5],[5,7],[7,11],[11,13],[13,17],[19,23],[29,31],[31,37],[41,43],[43,47],[61,67],[67,71],[73,79]]}
</pre>
</pre>



=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes
<syntaxhighlight lang="julia">using Primes


function specialneighbors(N, savepairs=true)
function specialneighbors(N, savepairs=true)
Line 311: Line 677:
print("\nCount of such prime pairs under 1,000,000,000: ",
print("\nCount of such prime pairs under 1,000,000,000: ",
specialneighbors(1_000_000_000, false)[2])
specialneighbors(1_000_000_000, false)[2])
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
13 special neighbor prime pairs under 100:
13 special neighbor prime pairs under 100:
Line 334: Line 700:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>p = Prime@Range@PrimePi[100];
<syntaxhighlight lang="mathematica">p = Prime@Range@PrimePi[100];
Select[Partition[p, 2, 1], Total/*(# - 1 &)/*PrimeQ]</lang>
Select[Partition[p, 2, 1], Total/*(# - 1 &)/*PrimeQ]</syntaxhighlight>
{{out}}
{{out}}
<pre>{{3, 5}, {5, 7}, {7, 11}, {11, 13}, {13, 17}, {19, 23}, {29, 31}, {31, 37}, {41, 43}, {43, 47}, {61, 67}, {67, 71}, {73, 79}}</pre>
<pre>{{3, 5}, {5, 7}, {7, 11}, {11, 13}, {13, 17}, {19, 23}, {29, 31}, {31, 37}, {41, 43}, {43, 47}, {61, 67}, {67, 71}, {73, 79}}</pre>


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import strutils, sugar
<syntaxhighlight lang="nim">import strutils, sugar


const Max = 100 - 1
const Max = 100 - 1
Line 363: Line 729:


echo "Found $1 special neighbor primes less than $2:".format(list.len, Max + 1)
echo "Found $1 special neighbor primes less than $2:".format(list.len, Max + 1)
echo list.join(", ")</lang>
echo list.join(", ")</syntaxhighlight>


{{out}}
{{out}}
Line 369: Line 735:
(3, 5), (5, 7), (7, 11), (11, 13), (13, 17), (19, 23), (29, 31), (31, 37), (41, 43), (43, 47), (61, 67), (67, 71), (73, 79)</pre>
(3, 5), (5, 7), (7, 11), (11, 13), (13, 17), (19, 23), (29, 31), (31, 37), (41, 43), (43, 47), (61, 67), (67, 71), (73, 79)</pre>


=={{header|PARI/GP}}
=={{header|PARI/GP}}==
<lang parigp>for(p1=1,100,p2=nextprime(p1+1); if(isprime(p1)&&p2<100&&isprime(p1+p2-1),print(p1," ",p2," ",p1+p2-1)))</lang>
<syntaxhighlight lang="parigp">for(p1=1,100,p2=nextprime(p1+1); if(isprime(p1)&&p2<100&&isprime(p1+p2-1),print(p1," ",p2," ",p1+p2-1)))</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Special_neighbor_primes
use strict; # https://rosettacode.org/wiki/Special_neighbor_primes
Line 384: Line 750:
is_prime( $@ = $primes[$_-1] + $primes[$_] - 1 ) and
is_prime( $@ = $primes[$_-1] + $primes[$_] - 1 ) and
printf "%2d + %2d - 1 = %3d\n", $primes[$_-1], $primes[$_], $@;
printf "%2d + %2d - 1 = %3d\n", $primes[$_-1], $primes[$_], $@;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 403: Line 769:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">np</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)+</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">np</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)+</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Line 421: Line 787:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %,d special neighbour primes &lt; %,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %,d special neighbour primes &lt; %,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 446: Line 812:
Found 103,611 special neighbour primes < 10,000,000
Found 103,611 special neighbour primes < 10,000,000
</pre>
</pre>


=={{header|PL/0}}==
PL/0 can only output a single integer per line, so to avoid confusing output, this sample just shows the first prime of each pair.
<br>
This is almost identical to the [[Neighbour primes#PL/0|PL/0 sample in the Neighbour 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 < 100 do begin
n := ( p1 + p2 ) - 1;
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
11
13
19
29
31
41
43
61
67
73
</pre>

=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* FIND SOME PAIRS OF PRIMES P, Q BETWEEN 1 AND 99 SUCH THAT P + Q -1 */
/* IS ALSO A PRIME */

/* CP/M BDOS SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$STRING( .( 0DH, 0AH, '$' ) ); END;
PR$NUMBER: PROCEDURE( N );
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR( 6 ) BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;

/* TASK */

DECLARE FALSE LITERALLY '0';
DECLARE TRUE LITERALLY '0FFH';
DECLARE MAX$LOW$PRIME LITERALLY '99';
DECLARE PRIME ( 200 )BYTE;
/* THE SIZE OF PRIME SHOULD BE AT LEAST MAX$LOW$PRIME DOUBLED */
/* SIEVE THE PRIMES TO MAX$PRIME */
DECLARE ( P, Q, COUNT ) ADDRESS;
PRIME( 1 ) = FALSE; PRIME( 2 ) = TRUE;
DO P = 3 TO LAST( PRIME ) BY 2; PRIME( P ) = TRUE; END;
DO P = 4 TO LAST( PRIME ) BY 2; PRIME( P ) = FALSE; END;
DO P = 3 TO MAX$LOW$PRIME + 1;
IF PRIME( P ) THEN DO;
DO Q = P * P TO LAST( PRIME ) BY P + P; PRIME( Q ) = FALSE; END;
END;
END;
/* FIND AND SHOW THE SPECIAL NEIGHBOUR PRIMES */
COUNT = 0;
P = 2;
Q = 3;
DO WHILE Q < MAX$LOW$PRIME;
IF PRIME( Q ) THEN DO;
DECLARE SNP ADDRESS;
SNP = P + Q - 1;
IF PRIME( SNP ) THEN DO;
/* P AND Q ARE SPECIAL NEIGHBOUR PRIMES */
CALL PR$STRING( .'( $' );
IF P < 10 THEN CALL PR$CHAR( ' ' );
CALL PR$NUMBER( P );
CALL PR$STRING( .' + $' );
IF Q < 10 THEN CALL PR$CHAR( ' ' );
CALL PR$NUMBER( Q );
CALL PR$STRING( .' ) - 1 = $' );
IF SNP < 100 THEN CALL PR$CHAR( ' ' );
IF SNP < 10 THEN CALL PR$CHAR( ' ' );
CALL PR$NUMBER( SNP );
CALL PR$NL;
END;
P = Q;
END;
Q = Q + 2;
END;

EOF
</syntaxhighlight>
{{out}}
<pre>
( 3 + 5 ) - 1 = 7
( 5 + 7 ) - 1 = 11
( 7 + 11 ) - 1 = 17
( 11 + 13 ) - 1 = 23
( 13 + 17 ) - 1 = 29
( 19 + 23 ) - 1 = 41
( 29 + 31 ) - 1 = 59
( 31 + 37 ) - 1 = 67
( 41 + 43 ) - 1 = 83
( 43 + 47 ) - 1 = 89
( 61 + 67 ) - 1 = 127
( 67 + 71 ) - 1 = 137
( 73 + 79 ) - 1 = 151
</pre>

=={{header|Python}}==
<syntaxhighlight lang="python">#!/usr/bin/python

def isPrime(n):
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True

def nextPrime(n):
#finds the next prime after n
if n == 0:
return 2
if n < 3:
return n + 1
q = n + 2
while not isPrime(q):
q += 2
return q


if __name__ == "__main__":
for p1 in range(3,100,2):
p2 = nextPrime(p1)
if isPrime(p1) and p2 < 100 and isPrime(p1 + p2 - 1):
print(p1,'\t', p2,'\t', p1 + p2 - 1)</syntaxhighlight>
{{out}}
<pre>3 5 7
5 7 11
7 11 17
11 13 23
13 17 29
19 23 41
29 31 59
31 37 67
41 43 83
43 47 89
61 67 127
67 71 137
73 79 151</pre>


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6># 20210809 Raku programming solution
<syntaxhighlight lang="raku" line># 20210809 Raku programming solution


for (grep {.is-prime}, 3..*).rotor(2 => -1) -> (\P1,\P2) {
for (grep {.is-prime}, 3..*).rotor(2 => -1) -> (\P1,\P2) {
last if P2 ≥ Ⅽ;
last if P2 ≥ Ⅽ;
($_ = P1+P2-1).is-prime and printf "%2d, %2d => %3d\n", P1, P2, $_
($_ = P1+P2-1).is-prime and printf "%2d, %2d => %3d\n", P1, P2, $_
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> 3, 5 => 7
<pre> 3, 5 => 7
Line 471: Line 1,022:
=={{header|REXX}}==
=={{header|REXX}}==
A little extra code was added to present the results in a grid-like format.
A little extra code was added to present the results in a grid-like format.
<lang rexx>/*REXX pgm finds special neighbor primes: P1, P2, P1+P2-1 are prime, and P1 and P2<100*/
<syntaxhighlight lang="rexx">/*REXX pgm finds special neighbor primes: P1, P2, P1+P2-1 are prime, and P1 and P2<100*/
parse arg hi cols . /*obtain optional argument from the CL.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 100 /*Not specified? Then use the default.*/
if hi=='' | hi=="," then hi= 100 /*Not specified? Then use the default.*/
Line 517: Line 1,068:
end /*k*/ /* [↑] only process numbers ≤ √ J */
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</lang>
end /*j*/; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 531: Line 1,082:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"


Line 552: Line 1,103:
see "Found " + row + " special neighbor primes"
see "Found " + row + " special neighbor primes"
see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 572: Line 1,123:
Found 13 special neighbor primes
Found 13 special neighbor primes
done...
done...
</pre>

=={{header|RPL}}==
{{works with|HP|49}}
≪ → max
≪ <span style="color:red">{ } 3 5</span>
'''DO'''
'''IF''' DUP2 + <span style="color:red">1</span> - ISPRIME? '''THEN''' DUP2 R→C <span style="color:red">4</span> ROLL SWAP + UNROT '''END'''
NIP DUP NEXTPRIME
'''UNTIL''' DUP max ≥ '''END'''
DROP2
≫ ≫ '<span style="color:blue">SNP</span>' STO

100 <span style="color:blue">SNP</span>
{{out}}
<pre>
1: { (3.,5.) (5.,7.) (7.,11.) (11.,13.) (13.,17.) (19.,23.) (29.,31.) (31.,37.) (41.,43.) (43.,47.) (61.,67.) (67.,71.) (73.,79.) }
</pre>

=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'

Prime.each(100).each_cons(2).select{|p1, p2|(p1+p2-1).prime?}.each{|ar| p ar}</syntaxhighlight>
{{out}}
<pre>[3, 5]
[5, 7]
[7, 11]
[11, 13]
[13, 17]
[19, 23]
[29, 31]
[31, 37]
[41, 43]
[43, 47]
[61, 67]
[67, 71]
[73, 79]
</pre>
</pre>


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func special_neighbor_primes(upto) {
<syntaxhighlight lang="ruby">func special_neighbor_primes(upto) {
var list = []
var list = []
upto.primes.each_cons(2, {|p1,p2|
upto.primes.each_cons(2, {|p1,p2|
Line 599: Line 1,187:
var list = special_neighbor_primes(10**n)
var list = special_neighbor_primes(10**n)
say "Found #{list.len} special neighbour primes < 10^#{n}"
say "Found #{list.len} special neighbour primes < 10^#{n}"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 630: Line 1,218:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
I assume that 'neighbor' primes means pairs of successive primes.
I assume that 'neighbor' primes means pairs of successive primes.
<syntaxhighlight lang="wren">import "./math" for Int

import "./fmt" for Fmt
Anticipating a likely stretch goal.
<lang ecmascript>import "/math" for Int
import "/fmt" for Fmt


var max = 1e7 - 1
var max = 1e7 - 1
Line 656: Line 1,242:
for (i in 3..7) {
for (i in 3..7) {
specialNP.call(10.pow(i), false)
specialNP.call(10.pow(i), false)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 689: Line 1,275:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func IsPrime(N); \Return 'true' if N is a prime number
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
int N, I;
[if N <= 1 then return false;
[if N <= 1 then return false;
Line 710: Line 1,296:
];
];
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}

Latest revision as of 14:44, 21 April 2024

Special neighbor primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Let   (p1,  p2)   are neighbor primes.

Find and show here in base ten if   p1+ p2 -1   is prime,   where   p1,   p2  <  100.

11l

Translation of: Nim
F is_prime(n)
   I n == 2
      R 1B
   I n < 2 | n % 2 == 0
      R 0B
   L(i) (3 .. Int(sqrt(n))).step(2)
      I n % i == 0
         R 0B
   R 1B

V primes = (0.<100).filter(n -> is_prime(n))

L(i) 0 .< primes.len - 1
   V p1 = primes[i]
   V p2 = primes[i + 1]
   I is_prime(p1 + p2 - 1)
      print((p1, p2))
Output:
(3, 5)
(5, 7)
(7, 11)
(11, 13)
(13, 17)
(19, 23)
(29, 31)
(31, 37)
(41, 43)
(43, 47)
(61, 67)
(67, 71)
(73, 79)

Action!

INCLUDE "H6:SIEVE.ACT"

INT FUNC GetNextPrime(INT i BYTE ARRAY primes)
  DO
    i==+1
  UNTIL primes(i)
  OD
RETURN (i)

PROC Main()
  DEFINE MAXPRIME="99"
  DEFINE MAX="200"
  BYTE ARRAY primes(MAX+1)
  INT i,p

  Put(125) PutE() ;clear the screen
  Sieve(primes,MAX+1)
  FOR i=2 TO MAXPRIME
  DO
    IF primes(i) THEN
      p=GetNextPrime(i,primes)
      IF p<=MAXPRIME AND primes(i+p-1)=1 THEN
        PrintF("%I+%I-1=%I%E",i,p,i+p-1)
      FI
    FI
  OD
RETURN
Output:

Screenshot from Atari 8-bit computer

3+5-1=7
5+7-1=11
7+11-1=17
11+13-1=23
13+17-1=29
19+23-1=41
29+31-1=59
31+37-1=67
41+43-1=83
43+47-1=89
61+67-1=127
67+71-1=137
73+79-1=151

ALGOL 68

Very similar to The ALGOL 68 sample in the Neighbour primes task

BEGIN  # find adjacent primes p1, p2 such that p1 + p2 - 1 is also prime #
    PR read "primes.incl.a68" PR
    INT max prime = 100;
    []BOOL prime    = PRIMESIEVE ( max prime * 2 );                      # sieve the primes to max prime * 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 - 1 is prime #
    FOR i TO UPB low prime - 1 DO
        IF   INT p1 plus p2 minus 1 = ( low prime[ i ] + low prime[ i + 1 ] ) - 1;
             prime[ p1 plus p2 minus 1 ]
        THEN print( ( "(",         whole( low prime[ i     ], -3 )
                    , " +",        whole( low prime[ i + 1 ], -3 )
                    , " ) - 1 = ", whole( p1 plus p2 minus 1, -3 )
                    , newline
                    )
                  )
        FI
    OD
END
Output:
(  3 +  5 ) - 1 =   7
(  5 +  7 ) - 1 =  11
(  7 + 11 ) - 1 =  17
( 11 + 13 ) - 1 =  23
( 13 + 17 ) - 1 =  29
( 19 + 23 ) - 1 =  41
( 29 + 31 ) - 1 =  59
( 31 + 37 ) - 1 =  67
( 41 + 43 ) - 1 =  83
( 43 + 47 ) - 1 =  89
( 61 + 67 ) - 1 = 127
( 67 + 71 ) - 1 = 137
( 73 + 79 ) - 1 = 151

Arturo

primesBelow100: select 1..100 => prime?

loop 1..dec size primesBelow100 'p [
    p1: primesBelow100\[p-1]
    p2: primesBelow100\[p]
    if prime? dec p1 + p2 ->
        print ["(" p1 "," p2 ")"]
]
Output:
( 3 , 5 ) 
( 5 , 7 ) 
( 7 , 11 ) 
( 11 , 13 ) 
( 13 , 17 ) 
( 19 , 23 ) 
( 29 , 31 ) 
( 31 , 37 ) 
( 41 , 43 ) 
( 43 , 47 ) 
( 61 , 67 ) 
( 67 , 71 ) 
( 73 , 79 )

AWK

# syntax: GAWK -f SPECIAL_NEIGHBOR_PRIMES.AWK
BEGIN {
    start = 3
    stop = 99
    old_prime = 2
    for (n=start; n<=stop; n++) {
      if (is_prime(n) && is_prime(old_prime)) {
        sum = old_prime + n - 1
        if (is_prime(sum)) {
          count++
          printf("%d,%d -> %d\n",old_prime,n,sum)
        }
        old_prime = n
      }
    }
    printf("Special neighbor primes %d-%d: %d\n",start,stop,count)
    exit(0)
}
function is_prime(x,  i) {
    if (x <= 1) {
      return(0)
    }
    for (i=2; i<=int(sqrt(x)); i++) {
      if (x % i == 0) {
        return(0)
      }
    }
    return(1)
}
Output:
3,5 -> 7
5,7 -> 11
7,11 -> 17
11,13 -> 23
13,17 -> 29
19,23 -> 41
29,31 -> 59
31,37 -> 67
41,43 -> 83
43,47 -> 89
61,67 -> 127
67,71 -> 137
73,79 -> 151
Special neighbor primes 3-99: 13

BASIC

FreeBASIC

#include"isprime.bas"

function nextprime( n as uinteger ) as uinteger
    'finds the next prime after n
    if n = 0 then return 2
    if n < 3 then return n + 1
    dim as integer q = n + 2
    while not isprime(q)
        q+=2
    wend
    return q
end function

dim as uinteger p1, p2

for p1 = 3 to 100 step 2
    p2 = nextprime(p1)
    if isprime(p1) andalso p2<100 andalso isprime( p1 + p2 - 1 ) then
        print p1, p2, p1 + p2 - 1
    end if
next p1
Output:

3 5 7 5 7 11 7 11 17 11 13 23 13 17 29 19 23 41 29 31 59 31 37 67 41 43 83 43 47 89 61 67 127 67 71 137 73 79 151

GW-BASIC

Works with: BASICA
10 FOR P = 3 TO 99 STEP 2
20 GOSUB 130
30 IF Q = 0 THEN GOTO 110
40 GOSUB 220
50 IF P2>100 THEN END
60 T = P
70 P = P2 + T - 1
80 GOSUB 130
90 IF Q = 1 THEN PRINT USING "## + ## - 1 = ###";T;P2;P
100 P=T
110 NEXT P
120 END
130 REM tests if a number is prime
140 Q=0
150 IF P=3 THEN Q=1:RETURN
160 I=1
170 I=I+1
180 IF INT(P/I)*I = P THEN RETURN
190 IF I*I<=P THEN GOTO 170
200 Q = 1
210 RETURN
220 REM finds the next prime after P, result in P2
230 IF P = 0 THEN P2 = 2: RETURN
240 IF P<3 THEN P2 = P + 1: RETURN
250 T = P
260 P = P + 1
270 GOSUB 130
280 IF Q = 1 THEN P2 = P: P = T: RETURN
290 GOTO 260
Output:
 3 +  5 - 1 =   7
 5 +  7 - 1 =  11
 7 + 11 - 1 =  17
11 + 13 - 1 =  23
13 + 17 - 1 =  29
19 + 23 - 1 =  41
29 + 31 - 1 =  59
31 + 37 - 1 =  67
41 + 43 - 1 =  83
43 + 47 - 1 =  89
61 + 67 - 1 = 127
67 + 71 - 1 = 137
73 + 79 - 1 = 151

Tiny BASIC

    REM B = SECOND OF THE NEIGBOURING PRIMES
    REM C = P + B - 1
    REM I = index variable
    REM P = INPUT TO NEXTPRIME ROUTINE AND ISPRIME ROUTINE, also first of the two primes
    REM T = Temporary variable, multiple uses
    REM Z = OUTPUT OF ISPRIME, 1=prime, 0=not

    LET P = 1
 20 LET P = P + 2
    IF P > 100 THEN END
    GOSUB 100
    IF Z = 0 THEN GOTO 20
    GOSUB 120
    IF B > 100 THEN END
    LET T = P
    LET P = P + B - 1
    GOSUB 100
    LET C = P
    LET P = T
    IF Z = 0 THEN GOTO 20
    PRINT P," + ",B," - 1 = ", C
    GOTO 20

100 REM PRIMALITY BY TRIAL DIVISION
    LET Z = 1
    LET I = 2
110 IF (P/I)*I = P THEN LET Z = 0
    IF Z = 0 THEN RETURN
    LET I = I + 1
    IF I*I <= P THEN GOTO 110
    RETURN
    
120 REM next prime after P
    IF P < 2 THEN LET B = 2
    IF P = 2 THEN LET B = 3
    IF P < 3 THEN RETURN
    LET T = P
130 LET P = P + 1
    GOSUB 100
    IF Z = 1 THEN GOTO 140
    GOTO 130
140 LET B = P
    LET P = T
    RETURN
Output:

3 + 5 - 1 = 7 5 + 7 - 1 = 11 7 + 11 - 1 = 17 11 + 13 - 1 = 23 13 + 17 - 1 = 29 19 + 23 - 1 = 41 29 + 31 - 1 = 59 31 + 37 - 1 = 67 41 + 43 - 1 = 83 43 + 47 - 1 = 89 61 + 67 - 1 = 127 67 + 71 - 1 = 137 73 + 79 - 1 = 151

C

#include<stdio.h>
#include<stdlib.h>

int isprime( int p ) {
    int i;
    if(p==2) return 1;
    if(!(p%2)) return 0;
    for(i=3; i*i<=p; i+=2) {
       if(!(p%i)) return 0;
    }
    return 1;
}

int nextprime( int p ) {
    int i=0;
    if(p==0) return 2;
    if(p<3) return p+1;
    while(!isprime(++i + p));
    return i+p;
}

int main(void) {
    int p1, p2;
    for(p1=3;p1<=99;p1+=2) {
        p2=nextprime(p1);
        if(p2<100&&isprime(p1)&&isprime(p2+p1-1)) {
            printf( "%d + %d - 1 = %d\n", p1, p2, p1+p2-1 );
        }
    }
    return 0;
}
Output:
3 + 5 - 1 = 7

5 + 7 - 1 = 11 7 + 11 - 1 = 17 11 + 13 - 1 = 23 13 + 17 - 1 = 29 19 + 23 - 1 = 41 29 + 31 - 1 = 59 31 + 37 - 1 = 67 41 + 43 - 1 = 83 43 + 47 - 1 = 89 61 + 67 - 1 = 127 67 + 71 - 1 = 137 73 + 79 - 1 = 151

Delphi

Works with: Delphi version 6.0

Uses the Delphi Prime-Generator Object


procedure SpecialNeighborPrimes(Memo: TMemo);
var I: integer;
var P1,P2: integer;
var Sieve: TPrimeSieve;
begin
Sieve:=TPrimeSieve.Create;
try
{Build more primes than we need}
Sieve.Intialize(200);
{Go through all primes}
for I:=1 to High(Sieve.Primes) do
	begin
	{Get neighbor primes}
	P1:=Sieve.Primes[I-1];
	P2:=Sieve.Primes[I];
	{only test up to 100}
	if P2>=100 then break;
	{if P1+P2-1 is prime then display}
	if Sieve.Flags[P1 + P2 - 1] then Memo.Lines.Add(Format('(%d, %d)',[P1,P2]));
	end;
finally Sieve.Free; end;
end;
Output:
(3, 5)
(5, 7)
(7, 11)
(11, 13)
(13, 17)
(19, 23)
(29, 31)
(31, 37)
(41, 43)
(43, 47)
(61, 67)
(67, 71)
(73, 79)
Elapsed Time: 9.926 ms.


F#

This task uses Extensible Prime Generator (F#)

// Special neighbor primes. Nigel Galloway: August 6th., 2021
pCache|>Seq.pairwise|>Seq.takeWhile(snd>>(>)100)|>Seq.filter(fun(n,g)->isPrime(n+g-1))|>Seq.iter(printfn "%A")
Output:
(3, 5)
(5, 7)
(7, 11)
(11, 13)
(13, 17)
(19, 23)
(29, 31)
(31, 37)
(41, 43)
(43, 47)
(61, 67)
(67, 71)
(73, 79)

Factor

Works with: Factor version 0.99 2021-06-02
USING: kernel lists lists.lazy math math.primes
math.primes.lists prettyprint sequences ;

lprimes dup cdr lzip [ sum 1 - prime? ] lfilter
[ second 100 < ] lwhile [ . ] leach
Output:
{ 3 5 }
{ 5 7 }
{ 7 11 }
{ 11 13 }
{ 13 17 }
{ 19 23 }
{ 29 31 }
{ 31 37 }
{ 41 43 }
{ 43 47 }
{ 61 67 }
{ 67 71 }
{ 73 79 }

Fermat

Func Nextprime(p) =
    q:=1;
    while not Isprime(p+q)=1 do
        q:=q + 1;
    od;
    p+q.;
    
for p1 = 3 to 99 by 2 do
    p2:=Nextprime(p1);
    if p2<100 and Isprime(p1)=1 and Isprime(p1+p2-1) then
       !!(p1,' +',p2,' - 1 =',p1+p2-1);
    fi;
od;
Output:

3 +  5 - 1 =  7
5 +  7 - 1 =  11
7 +  11 - 1 =  17
11 +  13 - 1 =  23
13 +  17 - 1 =  29
19 +  23 - 1 =  41
29 +  31 - 1 =  59
31 +  37 - 1 =  67
41 +  43 - 1 =  83
43 +  47 - 1 =  89
61 +  67 - 1 =  127
67 +  71 - 1 =  137
73 +  79 - 1 =  151

Go

Translation of: Wren
Library: Go-rcu
package main

import (
    "fmt"
    "rcu"
)

const MAX = 1e7 - 1

var primes = rcu.Primes(MAX)

func specialNP(limit int, showAll bool) {
    if showAll {
        fmt.Println("Neighbor primes, p1 and p2, where p1 + p2 - 1 is prime:")
    }
    count := 0
    for i := 1; i < len(primes); i++ {
        p2 := primes[i]
        if p2 >= limit {
            break
        }
        p1 := primes[i-1]
        p3 := p1 + p2 - 1
        if rcu.IsPrime(p3) {
            if showAll {
                fmt.Printf("(%2d, %2d) => %3d\n", p1, p2, p3)
            }
            count++
        }
    }
    ccount := rcu.Commatize(count)
    climit := rcu.Commatize(limit)
    fmt.Printf("\nFound %s special neighbor primes under %s.\n", ccount, climit)
}

func main() {
    specialNP(100, true)
    var pow = 1000
    for i := 3; i < 8; i++ {
        specialNP(pow, false)
        pow *= 10
    }
}
Output:
Neighbor primes, p1 and p2, where p1 + p2 - 1 is prime:
( 3,  5) =>   7
( 5,  7) =>  11
( 7, 11) =>  17
(11, 13) =>  23
(13, 17) =>  29
(19, 23) =>  41
(29, 31) =>  59
(31, 37) =>  67
(41, 43) =>  83
(43, 47) =>  89
(61, 67) => 127
(67, 71) => 137
(73, 79) => 151

Found 13 special neighbor primes under 100.

Found 71 special neighbor primes under 1,000.

Found 367 special neighbor primes under 10,000.

Found 2,165 special neighbor primes under 100,000.

Found 14,526 special neighbor primes under 1,000,000.

Found 103,611 special neighbor primes under 10,000,000.

J

   (#~ 1 p: {:"1) 2 (, _1 + +/)\ i.&.(p:inv) 100
 3  5   7
 5  7  11
 7 11  17
11 13  23
13 17  29
19 23  41
29 31  59
31 37  67
41 43  83
43 47  89
61 67 127
67 71 137
73 79 151

jq

Works with: jq

Works with gojq, the Go implementation of jq

This entry uses `is_prime` as defined at Erdős-primes#jq.

# Assumes . > 2
def next_prime:
  first(range(.+2; infinite) | select(is_prime));
  
def specialNP($savePairs):
  . as $limit
  | {p1: 2, p2: 3}
  | until( .p2 >= $limit;
      if (.p1 + .p2 - 1 | is_prime)
      then .pcount += 1
      | if $savePairs then .neighbors = .neighbors + [[.p1, .p2]] else . end
      else .
      end
      | .p1 = .p2
      | .p2 = (.p1|next_prime)
      )
  | if $savePairs then {pcount, neighbors} else {pcount} end;

100|specialNP(true)
Output:
{"pcount":13,"neighbors":[[3,5],[5,7],[7,11],[11,13],[13,17],[19,23],[29,31],[31,37],[41,43],[43,47],[61,67],[67,71],[73,79]]}

Julia

using Primes

function specialneighbors(N, savepairs=true)
    neighbors, p1, pcount = Pair{Int}[], 2, 0
    while (p2 = nextprime(p1 + 1)) < N
        if isprime(p2 + p1 - 1)
            savepairs && push!(neighbors, p1 => p2)
            pcount += 1
        end
        p1 = p2
    end
    return neighbors, pcount
end

spn, n = specialneighbors(100)
println("$n special neighbor prime pairs under 100:")
println("p1   p2   p1 + p2 - 1\n--------------------------")
for (p1, p2) in specialneighbors(100)[1]
    println(lpad(p1, 2), "   ", rpad(p2, 7), p1 + p2 - 1)
end

print("\nCount of such prime pairs under 1,000,000,000: ",
    specialneighbors(1_000_000_000, false)[2])
Output:
13 special neighbor prime pairs under 100:
p1   p2   p1 + p2 - 1
--------------------------
 3   5      7
 5   7      11
 7   11     17
11   13     23
13   17     29
19   23     41
29   31     59
31   37     67
41   43     83
43   47     89
61   67     127
67   71     137
73   79     151

Count of such prime pairs under 1,000,000,000: 6041231

Mathematica/Wolfram Language

p = Prime@Range@PrimePi[100];
Select[Partition[p, 2, 1], Total/*(# - 1 &)/*PrimeQ]
Output:
{{3, 5}, {5, 7}, {7, 11}, {11, 13}, {13, 17}, {19, 23}, {29, 31}, {31, 37}, {41, 43}, {43, 47}, {61, 67}, {67, 71}, {73, 79}}

Nim

import strutils, sugar

const Max = 100 - 1

func isPrime(n: Positive): bool =
  if n == 1: return false
  if n mod 2 == 0: return n == 2
  for d in countup(3, n, 2):
    if d * d > n: break
    if n mod d == 0: return false
  result = true

const Primes = collect(newSeq):
                 for n in 2..Max:
                   if n.isPrime: n

let list = collect(newSeq):
             for i in 0..<Primes.high:
               let p1 = Primes[i]
               let p2 = Primes[i + 1]
               if (p1 + p2 - 1).isPrime: (p1, p2)

echo "Found $1 special neighbor primes less than $2:".format(list.len, Max + 1)
echo list.join(", ")
Output:
Found 13 special neighbor primes less than 100:
(3, 5), (5, 7), (7, 11), (11, 13), (13, 17), (19, 23), (29, 31), (31, 37), (41, 43), (43, 47), (61, 67), (67, 71), (73, 79)

PARI/GP

for(p1=1,100,p2=nextprime(p1+1); if(isprime(p1)&&p2<100&&isprime(p1+p2-1),print(p1," ",p2," ",p1+p2-1)))

Perl

#!/usr/bin/perl

use strict; # https://rosettacode.org/wiki/Special_neighbor_primes
use warnings;
use ntheory qw( primes is_prime );

my @primes = @{ primes(100) };
for ( 1 .. $#primes )
  {
  is_prime( $@ = $primes[$_-1] + $primes[$_] - 1 ) and
    printf "%2d + %2d - 1 = %3d\n", $primes[$_-1], $primes[$_], $@;
  }
Output:
 3 +  5 - 1 =   7
 5 +  7 - 1 =  11
 7 + 11 - 1 =  17
11 + 13 - 1 =  23
13 + 17 - 1 =  29
19 + 23 - 1 =  41
29 + 31 - 1 =  59
31 + 37 - 1 =  67
41 + 43 - 1 =  83
43 + 47 - 1 =  89
61 + 67 - 1 = 127
67 + 71 - 1 = 137
73 + 79 - 1 = 151

Phix

with javascript_semantics
function np(integer n) return is_prime(get_prime(n)+get_prime(n+1)-1) end function
function npt(integer p) return filter(tagset(length(get_primes_le(p))-1),np) end function
sequence s = npt(100)
printf(1,"Found %d special neighbour primes < 100:\n",length(s))
for i=1 to length(s) do
    integer si = s[i],
            pi = get_prime(si),
            pj = get_prime(si+1)
    printf(1," (%2d,%2d) => %d\n",{pi,pj,pi+pj-1})
end for
printf(1,"\n")
for i=2 to 7 do
    integer p = power(10,i),
            l = length(npt(p))
    printf(1,"Found %,d special neighbour primes < %,d\n",{l,p})
end for
Output:
Found 13 special neighbour primes < 100:
 ( 3, 5) => 7
 ( 5, 7) => 11
 ( 7,11) => 17
 (11,13) => 23
 (13,17) => 29
 (19,23) => 41
 (29,31) => 59
 (31,37) => 67
 (41,43) => 83
 (43,47) => 89
 (61,67) => 127
 (67,71) => 137
 (73,79) => 151

Found 13 special neighbour primes < 100
Found 71 special neighbour primes < 1,000
Found 367 special neighbour primes < 10,000
Found 2,165 special neighbour primes < 100,000
Found 14,526 special neighbour primes < 1,000,000
Found 103,611 special neighbour primes < 10,000,000


PL/0

PL/0 can only output a single integer per line, so to avoid confusing output, this sample just shows the first prime of each pair.
This is almost identical to the PL/0 sample in the Neighbour primes task

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 < 100 do begin
        n := ( p1 + p2 ) - 1;
        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.
Output:
          3
          5
          7
         11
         13
         19
         29
         31
         41
         43
         61
         67
         73

PL/M

Works with: 8080 PL/M Compiler

... under CP/M (or an emulator)

100H: /* FIND SOME PAIRS OF PRIMES P, Q BETWEEN 1 AND 99 SUCH THAT P + Q -1 */
      /* IS ALSO A PRIME                                                    */

   /* CP/M BDOS SYSTEM CALL AND I/O ROUTINES                                */
   BDOS: PROCEDURE( FN, ARG );   DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
   PR$CHAR:   PROCEDURE( C ); DECLARE C BYTE;    CALL BDOS( 2, C );    END;
   PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S );    END;
   PR$NL:     PROCEDURE; CALL PR$STRING( .( 0DH, 0AH, '$' ) );         END;
   PR$NUMBER: PROCEDURE( N );
      DECLARE N ADDRESS;
      DECLARE V ADDRESS, N$STR( 6 ) BYTE, W BYTE;
      V = N;
      W = LAST( N$STR );
      N$STR( W ) = '$';
      N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
      DO WHILE( ( V := V / 10 ) > 0 );
         N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
      END;
      CALL PR$STRING( .N$STR( W ) );
   END PR$NUMBER;

   /* TASK                                                                  */

   DECLARE FALSE           LITERALLY '0';
   DECLARE TRUE            LITERALLY '0FFH';
   DECLARE MAX$LOW$PRIME   LITERALLY '99';
   DECLARE PRIME ( 200 )BYTE;
   /* THE SIZE OF PRIME SHOULD BE AT LEAST MAX$LOW$PRIME DOUBLED            */
   /* SIEVE THE PRIMES TO MAX$PRIME                                         */
   DECLARE ( P, Q, COUNT ) ADDRESS;
   PRIME( 1 ) = FALSE; PRIME( 2 ) = TRUE;
   DO P = 3 TO LAST( PRIME ) BY 2; PRIME( P ) = TRUE;  END;
   DO P = 4 TO LAST( PRIME ) BY 2; PRIME( P ) = FALSE; END;
   DO P = 3 TO MAX$LOW$PRIME + 1;
      IF PRIME( P ) THEN DO;
         DO Q = P * P TO LAST( PRIME ) BY P + P; PRIME( Q ) = FALSE; END;
      END;
   END;
   /* FIND AND SHOW THE SPECIAL NEIGHBOUR PRIMES                            */
   COUNT = 0;
   P     = 2;
   Q     = 3;
   DO WHILE Q < MAX$LOW$PRIME;
      IF PRIME( Q ) THEN DO;
         DECLARE SNP ADDRESS;
         SNP = P + Q - 1;
         IF PRIME( SNP ) THEN DO;
            /* P AND Q ARE SPECIAL NEIGHBOUR PRIMES                         */
            CALL PR$STRING( .'( $' );
            IF P < 10 THEN CALL PR$CHAR( ' ' );
            CALL PR$NUMBER( P );
            CALL PR$STRING( .' + $' );
            IF Q < 10 THEN CALL PR$CHAR( ' ' );
            CALL PR$NUMBER( Q );
            CALL PR$STRING( .' ) - 1 = $' );
            IF SNP < 100 THEN CALL PR$CHAR( ' ' );
            IF SNP <  10 THEN CALL PR$CHAR( ' ' );
            CALL PR$NUMBER( SNP );
            CALL PR$NL;
         END;
         P = Q;
      END;
      Q = Q + 2;
   END;

EOF
Output:
(  3 +  5 ) - 1 =   7
(  5 +  7 ) - 1 =  11
(  7 + 11 ) - 1 =  17
( 11 + 13 ) - 1 =  23
( 13 + 17 ) - 1 =  29
( 19 + 23 ) - 1 =  41
( 29 + 31 ) - 1 =  59
( 31 + 37 ) - 1 =  67
( 41 + 43 ) - 1 =  83
( 43 + 47 ) - 1 =  89
( 61 + 67 ) - 1 = 127
( 67 + 71 ) - 1 = 137
( 73 + 79 ) - 1 = 151

Python

#!/usr/bin/python

def isPrime(n):
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False        
    return True

def nextPrime(n):
    #finds the next prime after n
    if n == 0:
        return 2
    if n < 3:
        return n + 1
    q = n + 2
    while not isPrime(q):
        q += 2
    return q


if __name__ == "__main__":
    for p1 in range(3,100,2):
        p2 = nextPrime(p1)
        if isPrime(p1) and p2 < 100 and isPrime(p1 + p2 - 1):
            print(p1,'\t', p2,'\t', p1 + p2 - 1)
Output:
3 	 5 	 7
5 	 7 	 11
7 	 11 	 17
11 	 13 	 23
13 	 17 	 29
19 	 23 	 41
29 	 31 	 59
31 	 37 	 67
41 	 43 	 83
43 	 47 	 89
61 	 67 	 127
67 	 71 	 137
73 	 79 	 151

Raku

# 20210809 Raku programming solution 

for (grep {.is-prime}, 3..*).rotor(2 => -1) -> (\P1,\P2) {
   last if P2;
   ($_ = P1+P2-1).is-prime and printf "%2d, %2d => %3d\n", P1, P2, $_
}
Output:
 3,  5 =>   7
 5,  7 =>  11
 7, 11 =>  17
11, 13 =>  23
13, 17 =>  29
19, 23 =>  41
29, 31 =>  59
31, 37 =>  67
41, 43 =>  83
43, 47 =>  89
61, 67 => 127
67, 71 => 137
73, 79 => 151

REXX

A little extra code was added to present the results in a grid-like format.

/*REXX pgm finds special neighbor primes:  P1, P2, P1+P2-1  are prime, and P1 and P2<100*/
parse arg hi cols .                              /*obtain optional argument from the CL.*/
if   hi=='' |   hi==","  then   hi=  100         /*Not specified?  Then use the default.*/
if cols=='' | cols==","  then cols=    5         /* "      "         "   "   "     "    */
call genP hi                                     /*build semaphore array for low primes.*/
     do p=1  while @.p<hi
     end  /*p*/;           lim= p-1;   q= p+1    /*set LIM to prime for P; calc. 2nd HI.*/
#m= # - 1
call genP @.# + @.#m  -  1                       /*build semaphore array for high primes*/
w= 20                                            /*width of a number in any column.     */
title= ' special neighbor primes:  P1, P2, P1+P2-1  are primes,  and P1 and P2 < ' ,
                                                                      commas(hi)
if cols>0 then say ' index │'center(title,   1 + cols*(w+1)     )
if cols>0 then say '───────┼'center(""   ,   1 + cols*(w+1), '─')
found= 0;                  idx= 1                /*initialize # neighbor primes & index.*/
$=                                               /*a list of  neighbor  primes (so far).*/
     do j=1  to  lim;      jp= j+1;   q= @.jp    /*look for neighbor primes within range*/
     y= @.j + q  -  1;     if \!.y  then iterate /*is X also a prime?  No, then skip it.*/
     found= found + 1                            /*bump the number of  neighbor primes. */
     if cols==0            then iterate          /*Build the list  (to be shown later)? */
     $= $  right( @.j','q"──►"y, w)              /*add neighbor prime ──► the  $  list. */
     if found//cols\==0  then iterate            /*have we populated a line of output?  */
     say center(idx, 7)'│'  substr($, 2);   $=   /*display what we have so far  (cols). */
     idx= idx + cols                             /*bump the  index  count for the output*/
     end   /*j*/

if $\==''  then say center(idx, 7)"│"  substr($, 2)  /*possible display residual output.*/
if cols>0 then say '───────┴'center(""                         ,  1 + cols*(w+1), '─')
say
say 'Found '       commas(found)      title
exit 0                                           /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?;  do jc=length(?)-3  to 1  by -3; ?=insert(',', ?, jc); end;  return ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: !.= 0;            parse arg limit          /*placeholders for primes (semaphores).*/
      @.1=2;  @.2=3;  @.3=5;  @.4=7;  @.5=11     /*define some low primes.              */
      !.2=1;  !.3=1;  !.5=1;  !.7=1;  !.11=1     /*   "     "   "    "     flags.       */
                        #=5;     sq.#= @.# **2   /*number of primes so far;     prime². */
                                                 /* [↓]  generate more  primes  ≤  high.*/
        do j=@.#+2  by 2  to limit               /*find odd primes from here on.        */
        parse var j '' -1 _;       if    _==5  then iterate  /*J ÷ by 5?  (right digit).*/
        if j//3==0  then iterate;  if j//7==0  then iterate  /*" "  " 3?     J ÷ by 7?  */
               do k=5  while sq.k<=j             /* [↓]  divide by the known odd primes.*/
               if j // @.k == 0  then iterate j  /*Is  J ÷ X?  Then not prime.     ___  */
               end   /*k*/                       /* [↑]  only process numbers  ≤  √ J   */
        #= #+1;   @.#= j;    sq.#= j*j;   !.j= 1 /*bump # of Ps; assign next P;  P²; P# */
        end          /*j*/;               return
output   when using the default inputs:
 index │               special neighbor primes:  P1, P2, P1+P2-1  are primes,  and P1 and P2 <  100
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │              3,5──►7             5,7──►11            7,11──►17           11,13──►23           13,17──►29
   6   │           19,23──►41           29,31──►59           31,37──►67           41,43──►83           43,47──►89
  11   │          61,67──►127          67,71──►137          73,79──►151
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────

Found  13  special neighbor primes:  P1, P2, P1+P2-1  are primes,  and P1 and P2 <  100

Ring

load "stdlib.ring"

see "working..." + nl
see "Special neighbor primes are:" + nl
row = 0
oldPrime = 2

for n = 3 to 100
    if isprime(n) and isprime(oldPrime) 
       sum = oldPrime + n - 1
       if isprime(sum)
          row++
          see "" + oldPrime + "," + n + " => " + sum + nl
       ok
       oldPrime = n
    ok
next

see "Found " + row + " special neighbor primes"
see "done..." + nl
Output:
working...
Special neighbor primes are:
3,5 => 7
5,7 => 11
7,11 => 17
11,13 => 23
13,17 => 29
19,23 => 41
29,31 => 59
31,37 => 67
41,43 => 83
43,47 => 89
61,67 => 127
67,71 => 137
73,79 => 151
Found 13 special neighbor primes
done...

RPL

Works with: HP version 49
≪ → max
  ≪ { } 3 5
     DO
       IF DUP2 + 1 - ISPRIME? THEN DUP2 R→C 4 ROLL SWAP + UNROT END
       NIP DUP NEXTPRIME
     UNTIL DUP max ≥ END
     DROP2
≫ ≫ 'SNP' STO  
100 SNP
Output:
1: { (3.,5.) (5.,7.) (7.,11.) (11.,13.) (13.,17.) (19.,23.) (29.,31.) (31.,37.) (41.,43.) (43.,47.) (61.,67.) (67.,71.) (73.,79.) }

Ruby

require 'prime'

Prime.each(100).each_cons(2).select{|p1, p2|(p1+p2-1).prime?}.each{|ar| p ar}
Output:
[3, 5]
[5, 7]
[7, 11]
[11, 13]
[13, 17]
[19, 23]
[29, 31]
[31, 37]
[41, 43]
[43, 47]
[61, 67]
[67, 71]
[73, 79]

Sidef

func special_neighbor_primes(upto) {
    var list = []
    upto.primes.each_cons(2, {|p1,p2|
        var n = (p1 + p2 - 1)
        if (n.is_prime) {
            list << [p1, p2, n]
        }
    })
    return list
}

with (100) {|n|
    var list = special_neighbor_primes(n)
    say "Found #{list.len} special neighbour primes < n:"
    list.each_2d {|p1,p2,q|
        printf(" (%2s, %2s) => %s\n", p1, p2, q)
    }
}

say ''

for n in (1..7) {
    var list = special_neighbor_primes(10**n)
    say "Found #{list.len} special neighbour primes < 10^#{n}"
}
Output:
Found 13 special neighbour primes < n:
 ( 3,  5) => 7
 ( 5,  7) => 11
 ( 7, 11) => 17
 (11, 13) => 23
 (13, 17) => 29
 (19, 23) => 41
 (29, 31) => 59
 (31, 37) => 67
 (41, 43) => 83
 (43, 47) => 89
 (61, 67) => 127
 (67, 71) => 137
 (73, 79) => 151

Found 2 special neighbour primes < 10^1
Found 13 special neighbour primes < 10^2
Found 71 special neighbour primes < 10^3
Found 367 special neighbour primes < 10^4
Found 2165 special neighbour primes < 10^5
Found 14526 special neighbour primes < 10^6
Found 103611 special neighbour primes < 10^7

Wren

Library: Wren-math
Library: Wren-fmt

I assume that 'neighbor' primes means pairs of successive primes.

import "./math" for Int
import "./fmt" for Fmt

var max = 1e7 - 1
var primes = Int.primeSieve(max)

var specialNP = Fn.new { |limit, showAll|
    if (showAll) System.print("Neighbor primes, p1 and p2, where p1 + p2 - 1 is prime:")
    var count = 0
    var p3
    for (i in 1...primes.where { |p| p < limit }.count) {
        var p2 = primes[i]
        var p1 = primes[i-1]
        if (Int.isPrime(p3 = p1 + p2 - 1)) {
            if (showAll) Fmt.print("($2d, $2d) => $3d", p1, p2, p3)
            count = count + 1
        }
    }
    Fmt.print("\nFound $,d special neighbor primes under $,d.", count, limit)
}

specialNP.call(100, true)
for (i in 3..7) {
    specialNP.call(10.pow(i), false)
}
Output:
Neighbor primes, p1 and p2, where p1 + p2 - 1 is prime:
( 3,  5) =>   7
( 5,  7) =>  11
( 7, 11) =>  17
(11, 13) =>  23
(13, 17) =>  29
(19, 23) =>  41
(29, 31) =>  59
(31, 37) =>  67
(41, 43) =>  83
(43, 47) =>  89
(61, 67) => 127
(67, 71) => 137
(73, 79) => 151

Found 13 special neighbor primes under 100.

Found 71 special neighbor primes under 1,000.

Found 367 special neighbor primes under 10,000.

Found 2,165 special neighbor primes under 100,000.

Found 14,526 special neighbor primes under 1,000,000.

Found 103,611 special neighbor primes under 10,000,000.

XPL0

func IsPrime(N);        \Return 'true' if N is a prime number
int  N, I;
[if N <= 1 then return false;
for I:= 2 to sqrt(N) do
    if rem(N/I) = 0 then return false;
return true;
];

int P, P1, P2;
[P:= 2;
loop    [P1:= P;
        repeat  P:= P+1;
                if P >= 100 then quit;
        until   IsPrime(P);
        P2:= P;
        if IsPrime(P1+P2-1) then
                [IntOut(0, P1);  ChOut(0, ^ );
                 IntOut(0, P2);  ChOut(0, ^ );
                 IntOut(0, P1+P2-1);  CrLf(0);
                ];
        ];
]
Output:
3 5 7
5 7 11
7 11 17
11 13 23
13 17 29
19 23 41
29 31 59
31 37 67
41 43 83
43 47 89
61 67 127
67 71 137
73 79 151