Strange unique prime triplets: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(16 intermediate revisions by 8 users not shown)
Line 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}}==
Line 869 ⟶ 935:
74588542
</pre>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: formatting io kernel math math.combinatorics math.primes
Line 1,135 ⟶ 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}}==
Line 1,361 ⟶ 1,445:
{{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}}==
Line 1,541 ⟶ 1,677:
Stretch goal: 241580
</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Primes
Line 2,029 ⟶ 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 2,096 ⟶ 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}}==
Line 2,282 ⟶ 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>
 
Line 2,407 ⟶ 2,772:
 
=={{header|Scala}}==
Scala3 ready
<syntaxhighlight lang="scala">
val primeStream5 = LazyList.from(5, 6)
Line 2,437 ⟶ 2,803:
}
</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}}==
Line 2,669 ⟶ 3,082:
===Basic===
{{libheader|Wren-math}}
{{libheader|Wren-traititerate}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./traititerate" for Stepped
import "./fmt" for Fmt
 
var strangePrimes = Fn.new { |n, countOnly|
Line 2,751 ⟶ 3,164:
===Faster===
The following version uses a prime sieve and is about 17 times faster than the 'basic' version.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
var max = 1000
9,476

edits