Sum of two adjacent numbers are primes: Difference between revisions

Added Easylang
m (→‎{{header|Phix}}: extra credit)
(Added Easylang)
 
(36 intermediate revisions by 19 users not shown)
Line 3:
;Task:
<br>
Show on this page the first 20 numbersprime and sumsums of two adjacentconsecutive numbers which sum is primeintegers.
;Extra credit
Show the ten millionth such prime sum.
<br>
 
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<syntaxhighlight lang="action!">
;;; Find numbers n such that n + n+1 is prime
;;; Library: Action! Sieve of Eratosthenes
INCLUDE "H6:SIEVE.ACT"
 
PROC Main()
DEFINE MAX_PRIME = "255"
 
BYTE ARRAY primes(MAX_PRIME)
CARD n, n1, count
Sieve(primes,MAX_PRIME)
 
count = 0;
n1 = 1
WHILE count < 20 DO
n = n1
n1 ==+ 1
IF primes( n + n1 ) THEN
count ==+ 1
PrintF( "%2U: %2U + %2U = %2U%E", count, n, n1, n + n1 )
FI
OD
 
RETURN
</syntaxhighlight>
{{out}}
<pre>
1: 1 + 2 = 3
2: 2 + 3 = 5
3: 3 + 4 = 7
4: 5 + 6 = 11
5: 6 + 7 = 13
6: 8 + 9 = 17
7: 9 + 10 = 19
8: 11 + 12 = 23
9: 14 + 15 = 29
10: 15 + 16 = 31
11: 18 + 19 = 37
12: 20 + 21 = 41
13: 21 + 22 = 43
14: 23 + 24 = 47
15: 26 + 27 = 53
16: 29 + 30 = 59
17: 30 + 31 = 61
18: 33 + 34 = 67
19: 35 + 36 = 71
20: 36 + 37 = 73
</pre>
 
=={{header|ALGOL 68}}==
Using Pete Lomax's observation that all odd primes are n + n+1 for some n - see [[#Phix]]
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find the first 20 primes which are n + ( n + 1 ) for some n #
PR read "primes.incl.a68" PR # include prime utilities #
[]BOOL prime = PRIMESIEVE 200; # should be enough primes #
INT p count := 0;
FOR np FROM 3 BY 2 WHILE p count < 20 DO
INT n1 = n + 1;
INT p = n + n1;
IF prime[ p ] THEN
INT n = p OVER 2;
INT n1 = n + 1;
p count +:= 1;
print( ( whole( n, -2 ), " + ", whole( n1, -2 ), " = ", whole( p, -3 ), newline ) )
FI
OD
END</lang>
</syntaxhighlight>
{{out}}
<pre>
Line 43 ⟶ 100:
36 + 37 = 73
</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">
begin % find numbers n such that n + n+1 is prime %
 
% sets s to a sieve of primes, using the sieve of Eratosthenes %
procedure sieve( logical array s ( * ); integer value n ) ;
begin
% start with everything flagged as prime %
for i := 1 until n do s( i ) := true;
% sieve out the non-primes %
s( 1 ) := false;
for i := 2 until truncate( sqrt( n ) )
do begin
if s( i )
then begin
for p := i * i step i until n do s( p ) := false
end if_s_i
end for_i ;
end sieve ;
 
integer sieveMax;
sieveMax := 200;
begin
logical array prime ( 1 :: sieveMax );
integer count, n, n1;
i_w := 2; % set output integer field width %
s_w := 1; % and output separator width %
 
% find and display the numbers %
sieve( prime, sieveMax );
count := 0;
n1 := 1;
while count < 20 do begin
n := n1;
n1 := n1 + 1;
if prime( n + n1 ) then begin
count := count + 1;
write( count, ": ", n, " + ", n1, " = ", n + n1 )
end if_prime__n_plus_n1
end while_count_lt_20
end
 
end.
</syntaxhighlight>
{{out}}
<pre>
1 : 1 + 2 = 3
2 : 2 + 3 = 5
3 : 3 + 4 = 7
4 : 5 + 6 = 11
5 : 6 + 7 = 13
6 : 8 + 9 = 17
7 : 9 + 10 = 19
8 : 11 + 12 = 23
9 : 14 + 15 = 29
10 : 15 + 16 = 31
11 : 18 + 19 = 37
12 : 20 + 21 = 41
13 : 21 + 22 = 43
14 : 23 + 24 = 47
15 : 26 + 27 = 53
16 : 29 + 30 = 59
17 : 30 + 31 = 61
18 : 33 + 34 = 67
19 : 35 + 36 = 71
20 : 36 + 37 = 73
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">loop.with:'i
select.first:20 1..∞ 'x -> prime? x + x+1
'x -> print [
(pad to :string i+1 2)++":"
(pad to :string x 2) "+" (pad.right to :string x+1 2) "=" x + x+1
]</syntaxhighlight>
 
{{out}}
 
<pre> 1: 1 + 2 = 3
2: 2 + 3 = 5
3: 3 + 4 = 7
4: 5 + 6 = 11
5: 6 + 7 = 13
6: 8 + 9 = 17
7: 9 + 10 = 19
8: 11 + 12 = 23
9: 14 + 15 = 29
10: 15 + 16 = 31
11: 18 + 19 = 37
12: 20 + 21 = 41
13: 21 + 22 = 43
14: 23 + 24 = 47
15: 26 + 27 = 53
16: 29 + 30 = 59
17: 30 + 31 = 61
18: 33 + 34 = 67
19: 35 + 36 = 71
20: 36 + 37 = 73</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SUM_OF_TWO_ADJACENT_NUMBERS_ARE_PRIMES.AWK
BEGIN {
n = 1
stop = 20
printf("The first %d pairs of numbers whose sum is prime:\n",stop)
while (count < stop) {
if (is_prime(2*n + 1)) {
printf("%2d + %2d = %2d\n",n,n+1,2*n+1)
count++
}
n++
}
exit(0)
}
function is_prime(n, d) {
d = 5
if (n < 2) { return(0) }
if (n % 2 == 0) { return(n == 2) }
if (n % 3 == 0) { return(n == 3) }
while (d*d <= n) {
if (n % d == 0) { return(0) }
d += 2
if (n % d == 0) { return(0) }
d += 4
}
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
The first 20 pairs of numbers whose sum is prime:
1 + 2 = 3
2 + 3 = 5
3 + 4 = 7
5 + 6 = 11
6 + 7 = 13
8 + 9 = 17
9 + 10 = 19
11 + 12 = 23
14 + 15 = 29
15 + 16 = 31
18 + 19 = 37
20 + 21 = 41
21 + 22 = 43
23 + 24 = 47
26 + 27 = 53
29 + 30 = 59
30 + 31 = 61
33 + 34 = 67
35 + 36 = 71
36 + 37 = 73
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
if v mod 3 = 0 then return v = 3
d = 5
while d * d <= v
if v mod d = 0 then return False else d += 2
end while
return True
end function
 
n = 0
num = 0
 
print "The first 20 pairs of numbers whose sum is prime:"
while True
n += 1
suma = 2*n+1
if isPrime(suma) then
num += 1
if num < 21 then
print n; " + "; (n+1); " = "; suma
else
exit while
end if
end if
end while
end</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">Function isPrime(Byval ValorEval As Integer) As Boolean
If ValorEval <= 1 Then Return False
For i As Integer = 2 To Int(Sqr(ValorEval))
If ValorEval Mod i = 0 Then Return False
Next i
Return True
End Function
 
Dim As Integer n = 0, num = 0, suma
print "The first 20 pairs of numbers whose sum is prime:"
Do
n += 1
suma = 2*n+1
If isPrime(suma) Then
num += 1
If num < 21 Then
Print using "## + ## = ##"; n; (n+1); suma
Else
Exit Do
End If
End If
Loop
Sleep</syntaxhighlight>
{{out}}
<pre>The first 20 pairs of numbers whose sum is prime:
1 + 2 = 3
2 + 3 = 5
3 + 4 = 7
5 + 6 = 11
6 + 7 = 13
8 + 9 = 17
9 + 10 = 19
11 + 12 = 23
14 + 15 = 29
15 + 16 = 31
18 + 19 = 37
20 + 21 = 41
21 + 22 = 43
23 + 24 = 47
26 + 27 = 53
29 + 30 = 59
30 + 31 = 61
33 + 34 = 67
35 + 36 = 71
36 + 37 = 73</pre>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
ElseIf v % 2 = 0 : ProcedureReturn #False
ElseIf v < 9 : ProcedureReturn #True
ElseIf v % 3 = 0 : ProcedureReturn #False
Else
Protected r = Round(Sqr(v), #PB_Round_Down)
Protected f = 5
While f <= r
If v % f = 0 Or v % (f + 2) = 0
ProcedureReturn #False
EndIf
f + 6
Wend
EndIf
ProcedureReturn #True
EndProcedure
 
If OpenConsole()
Define n.i = 0, num.i = 0, suma.i
PrintN("The first 20 pairs of numbers whose sum is prime:")
Repeat
n + 1
suma = 2*n+1
If isPrime(suma)
num + 1
If num < 21
PrintN(Str(n) + " + " + Str(n+1) + " = " + Str(suma))
Else
Break
EndIf
EndIf
ForEver
CloseConsole()
EndIf</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">FUNCTION isPrime (ValorEval)
IF ValorEval <= 1 THEN isPrime = 0
FOR i = 2 TO INT(SQR(ValorEval))
IF ValorEval MOD i = 0 THEN isPrime = 0
NEXT i
isPrime = 1
END FUNCTION
 
n = 0
num = 0
PRINT "The first 20 pairs of numbers whose sum is prime:"
DO
n = n + 1
suma = 2 * n + 1
IF isPrime(suma) THEN
num = num + 1
IF num < 21 THEN
PRINT USING "## + ## = ##"; n; (n + 1); suma
ELSE
EXIT DO
END IF
END IF
LOOP
END</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">FUNCTION isPrime (ValorEval)
IF ValorEval <= 1 THEN LET isPrime = 0
FOR i = 2 TO INT(SQR(ValorEval))
IF MOD(ValorEval, i) = 0 THEN LET isPrime = 0
NEXT i
LET isPrime = 1
END FUNCTION
 
LET n = 0
LET num = 0
PRINT "The first 20 pairs of numbers whose sum is prime:"
DO
LET n = n + 1
LET suma = 2 * n + 1
IF isPrime(suma) = 1 THEN
LET num = num + 1
IF num < 21 THEN
PRINT USING "##": n;
PRINT " + ";
PRINT USING "##": n+1;
PRINT " = ";
PRINT USING "##": suma
ELSE
EXIT DO
END IF
END IF
LOOP
END</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">sub isPrime(v)
if v < 2 then return False : fi
if mod(v, 2) = 0 then return v = 2 : fi
if mod(v, 3) = 0 then return v = 3 : fi
d = 5
while d * d <= v
if mod(v, d) = 0 then return False else d = d + 2 : fi
wend
return True
end sub
 
n = 0
num = 0
print "The first 20 pairs of numbers whose sum is prime:"
do
n = n + 1
suma = 2*n+1
if isPrime(suma) then
num = num + 1
if num < 21 then
print n using "##", " + ", (n+1) using "##", " = ", suma using "##"
else
break
end if
end if
loop
end</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
 
 
=={{header|C}}==
{{trans|WrenGo}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
#define TRUE 1
#define FALSE 0
 
typedef int bool;
int isPrime(int n) {
 
if (n < 2) return FALSE;
void primeSieve(int *c, int limit, bool processEven, bool primesOnly) {
if (!(n%2)) return n == 2;
ifint (!(n%3))i, returnix, np, == 3p2;
int d = 5limit++;
whilec[0] (d*d <= n) {TRUE;
c[1] = TRUE;
if (!(n%d)) return FALSE;
if (processEven) {
d += 2;
iffor (!(n%d)i = 4; i < limit; i +=2) returnc[i] = FALSETRUE;
d += 4;}
p = 3;
while (TRUE) {
p2 = p * p;
if (p2 >= limit) break;
for (i = p2; i < limit; i += 2*p) c[i] = TRUE;
while (TRUE) {
p += 2;
if (!c[p]) break;
}
}
if (primesOnly) {
/* move the actual primes to the front of the array */
c[0] = 2;
for (i = 3, ix = 1; i < limit; i += 2) {
if (!c[i]) c[ix++] = i;
}
}
return TRUE;
}
 
int main() {
int counti, =p, 0hp, n = 110000000;
int limit = (int)(log(n) * (double)n * 1.2); /* should be more than enough */
int *primes = (int *)calloc(limit, sizeof(int));
primeSieve(primes, limit-1, FALSE, TRUE);
printf("The first 20 pairs of natural numbers whose sum is prime are:\n");
whilefor (counti = 1; i <= 20; ++i) {
ifp (isPrime(2*n= + 1)) {primes[i];
printf("%2d + %2dhp = %2d\n",p n, n + 1,/ 2*n + 1);
printf("%2d + %2d = count+%2d\n", hp, hp+1, p);
}
n++;
}
printf("\nThe 10 millionth such pair is:\n");
p = primes[n];
hp = p / 2;
printf("%2d + %2d = %2d\n", hp, hp+1, p);
free(primes);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 101 ⟶ 564:
35 + 36 = 71
36 + 37 = 73
 
The 10 millionth such pair is:
89712345 + 89712346 = 179424691
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
procedure AdjacentPrimeSums(Memo: TMemo);
var I,Sum,Cnt: integer;
var S: string;
begin
Cnt:=0;
S:='';
for I:=0 to High(I) do
if IsPrime(I+I+1) then
begin
Inc(Cnt);
Memo.Lines.Add(Format('%3d %3d+%3d=%4d',[Cnt,I,I+1,I+I+1]));
if Cnt>=20 then break
end;
end;
 
 
 
 
</syntaxhighlight>
{{out}}
<pre>
1: 1 + 2 = 3
2: 2 + 3 = 5
3: 3 + 4 = 7
4: 5 + 6 = 11
5: 6 + 7 = 13
6: 8 + 9 = 17
7: 9 + 10 = 19
8: 11 + 12 = 23
9: 14 + 15 = 29
10: 15 + 16 = 31
11: 18 + 19 = 37
12: 20 + 21 = 41
13: 21 + 22 = 43
14: 23 + 24 = 47
15: 26 + 27 = 53
16: 29 + 30 = 59
17: 30 + 31 = 61
18: 33 + 34 = 67
19: 35 + 36 = 71
20: 36 + 37 = 73
 
Elapsed Time: 25.093 ms.
 
</pre>
 
 
=={{header|EasyLang}}==
{{trans|BASIC256}}
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
print "The first 20 pairs of numbers whose sum is prime:"
repeat
n += 1
sum = 2 * n + 1
if isprim sum = 1
print n & " + " & n + 1 & " = " & sum
cnt += 1
.
until cnt = 20
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// 2n+1 is prime. Nigel Galloway: Januuary 22nd., 2022
primes32()|>Seq.skip 1|>Seq.take 20|>Seq.map(fun n->n/2)|>Seq.iteri(fun n g->printfn "%2d: %2d + %2d=%d" (n+1) g (g+1) (g+g+1))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 132 ⟶ 677:
20: 36 + 37=73
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<syntaxhighlight lang="factor">USING: arrays formatting kernel lists lists.lazy math
math.primes.lists sequences ;
 
20 lprimes cdr [ 2/ dup 1 + 2array ] lmap-lazy ltake
[ dup sum suffix "%d + %d = %d\n" vprintf ] leach</syntaxhighlight>
{{out}}
<pre>
1 + 2 = 3
2 + 3 = 5
3 + 4 = 7
5 + 6 = 11
6 + 7 = 13
8 + 9 = 17
9 + 10 = 19
11 + 12 = 23
14 + 15 = 29
15 + 16 = 31
18 + 19 = 37
20 + 21 = 41
21 + 22 = 43
23 + 24 = 47
26 + 27 = 53
29 + 30 = 59
30 + 31 = 61
33 + 34 = 67
35 + 36 = 71
36 + 37 = 73
</pre>
 
=={{header|Go}}==
{{libheader|Go-rcu}}
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"math"
"rcu"
)
 
func main() {
limit := int(math.Log(1e7) * 1e7 * 1.2) // should be more than enough
primes := rcu.Primes(limit)
fmt.Println("The first 20 pairs of natural numbers whose sum is prime are:")
for i := 1; i <= 20; i++ {
p := primes[i]
hp := p / 2
fmt.Printf("%2d + %2d = %2d\n", hp, hp+1, p)
}
fmt.Println("\nThe 10 millionth such pair is:")
p := primes[1e7]
hp := p / 2
fmt.Printf("%2d + %2d = %2d\n", hp, hp+1, p)
}</syntaxhighlight>
 
{{out}}
<pre>
The first 20 pairs of natural numbers whose sum is prime are:
1 + 2 = 3
2 + 3 = 5
3 + 4 = 7
5 + 6 = 11
6 + 7 = 13
8 + 9 = 17
9 + 10 = 19
11 + 12 = 23
14 + 15 = 29
15 + 16 = 31
18 + 19 = 37
20 + 21 = 41
21 + 22 = 43
23 + 24 = 47
26 + 27 = 53
29 + 30 = 59
30 + 31 = 61
33 + 34 = 67
35 + 36 = 71
36 + 37 = 73
 
The 10 millionth such pair is:
89712345 + 89712346 = 179424691
</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.Numbers.Primes
 
primeSumsOfConsecutiveNumbers :: Integral a => [(a, (a, a))]
primeSumsOfConsecutiveNumbers =
let go a b = [(n, (a, b)) | let n = a + b, isPrime n]
in concat $ zipWith go [1 ..] [2 ..]
 
main :: IO ()
main = mapM_ print $ take 20 primeSumsOfConsecutiveNumbers</syntaxhighlight>
{{Out}}
<pre>(3,(1,2))
(5,(2,3))
(7,(3,4))
(11,(5,6))
(13,(6,7))
(17,(8,9))
(19,(9,10))
(23,(11,12))
(29,(14,15))
(31,(15,16))
(37,(18,19))
(41,(20,21))
(43,(21,22))
(47,(23,24))
(53,(26,27))
(59,(29,30))
(61,(30,31))
(67,(33,34))
(71,(35,36))
(73,(36,37))</pre>
 
=={{header|J}}==
 
Here, we generate the first 20 odd primes, divide by 2, drop the fractional part and add 0 and 1 to that value. Then we format that pair of numbers with their sum and with decorations indicating the relevant arithmetic:
 
<syntaxhighlight lang="j"> (+/,&":'=',{.,&":'+',&":{:)"#. 0 1+/~<.-: p:1+i.20
3=1+2
5=2+3
7=3+4
11=5+6
13=6+7
17=8+9
19=9+10
23=11+12
29=14+15
31=15+16
37=18+19
41=20+21
43=21+22
47=23+24
53=26+27
59=29+30
61=30+31
67=33+34
71=35+36
73=36+37</syntaxhighlight>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
This entry uses a memory-less approach to computing `is_prime`,
and so a naive approach to computing the first 20 numbers satisfying
the condition is appropriate.
<syntaxhighlight lang="jq">def is_prime:
. as $n
| if ($n < 2) then false
elif ($n % 2 == 0) then $n == 2
elif ($n % 3 == 0) then $n == 3
else 5
| until( . <= 0;
if .*. > $n then -1
elif ($n % . == 0) then 0
else . + 2
| if ($n % . == 0) then 0
else . + 4
end
end)
| . == -1
end;
 
def naive:
limit(20;
range(0; infinite) as $i
| (2*$i + 1) as $q
| select($q | is_prime)
| "\($i) + \($i + 1) = \($q)" );
 
naive</syntaxhighlight>
{{out}}
<pre>
1 + 2 = 3
2 + 3 = 5
3 + 4 = 7
5 + 6 = 11
6 + 7 = 13
8 + 9 = 17
9 + 10 = 19
11 + 12 = 23
14 + 15 = 29
15 + 16 = 31
18 + 19 = 37
20 + 21 = 41
21 + 22 = 43
23 + 24 = 47
26 + 27 = 53
29 + 30 = 59
30 + 31 = 61
33 + 34 = 67
35 + 36 = 71
36 + 37 = 73
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Lazy
using Primes
 
sseq = @>> Lazy.range(2) filter(n -> isprime(2n + 1))
for n in take(20, sseq)
println("$n + $(n + 1) = $(n + n + 1)")
end
 
</lang>{{out}}
let
i, cnt = 0, 0
while cnt < 10_000_000
i += 1
if isprime(2i + 1)
cnt += 1
end
end
println("Ten millionth: $i + $(i + 1) = $(i + i + 1)")
end
</syntaxhighlight>{{out}}
<pre>
1 + 2 = 3
Line 162 ⟶ 917:
35 + 36 = 71
36 + 37 = 73
Ten millionth: 89712345 + 89712346 = 179424691
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Column[Row[{Floor[#/2], " + ", Ceiling[#/2], " = ", #}] & /@ Prime[1 + Range[20]]]
Row[{Floor[#/2], " + ", Ceiling[#/2], " = ", #}] &[Prime[10^7 + 1]]</syntaxhighlight>
{{out}}
<pre>1 + 2 = 3
2 + 3 = 5
3 + 4 = 7
5 + 6 = 11
6 + 7 = 13
8 + 9 = 17
9 + 10 = 19
11 + 12 = 23
14 + 15 = 29
15 + 16 = 31
18 + 19 = 37
20 + 21 = 41
21 + 22 = 43
23 + 24 = 47
26 + 27 = 53
29 + 30 = 59
30 + 31 = 61
33 + 34 = 67
35 + 36 = 71
36 + 37 = 73
89712345 + 89712346 = 179424691</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/[bitops, math, strformat, strutils]
 
type Sieve = object
data: seq[byte]
 
func `[]`(sieve: Sieve; idx: Positive): bool =
## Return value of element at index "idx".
let idx = idx shr 1
let iByte = idx shr 3
let iBit = idx and 7
result = sieve.data[iByte].testBit(iBit)
 
func `[]=`(sieve: var Sieve; idx: Positive; val: bool) =
## Set value of element at index "idx".
let idx = idx shr 1
let iByte = idx shr 3
let iBit = idx and 7
if val: sieve.data[iByte].setBit(iBit)
else: sieve.data[iByte].clearBit(iBit)
 
func newSieve(lim: Positive): Sieve =
## Create a sieve with given maximal index.
result.data = newSeq[byte]((lim + 16) shr 4)
 
func initPrimes(lim: Positive): seq[Natural] =
## Initialize the list of primes from 3 to "lim".
var composite = newSieve(lim)
composite[1] = true
for n in countup(3, sqrt(lim.toFloat).int, 2):
if not composite[n]:
for k in countup(n * n, lim, 2 * n):
composite[k] = true
for n in countup(3, lim, 2):
if not composite[n]:
result.add n
 
let primes = initPrimes(200_000_000)
 
echo "The first 20 pairs of natural numbers whose sum is prime are:"
var count = 0
for p in primes:
inc count
if count <= 20:
echo &"{(p-1) div 2} + {(p+1) div 2} = {p}"
if count == 20: echo()
elif count == 10_000_000:
echo "The 10 millionth such pair is:"
echo &"{(p-1) div 2} + {(p+1) div 2} = {p}"
break
</syntaxhighlight>
 
{{out}}
<pre>The first 20 pairs of natural numbers whose sum is prime are:
1 + 2 = 3
2 + 3 = 5
3 + 4 = 7
5 + 6 = 11
6 + 7 = 13
8 + 9 = 17
9 + 10 = 19
11 + 12 = 23
14 + 15 = 29
15 + 16 = 31
18 + 19 = 37
20 + 21 = 41
21 + 22 = 43
23 + 24 = 47
26 + 27 = 53
29 + 30 = 59
30 + 31 = 61
33 + 34 = 67
35 + 36 = 71
36 + 37 = 73
 
The 10 millionth such pair is:
89712345 + 89712346 = 179424691</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory 'is_prime';
 
my($n,$c);
while () { is_prime(1 + 2*++$n) and printf "%2d + %2d = %2d\n", $n, $n+1, 1+2*$n and ++$c == 20 and last }</langsyntaxhighlight>
{{out}}
<pre> 1 + 2 = 3
Line 196 ⟶ 1,056:
=={{header|Phix}}==
''Every'' prime p greater than 2 is odd, hence p/2 is k.5 and the adjacent numbers needed are k and k+1. DOH.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">doh</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
Line 202 ⟶ 1,062:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">21</span><span style="color: #0000FF;">)[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$],</span><span style="color: #000000;">doh</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">doh</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1e7</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 226 ⟶ 1,087:
36 + 37 = 73
89712345 + 89712346 = 179424691
</pre>
 
 
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* FIND NUMBERS N SUCH THAT N + N + 1 IS PRIME */
 
/* CP/M 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$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH */
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;
 
PR$NUMBER2: PROCEDURE( N ); /* PRINT A NUMBER IN AT LEAST 2 POSITIONS */
DECLARE N ADDRESS;
IF N < 10 THEN CALL PR$CHAR( ' ' );
CALL PR$NUMBER( N );
END PR$NUMBER2;
 
/* TASK */
/* ASSUMING THE FIRST 20 SUCH NUMBERS WILL BE < 255 */
 
DECLARE MAX$NUMBER LITERALLY '255'
, FALSE LITERALLY '0'
, TRUE LITERALLY '0FFH'
;
 
DECLARE PRIME ( MAX$NUMBER )BYTE; /* SIEVE THE PRIMES TO MAX$NUMBER - 1 */
DECLARE I ADDRESS;
PRIME( 0 ), PRIME( 1 ) = FALSE;
PRIME( 2 ) = TRUE;
DO I = 3 TO LAST( PRIME ) BY 2; PRIME( I ) = TRUE; END;
DO I = 4 TO LAST( PRIME ) BY 2; PRIME( I ) = FALSE; END;
DO I = 3 TO LAST( PRIME ) BY 2;
IF PRIME( I ) THEN DO;
DECLARE S ADDRESS;
DO S = I + I TO LAST( PRIME ) BY I;
PRIME( S ) = FALSE;
END;
END;
END;
 
/* FIND THE NUMBERS */
DECLARE ( N, N1, COUNT ) BYTE;
COUNT = 0;
N1 = 1;
DO WHILE COUNT < 20;
N = N1;
N1 = N1 + 1;
IF PRIME( N + N1 ) THEN DO;
COUNT = COUNT + 1;
CALL PR$NUMBER2( COUNT );
CALL PR$STRING( .': $' );
CALL PR$NUMBER2( N );
CALL PR$STRING( .' + $' );
CALL PR$NUMBER2( N1 );
CALL PR$STRING( .' = $' );
CALL PR$NUMBER2( N + N1 );
CALL PR$NL;
END;
END;
 
EOF
</syntaxhighlight>
{{out}}
<pre>
1: 1 + 2 = 3
2: 2 + 3 = 5
3: 3 + 4 = 7
4: 5 + 6 = 11
5: 6 + 7 = 13
6: 8 + 9 = 17
7: 9 + 10 = 19
8: 11 + 12 = 23
9: 14 + 15 = 29
10: 15 + 16 = 31
11: 18 + 19 = 37
12: 20 + 21 = 41
13: 21 + 22 = 43
14: 23 + 24 = 47
15: 26 + 27 = 53
16: 29 + 30 = 59
17: 30 + 31 = 61
18: 33 + 34 = 67
19: 35 + 36 = 71
20: 36 + 37 = 73
</pre>
 
=={{header|Python}}==
===Procedural===
<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
 
if __name__ == "__main__":
n = 0
num = 0
 
print('The first 20 pairs of numbers whose sum is prime:')
while True:
n += 1
suma = 2*n+1
if isPrime(suma):
num += 1
if num < 21:
print('{:2}'.format(n), "+", '{:2}'.format(n+1), "=", '{:2}'.format(suma))
else:
break</syntaxhighlight>
{{out}}
<pre>The first 20 pairs of numbers whose sum is prime:
1 + 2 = 3
2 + 3 = 5
3 + 4 = 7
5 + 6 = 11
6 + 7 = 13
8 + 9 = 17
9 + 10 = 19
11 + 12 = 23
14 + 15 = 29
15 + 16 = 31
18 + 19 = 37
20 + 21 = 41
21 + 22 = 43
23 + 24 = 47
26 + 27 = 53
29 + 30 = 59
30 + 31 = 61
33 + 34 = 67
35 + 36 = 71
36 + 37 = 73</pre>
 
===Functional===
<syntaxhighlight lang="python">'''Prime sums of two consecutive integers'''
 
from itertools import chain, count, islice
 
 
# primeSumsOfTwoConsecutiveIntegers :: [Int]
def primeSumsOfTwoConsecutiveIntegers():
'''Infinite series of prime sums of
two consecutive integers.
'''
def go(a, b):
n = a + b
return [(n, (a, b))] if isPrime(n) else []
 
return chain.from_iterable(
map(go, count(1), count(2))
)
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''First 20 prime sums of two consecutive integers.'''
print(
'\n'.join([
f'{n} = {a} + {b}' for n, (a, b) in islice(
primeSumsOfTwoConsecutiveIntegers(),
20
)
])
)
 
 
# ----------------------- GENERIC ------------------------
 
# isPrime :: Int -> Bool
def isPrime(n):
'''True if n is prime.'''
if n in (2, 3):
return True
if 2 > n or 0 == n % 2:
return False
if 9 > n:
return True
if 0 == n % 3:
return False
 
def p(x):
return 0 == n % x or 0 == n % (2 + x)
 
return not any(map(p, range(5, 1 + int(n ** 0.5), 6)))
 
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
 
 
or as a list comprehension (the walrus operator requires Python 3.8+)
 
<syntaxhighlight lang="python">'''Prime sums of two consecutive integers'''
 
from itertools import count, islice
 
 
# primeSumsOfTwoConsecutiveIntegers :: [Int]
def primeSumsOfTwoConsecutiveIntegers():
'''As a list comprehension.'''
return (
(n, (a, b))
for a in count(1)
if isPrime(n := a + (b := 1 + a))
)
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''First 20 prime sums of two consecutive integers.'''
print(
'\n'.join([
f'{n} = {a} + {b}' for n, (a, b) in islice(
primeSumsOfTwoConsecutiveIntegers(),
20
)
])
)
 
 
# ----------------------- GENERIC ------------------------
 
# isPrime :: Int -> Bool
def isPrime(n):
'''True if n is prime.'''
if n in (2, 3):
return True
if 2 > n or 0 == n % 2:
return False
if 9 > n:
return True
if 0 == n % 3:
return False
 
def p(x):
return 0 == n % x or 0 == n % (2 + x)
 
return not any(map(p, range(5, 1 + int(n ** 0.5), 6)))
 
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
 
{{Out}}
<pre>3 = 1 + 2
5 = 2 + 3
7 = 3 + 4
11 = 5 + 6
13 = 6 + 7
17 = 8 + 9
19 = 9 + 10
23 = 11 + 12
29 = 14 + 15
31 = 15 + 16
37 = 18 + 19
41 = 20 + 21
43 = 21 + 22
47 = 23 + 24
53 = 26 + 27
59 = 29 + 30
61 = 30 + 31
67 = 33 + 34
71 = 35 + 36
73 = 36 + 37</pre>
 
=={{header|Quackery}}==
 
Every odd number is the sum of two adjacent numbers, so this task is "the first twenty odd primes" or "the first twenty primes after 2".
 
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ 1 [] rot
[ over size
over != while
dip
[ over isprime if
[ over join ]
dip [ 2 + ] ]
again ]
drop nip ] is oddprimes ( n --> [ )
 
[ oddprimes
witheach
[ dup 2 /
dup echo
say " + "
1+ echo
say " = "
echo cr ] ] is task ( n --> )
 
20 task</syntaxhighlight>
 
{{out}}
 
<pre>1 + 2 = 3
2 + 3 = 5
3 + 4 = 7
5 + 6 = 11
6 + 7 = 13
8 + 9 = 17
9 + 10 = 19
11 + 12 = 23
14 + 15 = 29
15 + 16 = 31
18 + 19 = 37
20 + 21 = 41
21 + 22 = 43
23 + 24 = 47
26 + 27 = 53
29 + 30 = 59
30 + 31 = 61
33 + 34 = 67
35 + 36 = 71
36 + 37 = 73
</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>my @n-n1-triangular = map { $_, $_ + 1, $_ + ($_ + 1) }, ^Inf;
 
my @wanted = @n-n1-triangular.grep: *.[2].is-prime;
 
printf "%2d + %2d = %2d\n", |.list for @wanted.head(20);</langsyntaxhighlight>
{{out}}
<pre>
Line 259 ⟶ 1,453:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlibcore.ring"
see "working..." + nl
Line 279 ⟶ 1,473:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 304 ⟶ 1,498:
36 + 37 = 73
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
≪ { } 2
'''WHILE''' OVER SIZE 20 < '''REPEAT'''
NEXTPRIME
DUP 2 IQUOT →STR
LASTARG SWAP "+" + SWAP 1 + + "=" + OVER +
ROT SWAP + SWAP
'''END''' DROP
≫ 'TASK' STO
{{out}}
<pre>
1: {"1+2=3" "2+3=5" "3+4=7" "5+6=11" "6+7=13" "8+9=17" "9+10=19" "11+12=23" "14+15=29" "15+16=31" "18+19=37" "20+21=41" "21+22=43" "23+24=47" "26+27=53" "29+30=59" "30+31=61" "33+34=67" "35+36=71" "36+37=73"}
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
primes = Prime.each
primes.next # skip 2
primes.first(20).each{|pr| puts "%3d + %3d = %3d" % [pr/2, pr/2+1, pr]}
</syntaxhighlight>
{{out}}
<pre>
1 + 2 = 3
2 + 3 = 5
3 + 4 = 7
5 + 6 = 11
6 + 7 = 13
8 + 9 = 17
9 + 10 = 19
11 + 12 = 23
14 + 15 = 29
15 + 16 = 31
18 + 19 = 37
20 + 21 = 41
21 + 22 = 43
23 + 24 = 47
26 + 27 = 53
29 + 30 = 59
30 + 31 = 61
33 + 34 = 67
35 + 36 = 71
36 + 37 = 73
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var wanted = (1..Inf -> lazy.map {|n| [n, n+1, n+(n+1)] }\
.grep { .tail.is_prime })
 
wanted.first(20).each_2d {|a,b,c|
printf("%2d + %2d = %2d\n", a,b,c)
}</syntaxhighlight>
 
{{out}}
<pre>
1 + 2 = 3
2 + 3 = 5
3 + 4 = 7
5 + 6 = 11
6 + 7 = 13
8 + 9 = 17
9 + 10 = 19
11 + 12 = 23
14 + 15 = 29
15 + 16 = 31
18 + 19 = 37
20 + 21 = 41
21 + 22 = 43
23 + 24 = 47
26 + 27 = 53
29 + 30 = 59
30 + 31 = 61
33 + 34 = 67
35 + 36 = 71
36 + 37 = 73
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./fmt" for Fmt
 
var limit = (1e7.log * 1e7 * 1.2).floor // should be more than enough
var primes = Int.primeSieve(limit)
System.print("The first 20 pairs of natural numbers whose sum is prime are:")
for (i in 1..20) {
var count = 0
var np = 1primes[i]
var hp = (p/2).floor
while (count < 20) {
if (IntFmt.isPrimeprint(2*n"$2d + $2d = $2d", hp, hp + 1)), {p)
}
Fmt.print("$2d + $2d = $2d", n, n + 1, 2*n + 1)
System.print("\nThe 10 millionth such pair is:")
count = count + 1
var p = primes[1e7]
}
var hp = (p/2).floor
n = n + 1
Fmt.print("$2d + $2d = $2d", hp, hp + 1, p)</syntaxhighlight>
}</lang>
 
{{out}}
Line 346 ⟶ 1,621:
35 + 36 = 71
36 + 37 = 73
 
The 10 millionth such pair is:
89712345 + 89712346 = 179424691
</pre>
 
=={{header|XPL0}}==
{{trans|Ring}}
<syntaxhighlight lang="xpl0">
<lang XPL0>
include xpllib;
int N, Num, Sum;
Line 368 ⟶ 1,646:
];
Text(0, "Done...^M^J");
]</langsyntaxhighlight>
 
{{out}}
2,046

edits