Strange unique prime triplets: Difference between revisions

m
(→‎{{header|ALGOL 68}}: Use ALGOL 68-primes)
m (→‎{{header|Wren}}: Minor tidy)
 
(24 intermediate revisions by 14 users not shown)
Line 12:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F primes_upto(limit)
V is_prime = [0B] * 2 [+] [1B] * (limit - 1)
L(n) 0 .< Int(limit ^ 0.5 + 1.5)
Line 37:
 
V mx = 1'000
print("\nIf n, m, p < #. finds #.".format(mx, strange_triplets(mx).len))</langsyntaxhighlight>
 
{{out}}
Line 85:
 
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>
 
Line 90 ⟶ 147:
{{Trans|Algol W}} which is based on {{Trans|Wren}}
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight 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 #
Line 131 ⟶ 188:
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</langsyntaxhighlight>
{{out}}
<pre>
Line 182 ⟶ 239:
=={{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 232 ⟶ 289:
write( i_w := 3, s_w := 0, "Found ", sCount, " strange unique prime triplets up to 1000" );
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 280 ⟶ 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 323 ⟶ 446:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 374 ⟶ 497:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <string.h>
Line 467 ⟶ 590:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Strange unique prime triplets < 30:
Line 518 ⟶ 641:
=={{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 540 ⟶ 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 593 ⟶ 716:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <vector>
Line 653 ⟶ 776:
strange_unique_prime_triplets(1000, false);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 709 ⟶ 832:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Strange_primes;
 
Line 795 ⟶ 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 806 ⟶ 929:
printfn "%d" (Seq.length(sP 1000))
printfn "%d" (Seq.length(sP 10000))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 812 ⟶ 935:
74588542
</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting io kernel math math.combinatorics math.primes
sequences tools.memory.private ;
 
Line 829 ⟶ 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 879 ⟶ 1,003:
 
=={{header|Fermat}}==
<langsyntaxhighlight 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.
 
Line 888 ⟶ 1,012:
od;
od;
od</langsyntaxhighlight>
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.
<langsyntaxhighlight lang="freebasic">#include"isprime.bas"
 
dim as uinteger c = 0
Line 911 ⟶ 1,035:
next p
 
print "There are ";c;" triples below 1000."</langsyntaxhighlight>
{{out}}<pre>3 + 5 + 11 = 19
3 + 5 + 23 = 31
Line 958 ⟶ 1,082:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: prime? ( n -- ? ) here + c@ 0= ;
: notprime! ( n -- ) here + 1 swap c! ;
 
Line 1,027 ⟶ 1,151:
." Count of strange unique prime triplets < 1000: "
1000 count_strange_unique_prime_triplets . cr
bye</langsyntaxhighlight>
 
{{out}}
Line 1,078 ⟶ 1,202:
 
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Strange_unique_prime_triplets}}
 
'''Solution'''
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
Definitions:
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Strange unique prime triplets 01.png]]
In '''[https://formulae.org/?example=Strange_unique_prime_triplets this]''' page you can see the program(s) related to this task and their results.
 
[[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 1,163 ⟶ 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 1,216 ⟶ 1,357:
===Faster===
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,300 ⟶ 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 1,366 ⟶ 1,559:
return sieve;
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,417 ⟶ 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,447 ⟶ 1,706:
@time prime_sum_prime_triplets_to(10000)
@time prime_sum_prime_triplets_to(100000)
</langsyntaxhighlight>{{out}}
<pre>
Triplet Sum
Line 1,508 ⟶ 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}}==
<langsyntaxhighlight Nimlang="nim">import strformat, strutils, sugar
 
func isPrime(n: Positive): bool =
Line 1,548 ⟶ 1,817:
var count = 0
for _ in Primes1000.triplets(): inc count
echo "Count of strange unique prime triplets for n < m < p < 1000: ", ($count).insertSep()</langsyntaxhighlight>
 
{{out}}
Line 1,599 ⟶ 1,868:
=={{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,742 ⟶ 2,011:
Check_Limit(10000);
//Check_Limit(MAXZAHL);
END.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,797 ⟶ 2,066:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use List::Util 'sum';
Line 1,806 ⟶ 2,075:
printf "Found %d strange unique prime triplets up to $n.\n",
scalar grep { is_prime(sum @$_) } combinations(primes($n), 3);
}</langsyntaxhighlight>
{{out}}
<pre>Found 42 strange unique prime triplets up to 30.
Line 1,812 ⟶ 2,081:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0.8.4"</span><span style="color: #0000FF;">)</span>
Line 1,882 ⟶ 2,151:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,897 ⟶ 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,902 ⟶ 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,917 ⟶ 2,249:
 
mx = 1_000
print(f"\nIf n, m, p < {mx:_} finds {sum(1 for _ in strange_triplets(mx)):_}")</langsyntaxhighlight>
 
{{out}}
Line 1,964 ⟶ 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,973 ⟶ 2,388:
say "Found ", +$_, " strange unique prime triplets up to ", k
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,981 ⟶ 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 2,021 ⟶ 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 2,079 ⟶ 2,494:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 2,101 ⟶ 2,516:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,150 ⟶ 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 2,223 ⟶ 2,720:
strange_unique_prime_triplets(30, true);
strange_unique_prime_triplets(1000, false);
}</langsyntaxhighlight>
 
{{out}}
Line 2,272 ⟶ 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 2,343 ⟶ 2,946:
 
strangeUniquePrimeTriplets(limit: 30, verbose: true)
strangeUniquePrimeTriplets(limit: 1000, verbose: false)</langsyntaxhighlight>
 
{{out}}
Line 2,398 ⟶ 3,001:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports DT = System.DateTime
 
Module Module1
Line 2,472 ⟶ 3,075:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Same as C#</pre>
Line 2,479 ⟶ 3,082:
===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 2,508 ⟶ 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 2,561 ⟶ 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,591 ⟶ 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