Sum of two adjacent numbers are primes: Difference between revisions

From Rosetta Code
Content added Content deleted
m (syntax highlighting fixup automation)
Line 10: Line 10:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find the first 20 primes which are n + ( n + 1 ) for some n #
<syntaxhighlight 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 #
PR read "primes.incl.a68" PR # include prime utilities #
[]BOOL prime = PRIMESIEVE 200; # should be enough primes #
[]BOOL prime = PRIMESIEVE 200; # should be enough primes #
Line 22: Line 22:
FI
FI
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 47: Line 47:
</pre>
</pre>
=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SUM_OF_TWO_ADJACENT_NUMBERS_ARE_PRIMES.AWK
# syntax: GAWK -f SUM_OF_TWO_ADJACENT_NUMBERS_ARE_PRIMES.AWK
BEGIN {
BEGIN {
Line 75: Line 75:
return(1)
return(1)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 105: Line 105:
==={{header|BASIC256}}===
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang BASIC256>function isPrime(v)
<syntaxhighlight lang="basic256">function isPrime(v)
if v < 2 then return False
if v < 2 then return False
if v mod 2 = 0 then return v = 2
if v mod 2 = 0 then return v = 2
Line 132: Line 132:
end if
end if
end while
end while
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 139: Line 139:


==={{header|FreeBASIC}}===
==={{header|FreeBASIC}}===
<lang freebasic>Function isPrime(Byval ValorEval As Integer) As Boolean
<syntaxhighlight lang="freebasic">Function isPrime(Byval ValorEval As Integer) As Boolean
If ValorEval <= 1 Then Return False
If ValorEval <= 1 Then Return False
For i As Integer = 2 To Int(Sqr(ValorEval))
For i As Integer = 2 To Int(Sqr(ValorEval))
Line 161: Line 161:
End If
End If
Loop
Loop
Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>The first 20 pairs of numbers whose sum is prime:
<pre>The first 20 pairs of numbers whose sum is prime:
Line 186: Line 186:


==={{header|PureBasic}}===
==={{header|PureBasic}}===
<lang PureBasic>Procedure isPrime(v.i)
<syntaxhighlight lang="purebasic">Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
ElseIf v < 4 : ProcedureReturn #True
Line 222: Line 222:
ForEver
ForEver
CloseConsole()
CloseConsole()
EndIf</lang>
EndIf</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 232: Line 232:
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang qbasic>FUNCTION isPrime (ValorEval)
<syntaxhighlight lang="qbasic">FUNCTION isPrime (ValorEval)
IF ValorEval <= 1 THEN isPrime = 0
IF ValorEval <= 1 THEN isPrime = 0
FOR i = 2 TO INT(SQR(ValorEval))
FOR i = 2 TO INT(SQR(ValorEval))
Line 255: Line 255:
END IF
END IF
LOOP
LOOP
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 262: Line 262:


==={{header|True BASIC}}===
==={{header|True BASIC}}===
<lang qbasic>FUNCTION isPrime (ValorEval)
<syntaxhighlight lang="qbasic">FUNCTION isPrime (ValorEval)
IF ValorEval <= 1 THEN LET isPrime = 0
IF ValorEval <= 1 THEN LET isPrime = 0
FOR i = 2 TO INT(SQR(ValorEval))
FOR i = 2 TO INT(SQR(ValorEval))
Line 289: Line 289:
END IF
END IF
LOOP
LOOP
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 296: Line 296:


==={{header|Yabasic}}===
==={{header|Yabasic}}===
<lang yabasic>sub isPrime(v)
<syntaxhighlight lang="yabasic">sub isPrime(v)
if v < 2 then return False : fi
if v < 2 then return False : fi
if mod(v, 2) = 0 then return v = 2 : fi
if mod(v, 2) = 0 then return v = 2 : fi
Line 322: Line 322:
end if
end if
loop
loop
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 332: Line 332:
=={{header|C}}==
=={{header|C}}==
{{trans|Go}}
{{trans|Go}}
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <math.h>
#include <math.h>
Line 385: Line 385:
free(primes);
free(primes);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 417: Line 417:
=={{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">
// 2n+1 is prime. Nigel Galloway: Januuary 22nd., 2022
// 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))
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}}
{{out}}
<pre>
<pre>
Line 447: Line 447:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: arrays formatting kernel lists lists.lazy math
<syntaxhighlight lang="factor">USING: arrays formatting kernel lists lists.lazy math
math.primes.lists sequences ;
math.primes.lists sequences ;


20 lprimes cdr [ 2/ dup 1 + 2array ] lmap-lazy ltake
20 lprimes cdr [ 2/ dup 1 + 2array ] lmap-lazy ltake
[ dup sum suffix "%d + %d = %d\n" vprintf ] leach</lang>
[ dup sum suffix "%d + %d = %d\n" vprintf ] leach</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 478: Line 478:
=={{header|Go}}==
=={{header|Go}}==
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 499: Line 499:
hp := p / 2
hp := p / 2
fmt.Printf("%2d + %2d = %2d\n", hp, hp+1, p)
fmt.Printf("%2d + %2d = %2d\n", hp, hp+1, p)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 533: Line 533:
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:
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:


<lang J> (+/,&":'=',{.,&":'+',&":{:)"#. 0 1+/~<.-: p:1+i.20
<syntaxhighlight lang="j"> (+/,&":'=',{.,&":'+',&":{:)"#. 0 1+/~<.-: p:1+i.20
3=1+2
3=1+2
5=2+3
5=2+3
Line 553: Line 553:
67=33+34
67=33+34
71=35+36
71=35+36
73=36+37</lang>
73=36+37</syntaxhighlight>


=={{header|jq}}==
=={{header|jq}}==
Line 562: Line 562:
and so a naive approach to computing the first 20 numbers satisfying
and so a naive approach to computing the first 20 numbers satisfying
the condition is appropriate.
the condition is appropriate.
<lang jq>def is_prime:
<syntaxhighlight lang="jq">def is_prime:
. as $n
. as $n
| if ($n < 2) then false
| if ($n < 2) then false
Line 586: Line 586:
| "\($i) + \($i + 1) = \($q)" );
| "\($i) + \($i + 1) = \($q)" );


naive</lang>
naive</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 612: Line 612:


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


Line 630: Line 630:
println("Ten millionth: $i + $(i + 1) = $(i + i + 1)")
println("Ten millionth: $i + $(i + 1) = $(i + i + 1)")
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
1 + 2 = 3
1 + 2 = 3
Line 656: Line 656:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>Column[Row[{Floor[#/2], " + ", Ceiling[#/2], " = ", #}] & /@ Prime[1 + Range[20]]]
<syntaxhighlight lang="mathematica">Column[Row[{Floor[#/2], " + ", Ceiling[#/2], " = ", #}] & /@ Prime[1 + Range[20]]]
Row[{Floor[#/2], " + ", Ceiling[#/2], " = ", #}] &[Prime[10^7 + 1]]</lang>
Row[{Floor[#/2], " + ", Ceiling[#/2], " = ", #}] &[Prime[10^7 + 1]]</syntaxhighlight>
{{out}}
{{out}}
<pre>1 + 2 = 3
<pre>1 + 2 = 3
Line 683: Line 683:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use ntheory 'is_prime';
use ntheory 'is_prime';


my($n,$c);
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 }</lang>
while () { is_prime(1 + 2*++$n) and printf "%2d + %2d = %2d\n", $n, $n+1, 1+2*$n and ++$c == 20 and last }</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 + 2 = 3
<pre> 1 + 2 = 3
Line 713: Line 713:
=={{header|Phix}}==
=={{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.
''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.
<!--<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;">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>
<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 720: Line 720:
<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: #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>
<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}}
{{out}}
<pre>
<pre>
Line 748: Line 748:


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


def isPrime(n):
def isPrime(n):
Line 769: Line 769:
print('{:2}'.format(n), "+", '{:2}'.format(n+1), "=", '{:2}'.format(suma))
print('{:2}'.format(n), "+", '{:2}'.format(n+1), "=", '{:2}'.format(suma))
else:
else:
break</lang>
break</syntaxhighlight>
{{out}}
{{out}}
<pre>The first 20 pairs of numbers whose sum is prime:
<pre>The first 20 pairs of numbers whose sum is prime:
Line 795: Line 795:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>my @n-n1-triangular = map { $_, $_ + 1, $_ + ($_ + 1) }, ^Inf;
<syntaxhighlight lang="raku" line>my @n-n1-triangular = map { $_, $_ + 1, $_ + ($_ + 1) }, ^Inf;


my @wanted = @n-n1-triangular.grep: *.[2].is-prime;
my @wanted = @n-n1-triangular.grep: *.[2].is-prime;


printf "%2d + %2d = %2d\n", |.list for @wanted.head(20);</lang>
printf "%2d + %2d = %2d\n", |.list for @wanted.head(20);</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 825: Line 825:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlibcore.ring"
load "stdlibcore.ring"
see "working..." + nl
see "working..." + nl
Line 845: Line 845:


see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 873: Line 873:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>var wanted = (1..Inf -> lazy.map {|n| [n, n+1, n+(n+1)] }\
<syntaxhighlight lang="ruby">var wanted = (1..Inf -> lazy.map {|n| [n, n+1, n+(n+1)] }\
.grep { .tail.is_prime })
.grep { .tail.is_prime })


wanted.first(20).each_2d {|a,b,c|
wanted.first(20).each_2d {|a,b,c|
printf("%2d + %2d = %2d\n", a,b,c)
printf("%2d + %2d = %2d\n", a,b,c)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 908: Line 908:
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "./math" for Int
<syntaxhighlight lang="ecmascript">import "./math" for Int
import "./fmt" for Fmt
import "./fmt" for Fmt


Line 922: Line 922:
var p = primes[1e7]
var p = primes[1e7]
var hp = (p/2).floor
var hp = (p/2).floor
Fmt.print("$2d + $2d = $2d", hp, hp + 1, p)</lang>
Fmt.print("$2d + $2d = $2d", hp, hp + 1, p)</syntaxhighlight>


{{out}}
{{out}}
Line 954: Line 954:
=={{header|XPL0}}==
=={{header|XPL0}}==
{{trans|Ring}}
{{trans|Ring}}
<syntaxhighlight lang="xpl0">
<lang XPL0>
include xpllib;
include xpllib;
int N, Num, Sum;
int N, Num, Sum;
Line 972: Line 972:
];
];
Text(0, "Done...^M^J");
Text(0, "Done...^M^J");
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}

Revision as of 17:51, 28 August 2022

Sum of two adjacent numbers are 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


Show on this page the first 20 numbers and sum of two adjacent numbers which sum is prime.

Extra credit

Show the ten millionth such prime sum.

ALGOL 68

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 n WHILE p count < 20 DO
        INT n1 = n + 1;
        INT p  = n + n1;
        IF prime[ p ] THEN
            p count +:= 1;
            print( ( whole( n, -2 ), " + ", whole( n1, -2 ), " = ", whole( p, -3 ), newline ) )
        FI
    OD
END
Output:
 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

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)
}
Output:
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


BASIC

BASIC256

Translation of: FreeBASIC
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
Output:
Igual que la entrada de FreeBASIC.

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
Output:
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

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
Output:
Igual que la entrada de FreeBASIC.

QBasic

Works with: QBasic version 1.1
Works with: QuickBasic version 4.5
Translation of: FreeBASIC
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
Output:
Igual que la entrada de FreeBASIC.

True BASIC

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
Output:
Igual que la entrada de FreeBASIC.

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
Output:
Igual que la entrada de FreeBASIC.


C

Translation of: Go
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define TRUE 1
#define FALSE 0

typedef int bool;

void primeSieve(int *c, int limit, bool processEven, bool primesOnly) {
    int i, ix, p, p2;
    limit++;
    c[0] = TRUE;
    c[1] = TRUE;
    if (processEven) {
        for (i = 4; i < limit; i +=2) c[i] = TRUE;
    }
    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;
        }
    }
}

int main() {
    int i, p, hp, n = 10000000;
    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");
    for (i = 1; i <= 20; ++i) {
        p = primes[i];
        hp = p / 2;
        printf("%2d + %2d = %2d\n", hp, hp+1, p);
    }
    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;
}
Output:
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

F#

This task uses Extensible Prime Generator (F#)

// 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))
Output:
 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

Factor

Works with: Factor version 0.99 2021-06-02
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
Output:
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

Go

Library: Go-rcu
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)
}
Output:
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

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:

   (+/,&":'=',{.,&":'+',&":{:)"#. 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

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.

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
Output:
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

Julia

using Lazy
using Primes

seq = @>> Lazy.range() filter(n -> isprime(2n + 1))
for n in take(20, seq)
    println("$n + $(n + 1) = $(n + n + 1)")
end

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
Output:
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
Ten millionth: 89712345 + 89712346 = 179424691

Mathematica/Wolfram Language

Column[Row[{Floor[#/2], " + ", Ceiling[#/2], " = ", #}] & /@ Prime[1 + Range[20]]]
Row[{Floor[#/2], " + ", Ceiling[#/2], " = ", #}] &[Prime[10^7 + 1]]
Output:
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

Perl

Library: ntheory
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 }
Output:
 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

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.

with javascript_semantics
procedure doh(integer p)
    printf(1,"%d + %d = %d\n",{floor(p/2),ceil(p/2),p})
end procedure
papply(get_primes(-21)[2..$],doh)
doh(get_prime(1e7+1))
Output:
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


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
Output:
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


Raku

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);
Output:
 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

Ring

load "stdlibcore.ring"
see "working..." + nl
n = 0
num = 0

while true
     n++
     sum = 2*n+1
     if isprime(sum)
        num++
        if num < 21
          ? "" + n + " + " + (n+1) + " = " + sum
        else
          exit
        ok
      ok
end

see "done..." + nl
Output:
working...
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
done...

Sidef

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)
}
Output:
 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

Wren

Translation of: Go
Library: Wren-math
Library: Wren-fmt
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 p = primes[i]
    var hp = (p/2).floor
    Fmt.print("$2d + $2d = $2d", hp, hp + 1, p)
}
System.print("\nThe 10 millionth such pair is:")
var p = primes[1e7]
var hp = (p/2).floor
Fmt.print("$2d + $2d = $2d", hp, hp + 1, p)
Output:
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

XPL0

Translation of: Ring
include xpllib;
int N, Num, Sum;
[Text(0, "Working...^M^J");
N:= 0;
Num:= 0;
loop
    [N:= N+1;
    Sum:= 2*N + 1;
    if IsPrime(Sum) then
        [Num:= Num+1;
        if Num < 21 then
          [Text(0,"N = "); IntOut(0,N); Text(0,"  Sum = "); IntOut(0,Sum); CrLf(0)]
        else
          quit
        ]
    ];
Text(0, "Done...^M^J");
]
Output:
Working...
N = 1  Sum = 3
N = 2  Sum = 5
N = 3  Sum = 7
N = 5  Sum = 11
N = 6  Sum = 13
N = 8  Sum = 17
N = 9  Sum = 19
N = 11  Sum = 23
N = 14  Sum = 29
N = 15  Sum = 31
N = 18  Sum = 37
N = 20  Sum = 41
N = 21  Sum = 43
N = 23  Sum = 47
N = 26  Sum = 53
N = 29  Sum = 59
N = 30  Sum = 61
N = 33  Sum = 67
N = 35  Sum = 71
N = 36  Sum = 73
Done...