Sum of two adjacent numbers are primes: Difference between revisions
Content added Content deleted
(→{{header|Raku}}: Added Raku solution) |
(Added Wren) |
||
Line 81: | Line 81: | ||
n = 36 sum= 73 |
n = 36 sum= 73 |
||
done... |
done... |
||
</pre> |
|||
=={{header|Wren}}== |
|||
{{libheader|Wren-math}} |
|||
{{libheader|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> |
|||
{{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 |
|||
</pre> |
</pre> |
||
Revision as of 14:05, 21 January 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.
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 + " sum= " + sum else exit ok ok
end
see "done..." + nl </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...
Wren
<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
<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...