Strange unique prime triplets: Difference between revisions

m
(Added Java solution)
m (→‎{{header|Wren}}: Minor tidy)
 
(42 intermediate revisions by 23 users not shown)
Line 1:
{{Draft task|Prime numbersNumbers}}
 
Primes &nbsp; '''n, &nbsp; m,''' &nbsp; and &nbsp; '''p''' &nbsp; are &nbsp; ''strange unique primes'' &nbsp; if &nbsp; '''n, &nbsp; m,''' &nbsp; and &nbsp; '''p''' &nbsp; are unique and their sum &nbsp; &nbsp; '''n&nbsp;+&nbsp;m&nbsp;+&nbsp;p''' &nbsp; &nbsp; is also prime. Assume '''n''' < '''m''' < '''p'''.
 
 
Line 8:
:* &nbsp; (stretch goal) &nbsp; Show the <u>count</u> (only) of all the triplets of strange unique primes in which &nbsp; &nbsp;&nbsp;'''n,&nbsp;m,'''&nbsp;and&nbsp;'''p'''&nbsp; &nbsp; are all less than &nbsp; '''1,000'''.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F primes_upto(limit)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
I is_prime[n]
L(i) (n * n .< limit + 1).step(n)
is_prime[i] = 0B
R enumerate(is_prime).filter((i, prime) -> prime).map((i, prime) -> i)
 
F strange_triplets(Int mx = 30)
[(Int, Int, Int)] r
V primes = Array(primes_upto(mx))
V primes3 = Set(primes_upto(3 * mx))
L(n) primes
V i = L.index
L(m) primes[i + 1 ..]
V j = L.index + i + 1
L(p) primes[j + 1 ..]
I n + m + p C primes3
r.append((n, m, p))
R r
 
L(n, m, p) strange_triplets()
print(‘#2: #2+#2+#2 = #.’.format(L.index + 1, n, m, p, n + m + p))
 
V mx = 1'000
print("\nIf n, m, p < #. finds #.".format(mx, strange_triplets(mx).len))</syntaxhighlight>
 
{{out}}
<pre>
1: 3+ 5+11 = 19
2: 3+ 5+23 = 31
3: 3+ 5+29 = 37
4: 3+ 7+13 = 23
5: 3+ 7+19 = 29
6: 3+11+17 = 31
7: 3+11+23 = 37
8: 3+11+29 = 43
9: 3+17+23 = 43
10: 5+ 7+11 = 23
11: 5+ 7+17 = 29
12: 5+ 7+19 = 31
13: 5+ 7+29 = 41
14: 5+11+13 = 29
15: 5+13+19 = 37
16: 5+13+23 = 41
17: 5+13+29 = 47
18: 5+17+19 = 41
19: 5+19+23 = 47
20: 5+19+29 = 53
21: 7+11+13 = 31
22: 7+11+19 = 37
23: 7+11+23 = 41
24: 7+11+29 = 47
25: 7+13+17 = 37
26: 7+13+23 = 43
27: 7+17+19 = 43
28: 7+17+23 = 47
29: 7+17+29 = 53
30: 7+23+29 = 59
31: 11+13+17 = 41
32: 11+13+19 = 43
33: 11+13+23 = 47
34: 11+13+29 = 53
35: 11+17+19 = 47
36: 11+19+23 = 53
37: 11+19+29 = 59
38: 13+17+23 = 53
39: 13+17+29 = 59
40: 13+19+29 = 61
41: 17+19+23 = 59
42: 19+23+29 = 71
 
If n, m, p < 1000 finds 241580
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT"
 
PROC Main()
DEFINE MAXPRIME="29"
DEFINE MAX="99"
BYTE ARRAY primes(MAX+1)
BYTE n,m,p,c
INT count=[0]
 
Put(125) PutE() ;clear the screen
Sieve(primes,MAX+1)
c=0
FOR n=2 TO MAXPRIME-2
DO
IF primes(n) THEN
FOR m=n+1 TO MAXPRIME-1
DO
IF primes(m) THEN
FOR p=m+1 TO MAXPRIME
DO
IF primes(p)=1 AND primes(n+m+p)=1 THEN
PrintF("%I+%I+%I=%I ",n,m,p,n+m+p)
count==+1 c==+1
IF c=3 THEN
c=0 PutE()
FI
FI
OD
FI
OD
FI
OD
PrintF("%EThere are %I prime triplets",count)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Strange_unique_prime_triplets.png Screenshot from Atari 8-bit computer]
<pre>
3+5+11=19 3+5+23=31 3+5+29=37
3+7+13=23 3+7+19=29 3+11+17=31
3+11+23=37 3+11+29=43 3+17+23=43
5+7+11=23 5+7+17=29 5+7+19=31
5+7+29=41 5+11+13=29 5+13+19=37
5+13+23=41 5+13+29=47 5+17+19=41
5+19+23=47 5+19+29=53 7+11+13=31
7+11+19=37 7+11+23=41 7+11+29=47
7+13+17=37 7+13+23=43 7+17+19=43
7+17+23=47 7+17+29=53 7+23+29=59
11+13+17=41 11+13+19=43 11+13+23=47
11+13+29=53 11+17+19=47 11+19+23=53
11+19+29=59 13+17+23=53 13+17+29=59
13+19+29=61 17+19+23=59 19+23+29=71
 
There are 42 prime triplets
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|Algol W}} which is based on {{Trans|Wren}}
{{libheader|ALGOL 68-primes}}
<syntaxhighlight lang="algol68">BEGIN # find some strange unique primes - triplets of primes n, m, p #
# where n + m + p is also prime and n =/= m =/= p #
# we need to find the strange unique prime triplets below 1000 #
# so the maximum triplet sum could be roughly 3000 #
INT max number = 1000;
# sieve the primes to the maximum reuired prime #
PR read "primes.incl.a68" PR
[]BOOL prime = PRIMESIEVE ( max number * 3 );
# we need to find the strange unique prime triplets below 1000 #
INT s count := 0, c30 := 0;
# 2 cannot be one of the primes as the sum would be even otherwise #
FOR n FROM 3 BY 2 TO max number - 5 DO
IF prime[ n ] THEN
FOR m FROM n + 2 BY 2 TO max number- 3 DO
IF prime[ m ] THEN
FOR p FROM m + 2 BY 2 TO max number DO
IF prime[ p ] THEN
IF INT s = n + m + p;
prime[ s ]
THEN
# have 3 unique primes whose sum is prime #
s count +:= 1;
IF p <= 30 AND m <= 30 AND n <= 30 THEN
c30 +:= 1;
print( ( whole( c30, -3 ), ": "
, whole( n, -3 ), " + "
, whole( m, -3 ), " + "
, whole( p, -3 ), " = "
, whole( s, -3 ), newline
)
)
FI
FI
FI
OD # p #
FI
OD # m #
FI
OD # n # ;
print( ( "Found ", whole( c30, -6 ), " strange unique prime triplets up to 30", newline ) );
print( ( "Found ", whole( s count, -6 ), " strange unique prime triplets up to 1000", newline ) )
END</syntaxhighlight>
{{out}}
<pre>
1: 3 + 5 + 11 = 19
2: 3 + 5 + 23 = 31
3: 3 + 5 + 29 = 37
4: 3 + 7 + 13 = 23
5: 3 + 7 + 19 = 29
6: 3 + 11 + 17 = 31
7: 3 + 11 + 23 = 37
8: 3 + 11 + 29 = 43
9: 3 + 17 + 23 = 43
10: 5 + 7 + 11 = 23
11: 5 + 7 + 17 = 29
12: 5 + 7 + 19 = 31
13: 5 + 7 + 29 = 41
14: 5 + 11 + 13 = 29
15: 5 + 13 + 19 = 37
16: 5 + 13 + 23 = 41
17: 5 + 13 + 29 = 47
18: 5 + 17 + 19 = 41
19: 5 + 19 + 23 = 47
20: 5 + 19 + 29 = 53
21: 7 + 11 + 13 = 31
22: 7 + 11 + 19 = 37
23: 7 + 11 + 23 = 41
24: 7 + 11 + 29 = 47
25: 7 + 13 + 17 = 37
26: 7 + 13 + 23 = 43
27: 7 + 17 + 19 = 43
28: 7 + 17 + 23 = 47
29: 7 + 17 + 29 = 53
30: 7 + 23 + 29 = 59
31: 11 + 13 + 17 = 41
32: 11 + 13 + 19 = 43
33: 11 + 13 + 23 = 47
34: 11 + 13 + 29 = 53
35: 11 + 17 + 19 = 47
36: 11 + 19 + 23 = 53
37: 11 + 19 + 29 = 59
38: 13 + 17 + 23 = 53
39: 13 + 17 + 29 = 59
40: 13 + 19 + 29 = 61
41: 17 + 19 + 23 = 59
42: 19 + 23 + 29 = 71
Found 42 strange unique prime triplets up to 30
Found 241580 strange unique prime triplets up to 1000
</pre>
 
=={{header|ALGOL W}}==
Based on {{Trans|Wren}}
<langsyntaxhighlight lang="algolw">begin % find some strange unique primes - triplets of primes n, m, p %
% where n + m + p is also prime and n =/= m =/= p %
% sets p( 1 :: n ) to a sieve of primes up to n %
Line 33 ⟶ 261:
% construct a sieve of primes up to MAX_PRIME * 3 %
Eratosthenes( p, MAX_PRIME * 3 );
% count the nicestrange prime triplets whose members are less than< 1000 and %
% andwhose sum is prime the first 30 %
sCount := c30 := 0;
% 2 cannot be one of the primes as the sum would be even otherwise %
Line 41 ⟶ 269:
for m := n + 2 step 2 until MAX_PRIME - 3 do begin
if p( m ) then begin
for l := m + 2 STEPstep 2 until MAX_PRIME do begin
if p( l ) then begin
integer s;
Line 61 ⟶ 289:
write( i_w := 3, s_w := 0, "Found ", sCount, " strange unique prime triplets up to 1000" );
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 109 ⟶ 337:
Found 241580 strange unique prime triplets up to 1000
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">findTriplets: function [upTo][
results: []
loop select 2..upTo => prime? 'n ->
loop select n..upTo => prime? 'm ->
loop select m..upTo => prime? 'p ->
if all? @[
3 = size unique @[n m p]
prime? n+m+p
]-> 'results ++ @[@[n,m,p]]
return results
]
 
loop.with:'i findTriplets 30 'res ->
print [i+1 "->" join.with:" + " to [:string] res "=" sum res]
 
print ""
print ["If n, m, p < 1000 -> total number of triplets:" size findTriplets 1000]</syntaxhighlight>
 
{{out}}
 
<pre>1 -> 3 + 5 + 11 = 19
2 -> 3 + 5 + 23 = 31
3 -> 3 + 5 + 29 = 37
4 -> 3 + 7 + 13 = 23
5 -> 3 + 7 + 19 = 29
6 -> 3 + 11 + 17 = 31
7 -> 3 + 11 + 23 = 37
8 -> 3 + 11 + 29 = 43
9 -> 3 + 17 + 23 = 43
10 -> 5 + 7 + 11 = 23
11 -> 5 + 7 + 17 = 29
12 -> 5 + 7 + 19 = 31
13 -> 5 + 7 + 29 = 41
14 -> 5 + 11 + 13 = 29
15 -> 5 + 13 + 19 = 37
16 -> 5 + 13 + 23 = 41
17 -> 5 + 13 + 29 = 47
18 -> 5 + 17 + 19 = 41
19 -> 5 + 19 + 23 = 47
20 -> 5 + 19 + 29 = 53
21 -> 7 + 11 + 13 = 31
22 -> 7 + 11 + 19 = 37
23 -> 7 + 11 + 23 = 41
24 -> 7 + 11 + 29 = 47
25 -> 7 + 13 + 17 = 37
26 -> 7 + 13 + 23 = 43
27 -> 7 + 17 + 19 = 43
28 -> 7 + 17 + 23 = 47
29 -> 7 + 17 + 29 = 53
30 -> 7 + 23 + 29 = 59
31 -> 11 + 13 + 17 = 41
32 -> 11 + 13 + 19 = 43
33 -> 11 + 13 + 23 = 47
34 -> 11 + 13 + 29 = 53
35 -> 11 + 17 + 19 = 47
36 -> 11 + 19 + 23 = 53
37 -> 11 + 19 + 29 = 59
38 -> 13 + 17 + 23 = 53
39 -> 13 + 17 + 29 = 59
40 -> 13 + 19 + 29 = 61
41 -> 17 + 19 + 23 = 59
42 -> 19 + 23 + 29 = 71
 
If n, m, p < 1000 -> total number of triplets: 241580</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f STRANGE_UNIQUE_PRIME_TRIPLETS.AWK
# converted from Go
Line 151 ⟶ 446:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 200 ⟶ 495:
Unique prime triples 2-999 which sum to a prime: 241,580
</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <string.h>
 
#define LIMIT 3000
 
void init_sieve(unsigned char sieve[], int limit) {
int i, j;
 
for (i = 0; i < limit; i++) {
sieve[i] = 1;
}
sieve[0] = 0;
sieve[1] = 0;
 
for (i = 2; i < limit; i++) {
if (sieve[i]) {
for (j = i + i; j < limit; j += i) {
sieve[j] = 0;
}
}
}
}
 
void strange_unique_prime_triplets(unsigned char sieve[], int limit, bool verbose) {
int count = 0, sum;
int i, j, k, n, p;
int pi, pj, pk;
 
n = 0;
for (i = 0; i < limit; i++) {
if (sieve[i]) {
n++;
}
}
 
if (verbose) {
printf("Strange unique prime triplets < %d:\n", limit);
}
 
for (i = 0; i + 2 < n; i++) {
pi = 2;
p = i;
while (p > 0) {
pi++;
if (sieve[pi]) {
p--;
}
}
 
for (j = i + 1; j + 1 < n; j++) {
pj = pi;
p = j - i;
while (p > 0) {
pj++;
if (sieve[pj]) {
p--;
}
}
 
for (k = j + 1; k < n; k++) {
pk = pj;
p = k - j;
while (p > 0) {
pk++;
if (sieve[pk]) {
p--;
}
}
 
sum = pi + pj + pk;
if (sum < LIMIT && sieve[sum]) {
count++;
if (verbose) {
printf("%2d + %2d + %2d = %d\n", pi, pj, pk, sum);
}
}
}
}
}
 
printf("Count of strange unique prime triplets < %d is %d.\n\n", limit, count);
}
 
int main() {
unsigned char sieve[LIMIT];
 
init_sieve(sieve, LIMIT);
 
strange_unique_prime_triplets(sieve, 30, true);
strange_unique_prime_triplets(sieve, 1000, false);
 
return 0;
}</syntaxhighlight>
{{out}}
<pre>Strange unique prime triplets < 30:
3 + 5 + 11 = 19
3 + 5 + 23 = 31
3 + 5 + 29 = 37
3 + 7 + 13 = 23
3 + 7 + 19 = 29
3 + 11 + 17 = 31
3 + 11 + 23 = 37
3 + 11 + 29 = 43
3 + 17 + 23 = 43
5 + 7 + 11 = 23
5 + 7 + 17 = 29
5 + 7 + 19 = 31
5 + 7 + 29 = 41
5 + 11 + 13 = 29
5 + 13 + 19 = 37
5 + 13 + 23 = 41
5 + 13 + 29 = 47
5 + 17 + 19 = 41
5 + 19 + 23 = 47
5 + 19 + 29 = 53
7 + 11 + 13 = 31
7 + 11 + 19 = 37
7 + 11 + 23 = 41
7 + 11 + 29 = 47
7 + 13 + 17 = 37
7 + 13 + 23 = 43
7 + 17 + 19 = 43
7 + 17 + 23 = 47
7 + 17 + 29 = 53
7 + 23 + 29 = 59
11 + 13 + 17 = 41
11 + 13 + 19 = 43
11 + 13 + 23 = 47
11 + 13 + 29 = 53
11 + 17 + 19 = 47
11 + 19 + 23 = 53
11 + 19 + 29 = 59
13 + 17 + 23 = 53
13 + 17 + 29 = 59
13 + 19 + 29 = 61
17 + 19 + 23 = 59
19 + 23 + 29 = 71
Count of strange unique prime triplets < 30 is 42.
 
Count of strange unique prime triplets < 1000 is 241580.</pre>
 
=={{header|C#|CSharp}}==
Just for fun, <30 sorted by sum, instead of order generated. One might think one should include the sieve generation time, but it is orders of magnitude smaller than the permute/sum time for these relatively low numbers.
<langsyntaxhighlight lang="csharp">using System; using System.Collections.Generic; using static System.Console; using System.Linq; using DT = System.DateTime;
 
class Program { static void Main(string[] args) { string s;
Line 225 ⟶ 663:
if (!flags[j]) { yield return j;
for (int k = sq; k <= lim; k += j) flags[k] = true; }
for (; j <= lim; j++) if (!flags[j]) yield return j; } }</langsyntaxhighlight>
{{out}}
Timings from tio.run
Line 278 ⟶ 716:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <vector>
Line 338 ⟶ 776:
strange_unique_prime_triplets(1000, false);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 394 ⟶ 832:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Strange_primes;
 
Line 480 ⟶ 918:
writeln('There are ', cs, ' unique prime triples under 1,000 which sum to a prime.');
readln;
end.</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br>
<langsyntaxhighlight lang="fsharp">
// Strange unique prime triplets. Nigel Galloway: March 12th., 2021
let sP n=let N=primes32()|>Seq.takeWhile((>)n)|>Array.ofSeq
Line 491 ⟶ 929:
printfn "%d" (Seq.length(sP 1000))
printfn "%d" (Seq.length(sP 10000))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 497 ⟶ 935:
74588542
</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting io kernel math math.combinatorics math.primes
sequences tools.memory.private ;
 
Line 514 ⟶ 953:
30 strange
1,000 count-strange commas nl
"Found %s strange prime triplets with n, m, p < 1,000.\n" printf</langsyntaxhighlight>
{{out}}
<pre>
Line 562 ⟶ 1,001:
Found 241,580 strange prime triplets with n, m, p < 1,000.
</pre>
 
=={{header|Fermat}}==
<syntaxhighlight lang="fermat">Function IsSUPT(n,m,p) =
if Isprime(n) and Isprime(m) and Isprime(p) and Isprime(n+m+p) then 1 else 0 fi.
 
for n=3 to 19 do
for m=n+2 to 23 do
for p=m+2 to 29 do
if IsSUPT(n,m,p) then !!(n,m,p) fi;
od;
od;
od</syntaxhighlight>
I'll leave the stretch goal for someone else.
 
=={{header|FreeBASIC}}==
Use the function at [[Primality by trial division#FreeBASIC]] as an include; I can't be bothered reproducing it here.
<syntaxhighlight lang="freebasic">#include"isprime.bas"
 
dim as uinteger c = 0
 
for p as uinteger = 3 to 997
if not isprime(p) then continue for
for m as uinteger = p + 1 to 998
if not isprime(m) then continue for
for n as uinteger = m + 1 to 999
if not isprime(n) then continue for
if isprime(p + n + m) then
c = c + 1
if n < 30 then print p;" + ";m;" + ";n;" = "; p + m + n
end if
next n
next m
next p
 
print "There are ";c;" triples below 1000."</syntaxhighlight>
{{out}}<pre>3 + 5 + 11 = 19
3 + 5 + 23 = 31
3 + 5 + 29 = 37
3 + 7 + 13 = 23
3 + 7 + 19 = 29
3 + 11 + 17 = 31
3 + 11 + 23 = 37
3 + 11 + 29 = 43
3 + 17 + 23 = 43
5 + 7 + 11 = 23
5 + 7 + 17 = 29
5 + 7 + 19 = 31
5 + 7 + 29 = 41
5 + 11 + 13 = 29
5 + 13 + 19 = 37
5 + 13 + 23 = 41
5 + 13 + 29 = 47
5 + 17 + 19 = 41
5 + 19 + 23 = 47
5 + 19 + 29 = 53
7 + 11 + 13 = 31
7 + 11 + 19 = 37
7 + 11 + 23 = 41
7 + 11 + 29 = 47
7 + 13 + 17 = 37
7 + 13 + 23 = 43
7 + 17 + 19 = 43
7 + 17 + 23 = 47
7 + 17 + 29 = 53
7 + 23 + 29 = 59
11 + 13 + 17 = 41
11 + 13 + 19 = 43
11 + 13 + 23 = 47
11 + 13 + 29 = 53
11 + 17 + 19 = 47
11 + 19 + 23 = 53
11 + 19 + 29 = 59
13 + 17 + 23 = 53
13 + 17 + 29 = 59
13 + 19 + 29 = 61
17 + 19 + 23 = 59
19 + 23 + 29 = 71
There are 241580 triples below 1000.</pre>
 
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: prime? ( n -- ? ) here + c@ 0= ;
: notprime! ( n -- ) here + 1 swap c! ;
 
Line 634 ⟶ 1,151:
." Count of strange unique prime triplets < 1000: "
1000 count_strange_unique_prime_triplets . cr
bye</langsyntaxhighlight>
 
{{out}}
Line 683 ⟶ 1,200:
Count of strange unique prime triplets < 1000: 241580
</pre>
 
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Strange_unique_prime_triplets}}
 
'''Solution'''
 
Definitions:
 
[[File:Fōrmulæ - Strange unique prime triplets 01.png]]
 
[[File:Fōrmulæ - Strange unique prime triplets 02.png]]
 
'''Test case 1. Find all triplets of strange unique primes in which n, m, and p are all less than 30'''
 
[[File:Fōrmulæ - Strange unique prime triplets 03.png]]
 
[[File:Fōrmulæ - Strange unique prime triplets 04.png]]
 
'''Test case 2. (Stretch goal). Show the count (only) of all the triplets of strange unique primes in which n, m, and p are all less than 1,000'''
 
[[File:Fōrmulæ - Strange unique prime triplets 05.png]]
 
[[File:Fōrmulæ - Strange unique prime triplets 06.png]]
 
[[File:Fōrmulæ - Strange unique prime triplets 07.png]]
 
=={{header|Go}}==
===Basic===
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 762 ⟶ 1,304:
cs := commatize(strangePrimes(999, true))
fmt.Printf("\nThere are %s unique prime triples under 1,000 which sum to a prime.\n", cs)
}</langsyntaxhighlight>
 
{{out}}
Line 815 ⟶ 1,357:
===Faster===
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 899 ⟶ 1,441:
cs := commatize(strangePrimes(999, true))
fmt.Printf("\nThere are %s unique prime triples under 1,000 which sum to a prime.\n", cs)
}</langsyntaxhighlight>
 
{{out}}
Same as 'basic' version.
 
=={{header|J}}==
<syntaxhighlight lang="j">cb=. ;@:({. <@,. @\.)}.
comb3=. ]cb cb
triplets=. (#~ 1 p: +/"1)@comb3@(i.&.(p:inv)"0)</syntaxhighlight>
{{out}}
<pre> triplets 30
3 5 11
3 5 23
3 5 29
3 7 13
3 7 19
3 11 17
3 11 23
3 11 29
3 17 23
5 7 11
5 7 17
5 7 19
5 7 29
5 11 13
5 13 19
5 13 23
5 13 29
5 17 19
5 19 23
5 19 29
7 11 13
7 11 19
7 11 23
7 11 29
7 13 17
7 13 23
7 17 19
7 17 23
7 17 29
7 23 29
11 13 17
11 13 19
11 13 23
11 13 29
11 17 19
11 19 23
11 19 29
13 17 23
13 17 29
13 19 29
17 19 23
19 23 29
 
#@triplets 30 1000
42 241580</pre>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.*;
 
public class StrangeUniquePrimeTriplets {
Line 927 ⟶ 1,521:
int count = 0;
if (verbose)
System.out.printf("Strange unique prime triplets < %d:\n", limit);
for (int i = 0; i + 2 < n; ++i) {
for (int j = i + 1; j + 1 < n; ++j) {
Line 936 ⟶ 1,530:
++count;
if (verbose)
System.out.printf("%2d + %2d + %2d = %2d\n", primes[i], primes[j], primes[k], sum);
}
}
}
}
System.out.printf("\nCount of strange unique prime triplets < %d is %d.\n", limit, count);
}
 
Line 965 ⟶ 1,559:
return sieve;
}
}</syntaxhighlight>
 
private static void printf(String format, Object... args) {
System.out.println(String.format(format, args));
}
}</lang>
 
{{out}}
Line 1,020 ⟶ 1,610:
 
Count of strange unique prime triplets < 1000 is 241580.
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`.
<syntaxhighlight lang="jq">def count(s): reduce s as $x (null; .+1);
 
def task($n):
[2, (range(3;$n;2)|select(is_prime))]
| . as $p
| range(0; length) as $i
| range($i+1; length) as $j
| range($j+1; length) as $k
| [.[$i], .[$j], .[$k]]
| select( add| is_prime) ;
 
task(30),
"\nStretch goal: \(count(task(1000)))"</syntaxhighlight>
{{out}}
<pre>
[3,5,11]
[3,5,23]
[3,5,29]
[3,7,13]
[3,7,19]
[3,11,17]
[3,11,23]
[3,11,29]
[3,17,23]
[5,7,11]
[5,7,17]
[5,7,19]
[5,7,29]
[5,11,13]
[5,13,19]
[5,13,23]
[5,13,29]
[5,17,19]
[5,19,23]
[5,19,29]
[7,11,13]
[7,11,19]
[7,11,23]
[7,11,29]
[7,13,17]
[7,13,23]
[7,17,19]
[7,17,23]
[7,17,29]
[7,23,29]
[11,13,17]
[11,13,19]
[11,13,23]
[11,13,29]
[11,17,19]
[11,19,23]
[11,19,29]
[13,17,23]
[13,17,29]
[13,19,29]
[17,19,23]
[19,23,29]
 
Stretch goal: 241580
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function prime_sum_prime_triplets_to(N, verbose=false)
Line 1,050 ⟶ 1,706:
@time prime_sum_prime_triplets_to(10000)
@time prime_sum_prime_triplets_to(100000)
</langsyntaxhighlight>{{out}}
<pre>
Triplet Sum
Line 1,111 ⟶ 1,767:
224.940756 seconds (35 allocations: 218.156 KiB)
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">p = Prime[Range@PrimePi[30]];
Select[Subsets[p, {3}], Total/*PrimeQ]
 
p = Prime[Range@PrimePi[1000]];
Length[Select[Subsets[p, {3}], Total/*PrimeQ]]</syntaxhighlight>
{{out}}
<pre>{{3,5,11},{3,5,23},{3,5,29},{3,7,13},{3,7,19},{3,11,17},{3,11,23},{3,11,29},{3,17,23},{5,7,11},{5,7,17},{5,7,19},{5,7,29},{5,11,13},{5,13,19},{5,13,23},{5,13,29},{5,17,19},{5,19,23},{5,19,29},{7,11,13},{7,11,19},{7,11,23},{7,11,29},{7,13,17},{7,13,23},{7,17,19},{7,17,23},{7,17,29},{7,23,29},{11,13,17},{11,13,19},{11,13,23},{11,13,29},{11,17,19},{11,19,23},{11,19,29},{13,17,23},{13,17,29},{13,19,29},{17,19,23},{19,23,29}}
241580</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import strformat, strutils, sugar
 
func isPrime(n: Positive): bool =
if n < 2: return false
if n mod 2 == 0: return n == 2
if n mod 3 == 0: return n == 3
var d = 5
while d * d <= n:
if n mod d == 0: return false
inc d, 2
if n mod d == 0: return false
inc d, 4
result = true
 
 
iterator triplets(primes: openArray[int]): (int, int, int) =
## Yield the triplets.
for i in 0..primes.high-2:
let n = primes[i]
for j in (i+1)..primes.high-1:
let m = primes[j]
for k in (j+1)..primes.high:
let p = primes[k]
if (n + m + p).isPrime:
yield (n, m, p)
 
 
const Primes30 = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
echo "List of strange unique prime triplets for n < m < p < 30:"
for (n, m, p) in Primes30.triplets():
echo &"{n:2} + {m:2} + {p:2} = {n+m+p}"
 
echo()
const Primes1000 = collect(newSeq):
for n in 2..999:
if n.isPrime: n
var count = 0
for _ in Primes1000.triplets(): inc count
echo "Count of strange unique prime triplets for n < m < p < 1000: ", ($count).insertSep()</syntaxhighlight>
 
{{out}}
<pre>List of strange unique prime triplets for n < m < p < 30:
3 + 5 + 11 = 19
3 + 5 + 23 = 31
3 + 5 + 29 = 37
3 + 7 + 13 = 23
3 + 7 + 19 = 29
3 + 11 + 17 = 31
3 + 11 + 23 = 37
3 + 11 + 29 = 43
3 + 17 + 23 = 43
5 + 7 + 11 = 23
5 + 7 + 17 = 29
5 + 7 + 19 = 31
5 + 7 + 29 = 41
5 + 11 + 13 = 29
5 + 13 + 19 = 37
5 + 13 + 23 = 41
5 + 13 + 29 = 47
5 + 17 + 19 = 41
5 + 19 + 23 = 47
5 + 19 + 29 = 53
7 + 11 + 13 = 31
7 + 11 + 19 = 37
7 + 11 + 23 = 41
7 + 11 + 29 = 47
7 + 13 + 17 = 37
7 + 13 + 23 = 43
7 + 17 + 19 = 43
7 + 17 + 23 = 47
7 + 17 + 29 = 53
7 + 23 + 29 = 59
11 + 13 + 17 = 41
11 + 13 + 19 = 43
11 + 13 + 23 = 47
11 + 13 + 29 = 53
11 + 17 + 19 = 47
11 + 19 + 23 = 53
11 + 19 + 29 = 59
13 + 17 + 23 = 53
13 + 17 + 29 = 59
13 + 19 + 29 = 61
17 + 19 + 23 = 59
19 + 23 + 29 = 71
 
Count of strange unique prime triplets for n < m < p < 1000: 241_580</pre>
 
=={{header|Pascal}}==
{{works with|Free Pascal}}
<langsyntaxhighlight lang="pascal">program PrimeTriplets;
//Free Pascal Compiler version 3.2.1 [2020/11/03] for x86_64fpc 3.2.1
{$IFDEF FPC}
Line 1,256 ⟶ 2,011:
Check_Limit(10000);
//Check_Limit(MAXZAHL);
END.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,308 ⟶ 2,063:
Limit = 100000 count: 28694800655
real 1m5,378s</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl">use strict;
use warnings;
use List::Util 'sum';
use ntheory <primes is_prime>;
use Algorithm::Combinatorics 'combinations';
 
for my $n (30, 1000) {
printf "Found %d strange unique prime triplets up to $n.\n",
scalar grep { is_prime(sum @$_) } combinations(primes($n), 3);
}</syntaxhighlight>
{{out}}
<pre>Found 42 strange unique prime triplets up to 30.
Found 241580 strange unique prime triplets up to 1000.</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>requires("0.8.4")
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
function create_sieve(integer limit)
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0.8.4"</span><span style="color: #0000FF;">)</span>
sequence sieve = repeat(true,limit)
<span style="color: #008080;">function</span> <span style="color: #000000;">create_sieve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span>
sieve[1] = false
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sieve</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span>
for i=4 to limit by 2 do
<span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
sieve[i] = false
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4</span> <span style="color: #008080;">to</span> <span style="color: #000000;">limit</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
for p=3 to floor(sqrt(limit)) by 2 do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
integer p2 = p*p
<span style="color: #008080;">for</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
if sieve[p2] then
<span style="color: #004080;">integer</span> <span style="color: #000000;">p2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">*</span><span style="color: #000000;">p</span>
for k=p2 to limit by p*2 do
<span style="color: #008080;">if</span> <span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
sieve[k] = false
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">p2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">limit</span> <span style="color: #008080;">by</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return sieve
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">sieve</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
procedure strange_triplets(integer lim, bool bCountOnly=true)
atom t0 = time(), t1 = t0+1
<span style="color: #008080;">procedure</span> <span style="color: #000000;">strange_triplets</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">bCountOnly</span><span style="color: #0000FF;">=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
sequence primes = get_primes_le(lim),
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">(),</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
sieve = create_sieve(lim*3),
<span style="color: #004080;">sequence</span> <span style="color: #000000;">primes</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">),</span>
res = {}
<span style="color: #000000;">sieve</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">create_sieve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">*</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span>
atom count = 0
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
--
<span style="color: #004080;">atom</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
-- It is not worth involving 2, ie primes[1],
<span style="color: #000080;font-style:italic;">--
-- since (2 + any other two primes) is even,
-- alsoIt weis maynot asworth wellinvolving leave2, spaceie for {j,k}primes[1],
-- {k}since in(2 the+ any other two outerprimes) is loops.even,
-- Usingalso awe sievemay onas thewell innerleave testspace isfor over{j,k},
-- ten{k} timesin fasterthe thantwo is_prime(),outer whereasloops.
-- usingUsing a separatesieve tableon ofthe primesinner fortest theis over
-- ten times faster than is_prime(), whereas
-- two outer loops is about twice as fast as
-- using a separate table of primes for the
-- scanning the sieve skipping falsies. Also
-- interestingly,two usingouter nm = n+mloops is about twice as fast as
-- fastscanning asthe nmpsieve =skipping n+m+pfalsies. Also
-- interestingly, using nm = n+m is twice as
--
-- fast as nmp = n+m+p.
for i=2 to length(primes)-2 do
--</span>
integer n = primes[i]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
for j=i+1 to length(primes)-1 do
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
integer m = primes[j],
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
nm = n+m
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span>
for k=j+1 to length(primes) do
<span style="color: #000000;">nm</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">m</span>
integer p = primes[k],
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">j</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
nmp = nm+p
<span style="color: #004080;">integer</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">],</span>
if sieve[nmp] then
<span style="color: #000000;">nmp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nm</span><span style="color: #0000FF;">+</span><span style="color: #000000;">p</span>
count += 1
<span style="color: #008080;">if</span> <span style="color: #000000;">sieve</span><span style="color: #0000FF;">[</span><span style="color: #000000;">nmp</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
if not bCountOnly then
<span style="color: #000000;">count</span> res<span style= append(res,sprintf("%2dcolor: %2d#0000FF;">+%2d+%2d=</span> <span style="color: %d#000000;",>1</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">bCountOnly</span> <span style="color: #008080;">then</span>
{count, n, m, p, nmp}))
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%2d: %2d+%2d+%2d = %d"</span><span style="color: #0000FF;">,</span>
end if
<span style="color: #0000FF;">{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">nmp</span><span style="color: #0000FF;">}))</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if time()>t1 then
<span style="color: #008080;">end</span> <span progress(style="Working...color: (%,d)\r#008080;",{count})>if</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()></span><span style="color: #000000;">t1</span> <span style="color: #008080;">then</span>
t1 = time()+1
<span style="color: #7060A8;">progress</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Working... (%,d)\r"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">})</span>
end if
<span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
progress("")
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
string r = iff(bCountOnly?sprintf(" (%s)",{elapsed(time()-t0)})
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">progress</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
:sprintf(":\n%s",{join(shorten(res,"",3),"\n")}))
<span style="color: #004080;">string</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bCountOnly</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" (%s)"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)})</span>
printf(1,"%,d strange triplets < %,d found%s\n\n",{count,lim,r})
<span style="color: #0000FF;">:</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">":\n%s"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)}))</span>
end procedure
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%,d strange triplets &lt; %,d found%s\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
strange_triplets(30,false)
strange_triplets(1000)
<span style="color: #000000;">strange_triplets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">30</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
strange_triplets(10000)</lang>
<span style="color: #000000;">strange_triplets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">strange_triplets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,392 ⟶ 2,166:
 
74,588,542 strange triplets < 10,000 found (11.4s)
</pre>
 
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">
primes(2, Limit):- 2 =< Limit.
primes(N, Limit):-
between(3, Limit, N),
N /\ 1 > 0, % odd
M is floor(sqrt(N)) - 1, % reverse 2*I+1
Max is M div 2,
forall(between(1, Max, I), N mod (2*I+1) > 0).
 
primeComb(N, List, Comb):-
comb(N, List, Comb),
sumlist(Comb, Sum),
primes(Sum, inf).
 
comb(0, _, []).
comb(N, [X|T], [X|Comb]):-
N > 0,
N1 is N - 1,
comb(N1, T, Comb).
comb(N, [_|T], Comb):-
N > 0,
comb(N, T, Comb).
 
tripletList(Limit, List, Len):-
findall(N, primes(N, Limit), PrimeList),
findall(Comb, primeComb(3, PrimeList, Comb), List),
length(List, Len).
 
showList([]).
showList([[I, J, K]|TList]):-
Sum is I + J + K,
writef('%3r +%3r +%3r =%3r\n', [I, J, K, Sum]),
showList(TList).
 
run([]).
run([Limit|TLimits]):-
tripletList(Limit, List, Len),
( Limit < 50
-> List1 = List
; List1 = []
),
showList(List1),
writef('number of prime Triplets up to%5r is%7r\n', [Limit, Len]),
run(TLimits).
do:- run([30, 1000]).
</syntaxhighlight>
{{out}}
<pre>
?- do.
3 + 5 + 11 = 19
3 + 5 + 23 = 31
3 + 5 + 29 = 37
...
13 + 19 + 29 = 61
17 + 19 + 23 = 59
19 + 23 + 29 = 71
number of prime Triplets up to 30 is 42
number of prime Triplets up to 1000 is 241580
true.
</pre>
 
Line 1,397 ⟶ 2,234:
Using [https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.generate.Sieve.primerange sympy.primerange].
 
<langsyntaxhighlight lang="python">from sympy import primerange
 
def strange_triplets(mx: int = 30) -> None:
Line 1,412 ⟶ 2,249:
 
mx = 1_000
print(f"\nIf n, m, p < {mx:_} finds {sum(1 for _ in strange_triplets(mx)):_}")</langsyntaxhighlight>
 
{{out}}
Line 1,459 ⟶ 2,296:
 
If n, m, p < 1_000 finds 241_580</pre>
 
=={{header|Quackery}}==
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<code>comb</code> is defined at [[Combinations#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ dup size dip
[ witheach
[ over swap peek swap ] ]
nip pack ] is arrange ( [ [ --> [ )
 
[ 0 swap witheach + ] is sum ( [ --> n )
 
[] 30 times
[ i^ isprime if
[ i^ join ] ]
behead drop
3 over size comb
[] unrot
witheach
[ over swap arrange
dup sum
isprime not iff
drop done
nested swap dip join ]
drop
sortwith [ sum dip sum > ]
dup size echo
say " strange unique prime triplets found:"
cr cr
witheach
[ dup witheach
[ echo
i if say "+" ]
say " = " sum echo
cr ]</syntaxhighlight>
 
{{out}}
 
<pre>42 strange unique prime triplets found:
 
3+5+11 = 19
3+7+13 = 23
5+7+11 = 23
3+7+19 = 29
5+7+17 = 29
5+11+13 = 29
3+5+23 = 31
3+11+17 = 31
5+7+19 = 31
7+11+13 = 31
3+5+29 = 37
3+11+23 = 37
5+13+19 = 37
7+11+19 = 37
7+13+17 = 37
5+7+29 = 41
5+13+23 = 41
5+17+19 = 41
7+11+23 = 41
11+13+17 = 41
3+11+29 = 43
3+17+23 = 43
7+13+23 = 43
7+17+19 = 43
11+13+19 = 43
5+13+29 = 47
5+19+23 = 47
7+11+29 = 47
7+17+23 = 47
11+13+23 = 47
11+17+19 = 47
5+19+29 = 53
7+17+29 = 53
11+13+29 = 53
11+19+23 = 53
13+17+23 = 53
7+23+29 = 59
11+19+29 = 59
13+17+29 = 59
17+19+23 = 59
13+19+29 = 61
19+23+29 = 71</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line># 20210312 Raku programming solution
 
for 30, 1000 -> \k {
Line 1,468 ⟶ 2,388:
say "Found ", +$_, " strange unique prime triplets up to ", k
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,476 ⟶ 2,396:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds/lists triplet strange primes (<HI) where the triplets' sum is prime*/
parse arg hi . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 30 /*Not specified? Then use the default.*/
Line 1,516 ⟶ 2,436:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,574 ⟶ 2,494:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,596 ⟶ 2,516:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,645 ⟶ 2,565:
done...
 
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
« { }
'''DO''' SWAP OVER + SWAP NEXTPRIME
'''UNTIL''' DUP 30 > '''END'''
DROP DUP SIZE
→ primes size
« { }
1 size 2 - '''FOR''' m
m 1 + size 1 - '''FOR''' n
n 1 + size '''FOR''' p
primes m GET primes n GET primes p GET
'''IF''' 3 DUPN + + ISPRIME? '''THEN'''
ROT "+" + ROT + "+" + SWAP + +
'''ELSE''' 3 DROPN '''END'''
'''NEXT NEXT NEXT'''
DUP SIZE
» » '<span style="color:blue">TASK</span>' STO
 
{{out}}
<pre>
2: { "3+5+11" "3+5+23" "3+5+29" "3+7+13" "3+7+19" "3+11+17" "3+11+23" "3+11+29" "3+17+23" "5+7+11" "5+7+17" "5+7+19" "5+7+29" "5+11+13" "5+13+19" "5+13+23" "5+13+29" "5+17+19" "5+19+23" "5+19+29" "7+11+13" "7+11+19" "7+11+23" "7+11+29" "7+13+17" "7+13+23" "7+17+19" "7+17+23" "7+17+29" "7+23+29" "11+13+17" "11+13+19" "11+13+23" "11+13+29" "11+17+19" "11+19+23" "11+19+29" "13+17+23" "13+17+29" "13+19+29" "17+19+23" "19+23+29" }
1: 42.
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
Prime.each(30).to_a.combination(3).select{|trio| trio.sum.prime? }.each do |a,b,c|
puts "#{a} + #{b} + #{c} = #{a+b+c}"
end
 
m = 1000
count = Prime.each(m).to_a.combination(3).count{|trio| trio.sum.prime? }
puts "Count of strange unique prime triplets < #{m} is #{count}."
</syntaxhighlight>
{{out}}
<pre>3 + 5 + 11 = 19
3 + 5 + 23 = 31
3 + 5 + 29 = 37
3 + 7 + 13 = 23
3 + 7 + 19 = 29
3 + 11 + 17 = 31
3 + 11 + 23 = 37
3 + 11 + 29 = 43
3 + 17 + 23 = 43
5 + 7 + 11 = 23
5 + 7 + 17 = 29
5 + 7 + 19 = 31
5 + 7 + 29 = 41
5 + 11 + 13 = 29
5 + 13 + 19 = 37
5 + 13 + 23 = 41
5 + 13 + 29 = 47
5 + 17 + 19 = 41
5 + 19 + 23 = 47
5 + 19 + 29 = 53
7 + 11 + 13 = 31
7 + 11 + 19 = 37
7 + 11 + 23 = 41
7 + 11 + 29 = 47
7 + 13 + 17 = 37
7 + 13 + 23 = 43
7 + 17 + 19 = 43
7 + 17 + 23 = 47
7 + 17 + 29 = 53
7 + 23 + 29 = 59
11 + 13 + 17 = 41
11 + 13 + 19 = 43
11 + 13 + 23 = 47
11 + 13 + 29 = 53
11 + 17 + 19 = 47
11 + 19 + 23 = 53
11 + 19 + 29 = 59
13 + 17 + 23 = 53
13 + 17 + 29 = 59
13 + 19 + 29 = 61
17 + 19 + 23 = 59
19 + 23 + 29 = 71
Count of strange unique prime triplets < 1000 is 241580.
</pre>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn prime_sieve(limit: usize) -> Vec<bool> {
let mut sieve = vec![true; limit];
if limit > 0 {
Line 1,718 ⟶ 2,720:
strange_unique_prime_triplets(30, true);
strange_unique_prime_triplets(1000, false);
}</langsyntaxhighlight>
 
{{out}}
Line 1,767 ⟶ 2,769:
Count of strange unique prime triplets < 30 is 42.
Count of strange unique prime triplets < 1000 is 241580.
</pre>
 
=={{header|Scala}}==
Scala3 ready
<syntaxhighlight lang="scala">
val primeStream5 = LazyList.from(5, 6)
.flatMap(n => Seq(n, n + 2))
.filter(p => (5 to math.sqrt(p).floor.toInt by 6).forall(a => p % a > 0 && p % (a + 2) > 0))
 
val primes = LazyList(2, 3) ++ primeStream5
 
def isPrime(n: Int): Boolean =
if (n < 5) (n | 1) == 3
else primes.takeWhile(_ <= math.sqrt(n)).forall(n % _ > 0)
 
def triplets(limit: Int): Iterator[Seq[Int]] =
primes.takeWhile(_ <= limit)
.combinations{3}
.filter(primeTriplet => isPrime(primeTriplet.sum))
 
@main def main: Unit = {
for (list <- triplets(30)) {
val Seq(k, l, m) = list
println(f"$k%2d + $l%2d + $m%2d = ${list.sum}%2d")
}
 
for (limit <- Seq(30, 1000)) {
val start = System.currentTimeMillis
val num = triplets(limit).length
val duration = System.currentTimeMillis - start
println(f"number of prime triplets up to $limit%4d is $num%6d [time(ms): $duration%4d]")
}
}
</syntaxhighlight>
{{out}}
<pre>
3 + 5 + 11 = 19
3 + 5 + 23 = 31
3 + 5 + 29 = 37
3 + 7 + 13 = 23
3 + 7 + 19 = 29
3 + 11 + 17 = 31
3 + 11 + 23 = 37
3 + 11 + 29 = 43
3 + 17 + 23 = 43
5 + 7 + 11 = 23
5 + 7 + 17 = 29
5 + 7 + 19 = 31
5 + 7 + 29 = 41
5 + 11 + 13 = 29
5 + 13 + 19 = 37
5 + 13 + 23 = 41
5 + 13 + 29 = 47
5 + 17 + 19 = 41
5 + 19 + 23 = 47
5 + 19 + 29 = 53
7 + 11 + 13 = 31
7 + 11 + 19 = 37
7 + 11 + 23 = 41
7 + 11 + 29 = 47
7 + 13 + 17 = 37
7 + 13 + 23 = 43
7 + 17 + 19 = 43
7 + 17 + 23 = 47
7 + 17 + 29 = 53
7 + 23 + 29 = 59
11 + 13 + 17 = 41
11 + 13 + 19 = 43
11 + 13 + 23 = 47
11 + 13 + 29 = 53
11 + 17 + 19 = 47
11 + 19 + 23 = 53
11 + 19 + 29 = 59
13 + 17 + 23 = 53
13 + 17 + 29 = 59
13 + 19 + 29 = 61
17 + 19 + 23 = 59
19 + 23 + 29 = 71
number of prime triplets up to 30 is 42 [time(ms): 2]
number of prime triplets up to 1000 is 241580 [time(ms): 1271]
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">for n in (30, 1000) {
var triplets = []
combinations(n.primes, 3, {|*a|
triplets << a if a.sum.is_prime
})
if (n == 30) {
say "Unique prime triplets (p,q,r) <= #{n} such that p+q+r is prime:"
triplets.slices(6).each{.join(' ').say}
}
printf("Found %d strange unique prime triplets up to %s.\n", triplets.len, n)
}</syntaxhighlight>
{{out}}
<pre>
Unique prime triplets (p,q,r) <= 30 such that p+q+r is prime:
[3, 5, 11] [3, 5, 23] [3, 5, 29] [3, 7, 13] [3, 7, 19] [3, 11, 17]
[3, 11, 23] [3, 11, 29] [3, 17, 23] [5, 7, 11] [5, 7, 17] [5, 7, 19]
[5, 7, 29] [5, 11, 13] [5, 13, 19] [5, 13, 23] [5, 13, 29] [5, 17, 19]
[5, 19, 23] [5, 19, 29] [7, 11, 13] [7, 11, 19] [7, 11, 23] [7, 11, 29]
[7, 13, 17] [7, 13, 23] [7, 17, 19] [7, 17, 23] [7, 17, 29] [7, 23, 29]
[11, 13, 17] [11, 13, 19] [11, 13, 23] [11, 13, 29] [11, 17, 19] [11, 19, 23]
[11, 19, 29] [13, 17, 23] [13, 17, 29] [13, 19, 29] [17, 19, 23] [19, 23, 29]
Found 42 strange unique prime triplets up to 30.
Found 241580 strange unique prime triplets up to 1000.
</pre>
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
func primeSieve(limit: Int) -> [Bool] {
Line 1,838 ⟶ 2,946:
 
strangeUniquePrimeTriplets(limit: 30, verbose: true)
strangeUniquePrimeTriplets(limit: 1000, verbose: false)</langsyntaxhighlight>
 
{{out}}
Line 1,890 ⟶ 2,998:
Count of strange unique prime triplets < 1000 is 241580.
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Imports DT = System.DateTime
 
Module Module1
 
Iterator Function Primes(lim As Integer) As IEnumerable(Of Integer)
Dim flags(lim) As Boolean
 
Dim j = 2
 
Dim d = 3
Dim sq = 4
While sq <= lim
If Not flags(j) Then
Yield j
For k = sq To lim Step j
flags(k) = True
Next
End If
 
j += 1
d += 2
sq += d
End While
 
While j <= lim
If Not flags(j) Then
Yield j
End If
j += 1
End While
End Function
 
Sub Main()
For Each lmt In {90, 300, 3000, 30000, 111000}
Dim pr = Primes(lmt).Skip(1).ToList()
Dim st = DT.Now
Dim f = 0
Dim r As New List(Of String)
Dim i = -1
Dim m = lmt \ 3
Dim h = m
While i < 0
i = pr.IndexOf(h)
h -= 1
End While
Dim j = i - 1
Dim k = j - 1
For a = 0 To k
Dim pra = pr(a)
For b = a + 1 To j
Dim prab = pra + pr(b)
For c = b + 1 To i
Dim d = prab + pr(c)
If Not pr.Contains(d) Then
Continue For
End If
f += 1
If lmt < 100 Then
r.Add(String.Format("{3,5} = {0,2} + {1,2} + {2,2}", pra, pr(b), pr(c), d))
End If
Next
Next
Next
Dim s = "s.u.p.t.s under "
r.Sort()
If r.Count > 0 Then
Console.WriteLine("{0}{1}:" + vbNewLine + "{2}", s, m, String.Join(vbNewLine, r))
End If
If lmt > 100 Then
Console.WriteLine("Count of {0}{1,6:n0}: {2,13:n0} {3} sec", s, m, f, (DT.Now - st).ToString().Substring(6))
End If
Next
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre>Same as C#</pre>
 
=={{header|Wren}}==
===Basic===
{{libheader|Wren-math}}
{{libheader|Wren-traititerate}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./traititerate" for Stepped
import "./fmt" for Fmt
 
var strangePrimes = Fn.new { |n, countOnly|
Line 1,923 ⟶ 3,111:
strangePrimes.call(29, false)
var c = strangePrimes.call(999, true)
Fmt.print("\nThere are $,d unique prime triples under 1,000 which sum to a prime.", c)</langsyntaxhighlight>
 
{{out}}
Line 1,976 ⟶ 3,164:
===Faster===
The following version uses a prime sieve and is about 17 times faster than the 'basic' version.
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./fmt" for Fmt
 
var max = 1000
Line 2,006 ⟶ 3,194:
strangePrimes.call(29, false)
var c = strangePrimes.call(999, true)
Fmt.print("\nThere are $,d unique prime triples under 1,000 which sum to a prime.", c)</langsyntaxhighlight>
 
{{out}}
Same as 'basic' version.
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
if (N&1) = 0 then \even >2\ return false;
for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false;
I:= I+1;
];
return true;
]; \IsPrime
 
int Primes, Cnt, P, M, N, S;
[Primes:= [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];
Format(2, 0);
Cnt:= 0;
for P:= 2 to 9 do
for M:= 1 to P-1 do
for N:= 0 to M-1 do
[S:= Primes(N) + Primes(M) + Primes(P);
if IsPrime(S) then
[Cnt:= Cnt+1;
RlOut(0, float(Cnt));
Text(0, ": ");
RlOut(0, float(Primes(N)));
Text(0, " + ");
RlOut(0, float(Primes(M)));
Text(0, " + ");
RlOut(0, float(Primes(P)));
Text(0, " = ");
RlOut(0, float(S));
CrLf(0);
];
];
]</syntaxhighlight>
 
{{out}}
<pre>
1: 3 + 5 + 11 = 19
2: 5 + 7 + 11 = 23
3: 3 + 7 + 13 = 23
4: 5 + 11 + 13 = 29
5: 7 + 11 + 13 = 31
6: 5 + 7 + 17 = 29
7: 3 + 11 + 17 = 31
8: 7 + 13 + 17 = 37
9: 11 + 13 + 17 = 41
10: 3 + 7 + 19 = 29
11: 5 + 7 + 19 = 31
12: 7 + 11 + 19 = 37
13: 5 + 13 + 19 = 37
14: 11 + 13 + 19 = 43
15: 5 + 17 + 19 = 41
16: 7 + 17 + 19 = 43
17: 11 + 17 + 19 = 47
18: 3 + 5 + 23 = 31
19: 3 + 11 + 23 = 37
20: 7 + 11 + 23 = 41
21: 5 + 13 + 23 = 41
22: 7 + 13 + 23 = 43
23: 11 + 13 + 23 = 47
24: 3 + 17 + 23 = 43
25: 7 + 17 + 23 = 47
26: 13 + 17 + 23 = 53
27: 5 + 19 + 23 = 47
28: 11 + 19 + 23 = 53
29: 17 + 19 + 23 = 59
30: 3 + 5 + 29 = 37
31: 5 + 7 + 29 = 41
32: 3 + 11 + 29 = 43
33: 7 + 11 + 29 = 47
34: 5 + 13 + 29 = 47
35: 11 + 13 + 29 = 53
36: 7 + 17 + 29 = 53
37: 13 + 17 + 29 = 59
38: 5 + 19 + 29 = 53
39: 11 + 19 + 29 = 59
40: 13 + 19 + 29 = 61
41: 7 + 23 + 29 = 59
42: 19 + 23 + 29 = 71
</pre>
9,476

edits