Strange unique prime triplets: Difference between revisions
Content deleted Content added
Added Sidef |
Added Easylang |
||
(20 intermediate revisions by 9 users not shown) | |||
Line 12:
{{trans|Python}}
<
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))</
{{out}}
Line 89:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<
PROC Main()
Line 122:
OD
PrintF("%EThere are %I prime triplets",count)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Strange_unique_prime_triplets.png Screenshot from Atari 8-bit computer]
Line 147:
{{Trans|Algol W}} which is based on {{Trans|Wren}}
{{libheader|ALGOL 68-primes}}
<
# where n + m + p is also prime and n =/= m =/= p #
# we need to find the strange unique prime triplets below 1000 #
Line 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</
{{out}}
<pre>
Line 239:
=={{header|ALGOL W}}==
Based on {{Trans|Wren}}
<
% where n + m + p is also prime and n =/= m =/= p %
% sets p( 1 :: n ) to a sieve of primes up to n %
Line 289:
write( i_w := 3, s_w := 0, "Found ", sCount, " strange unique prime triplets up to 1000" );
end
end.</
{{out}}
<pre>
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}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f STRANGE_UNIQUE_PRIME_TRIPLETS.AWK
# converted from Go
Line 380 ⟶ 446:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 431 ⟶ 497:
=={{header|C}}==
<
#include <stdio.h>
#include <string.h>
Line 524 ⟶ 590:
return 0;
}</
{{out}}
<pre>Strange unique prime triplets < 30:
Line 575 ⟶ 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.
<
class Program { static void Main(string[] args) { string s;
Line 597 ⟶ 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; } }</
{{out}}
Timings from tio.run
Line 650 ⟶ 716:
=={{header|C++}}==
<
#include <iostream>
#include <vector>
Line 710 ⟶ 776:
strange_unique_prime_triplets(1000, false);
return 0;
}</
{{out}}
Line 766 ⟶ 832:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Strange_primes;
Line 852 ⟶ 918:
writeln('There are ', cs, ' unique prime triples under 1,000 which sum to a prime.');
readln;
end.</
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
if num < 2
return 0
.
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
lim = 29
for n = 1 to lim
for m = n + 1 to lim
for p = m + 1 to lim
sum = n + m + p
if isprim sum = 1 and isprim n = 1 and isprim m = 1 and isprim p = 1
write "(" & n & " " & m & " " & p & ") "
.
.
.
.
</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)
</pre>
=={{header|F_Sharp|F#}}==
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br>
<
// Strange unique prime triplets. Nigel Galloway: March 12th., 2021
let sP n=let N=primes32()|>Seq.takeWhile((>)n)|>Array.ofSeq
Line 863 ⟶ 961:
printfn "%d" (Seq.length(sP 1000))
printfn "%d" (Seq.length(sP 10000))
</syntaxhighlight>
{{out}}
<pre>
Line 869 ⟶ 967:
74588542
</pre>
=={{header|Factor}}==
<
sequences tools.memory.private ;
Line 886 ⟶ 985:
30 strange
1,000 count-strange commas nl
"Found %s strange prime triplets with n, m, p < 1,000.\n" printf</
{{out}}
<pre>
Line 936 ⟶ 1,035:
=={{header|Fermat}}==
<
if Isprime(n) and Isprime(m) and Isprime(p) and Isprime(n+m+p) then 1 else 0 fi.
Line 945 ⟶ 1,044:
od;
od;
od</
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.
<
dim as uinteger c = 0
Line 968 ⟶ 1,067:
next p
print "There are ";c;" triples below 1000."</
{{out}}<pre>3 + 5 + 11 = 19
3 + 5 + 23 = 31
Line 1,015 ⟶ 1,114:
=={{header|Forth}}==
{{works with|Gforth}}
<
: notprime! ( n -- ) here + 1 swap c! ;
Line 1,084 ⟶ 1,183:
." Count of strange unique prime triplets < 1000: "
1000 count_strange_unique_prime_triplets . cr
bye</
{{out}}
Line 1,135 ⟶ 1,234:
=={{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}}
<
import "fmt"
Line 1,220 ⟶ 1,336:
cs := commatize(strangePrimes(999, true))
fmt.Printf("\nThere are %s unique prime triples under 1,000 which sum to a prime.\n", cs)
}</
{{out}}
Line 1,273 ⟶ 1,389:
===Faster===
{{trans|Wren}}
<
import "fmt"
Line 1,357 ⟶ 1,473:
cs := commatize(strangePrimes(999, true))
fmt.Printf("\nThere are %s unique prime triples under 1,000 which sum to a prime.\n", cs)
}</
{{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}}==
<
public class StrangeUniquePrimeTriplets {
Line 1,423 ⟶ 1,591:
return sieve;
}
}</
{{out}}
Line 1,481 ⟶ 1,649:
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`.
<
def task($n):
Line 1,493 ⟶ 1,661:
task(30),
"\nStretch goal: \(count(task(1000)))"</
{{out}}
<pre>
Line 1,541 ⟶ 1,709:
Stretch goal: 241580
</pre>
=={{header|Julia}}==
<
function prime_sum_prime_triplets_to(N, verbose=false)
Line 1,569 ⟶ 1,738:
@time prime_sum_prime_triplets_to(10000)
@time prime_sum_prime_triplets_to(100000)
</
<pre>
Triplet Sum
Line 1,632 ⟶ 1,801:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Select[Subsets[p, {3}], Total/*PrimeQ]
p = Prime[Range@PrimePi[1000]];
Length[Select[Subsets[p, {3}], Total/*PrimeQ]]</
{{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}}
Line 1,642 ⟶ 1,811:
=={{header|Nim}}==
<
func isPrime(n: Positive): bool =
Line 1,680 ⟶ 1,849:
var count = 0
for _ in Primes1000.triplets(): inc count
echo "Count of strange unique prime triplets for n < m < p < 1000: ", ($count).insertSep()</
{{out}}
Line 1,731 ⟶ 1,900:
=={{header|Pascal}}==
{{works with|Free Pascal}}
<
//Free Pascal Compiler version 3.2.1 [2020/11/03] for x86_64fpc 3.2.1
{$IFDEF FPC}
Line 1,874 ⟶ 2,043:
Check_Limit(10000);
//Check_Limit(MAXZAHL);
END.</
{{out}}
<pre>
Line 1,929 ⟶ 2,098:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use List::Util 'sum';
Line 1,938 ⟶ 2,107:
printf "Found %d strange unique prime triplets up to $n.\n",
scalar grep { is_prime(sum @$_) } combinations(primes($n), 3);
}</
{{out}}
<pre>Found 42 strange unique prime triplets up to 30.
Line 1,944 ⟶ 2,113:
=={{header|Phix}}==
<!--<
<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 2,014 ⟶ 2,183:
<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>
<!--</
{{out}}
<pre>
Line 2,029 ⟶ 2,198:
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,034 ⟶ 2,266:
Using [https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.generate.Sieve.primerange sympy.primerange].
<
def strange_triplets(mx: int = 30) -> None:
Line 2,049 ⟶ 2,281:
mx = 1_000
print(f"\nIf n, m, p < {mx:_} finds {sum(1 for _ in strange_triplets(mx)):_}")</
{{out}}
Line 2,096 ⟶ 2,328:
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"
for 30, 1000 -> \k {
Line 2,105 ⟶ 2,420:
say "Found ", +$_, " strange unique prime triplets up to ", k
}
}</
{{out}}
<pre>
Line 2,113 ⟶ 2,428:
=={{header|REXX}}==
<
parse arg hi . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 30 /*Not specified? Then use the default.*/
Line 2,153 ⟶ 2,468:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; s.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</
{{out|output|text= when using the default input:}}
<pre>
Line 2,211 ⟶ 2,526:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 2,233 ⟶ 2,548:
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 2,282 ⟶ 2,597:
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}}==
<
let mut sieve = vec![true; limit];
if limit > 0 {
Line 2,355 ⟶ 2,752:
strange_unique_prime_triplets(30, true);
strange_unique_prime_triplets(1000, false);
}</
{{out}}
Line 2,404 ⟶ 2,801:
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}}==
<
var triplets = []
combinations(n.primes, 3, {|*a|
Line 2,417 ⟶ 2,894:
}
printf("Found %d strange unique prime triplets up to %s.\n", triplets.len, n)
}</
{{out}}
<pre>
Line 2,433 ⟶ 2,910:
=={{header|Swift}}==
<
func primeSieve(limit: Int) -> [Bool] {
Line 2,501 ⟶ 2,978:
strangeUniquePrimeTriplets(limit: 30, verbose: true)
strangeUniquePrimeTriplets(limit: 1000, verbose: false)</
{{out}}
Line 2,556 ⟶ 3,033:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Module Module1
Line 2,630 ⟶ 3,107:
End Sub
End Module</
{{out}}
<pre>Same as C#</pre>
Line 2,637 ⟶ 3,114:
===Basic===
{{libheader|Wren-math}}
{{libheader|Wren-
{{libheader|Wren-fmt}}
<
import "./
import "./fmt" for Fmt
var strangePrimes = Fn.new { |n, countOnly|
Line 2,666 ⟶ 3,143:
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)</
{{out}}
Line 2,719 ⟶ 3,196:
===Faster===
The following version uses a prime sieve and is about 17 times faster than the 'basic' version.
<
import "./fmt" for Fmt
var max = 1000
Line 2,749 ⟶ 3,226:
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)</
{{out}}
Line 2,755 ⟶ 3,232:
=={{header|XPL0}}==
<
int N, I;
[if N <= 2 then return N = 2;
Line 2,788 ⟶ 3,265:
];
];
]</
{{out}}
|