Sum of two adjacent numbers are primes

Revision as of 06:52, 22 January 2022 by Wherrera (talk | contribs) (julia example)


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

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

Julia

<lang julia>using Lazy using Primes

s = @>> Lazy.range(2) filter(n -> isprime(2n + 1)) for n in take(20, s)

   println("$n + $(n + 1) = $(n + n + 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

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

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