Sum of two adjacent numbers are primes

Revision as of 14:32, 25 January 2022 by Chunes (talk | contribs) (Add Factor)


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

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
Extra credit

Show the ten millionth such prime sum.

ALGOL 68

<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 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</lang>

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

C

Translation of: Wren

<lang c>#include <stdio.h>

  1. define TRUE 1
  2. define FALSE 0

int isPrime(int n) {

   if (n < 2) return FALSE;
   if (!(n%2)) return n == 2;
   if (!(n%3)) return n == 3;
   int d = 5;
   while (d*d <= n) {
       if (!(n%d)) return FALSE;
       d += 2;
       if (!(n%d)) return FALSE;
       d += 4;
   }
   return TRUE;

}

int main() {

   int count = 0, n = 1;
   printf("The first 20 pairs of natural numbers whose sum is prime are:\n");
   while (count < 20) {
       if (isPrime(2*n + 1)) {
           printf("%2d + %2d = %2d\n", n, n + 1, 2*n + 1);
           count++;
       }
       n++;
   }
   return 0;

}</lang>

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

F#

This task uses Extensible Prime Generator (F#) <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)) </lang>

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

<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</lang>

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

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:

<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</lang>

Julia

<lang 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

</lang>

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

Perl

Library: ntheory

<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 }</lang>

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

Raku

<lang perl6>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);</lang>

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

<lang 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 </lang>

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...

Wren

Library: Wren-math
Library: Wren-fmt

<lang ecmascript>import "./math" for Int import "./fmt" for Fmt

System.print("The first 20 pairs of natural numbers whose sum is prime are:") var count = 0 var n = 1 while (count < 20) {

   if (Int.isPrime(2*n + 1)) {
       Fmt.print("$2d + $2d = $2d", n, n + 1, 2*n + 1)
       count = count + 1
   }
   n = n + 1

}</lang>

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

XPL0

Translation of: Ring

<lang XPL0> 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"); ]</lang>

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...