Prime triplets: Difference between revisions
(Added Go) |
(→{{header|Raku}}: Added Raku solutions) |
||
Line 38: | Line 38: | ||
Same as Wren entry. |
Same as Wren entry. |
||
</pre> |
</pre> |
||
=={{header|Raku}}== |
|||
Adapted from [[Cousin primes]] |
|||
===Filter=== |
|||
Favoring brevity over efficiency due to the small range of n, the most concise solution is: |
|||
<lang perl6>say grep *.all.is-prime, map { $_, $_+2, $_+6 }, 2..5500;</lang> |
|||
{{out}} |
|||
<pre> |
|||
((5 7 11) (11 13 17) (17 19 23) (41 43 47) (101 103 107) (107 109 113) (191 193 197) (227 229 233) (311 313 317) (347 349 353) (461 463 467) (641 643 647) (821 823 827) (857 859 863) (881 883 887) (1091 1093 1097) (1277 1279 1283) (1301 1303 1307) (1427 1429 1433) (1481 1483 1487) (1487 1489 1493) (1607 1609 1613) (1871 1873 1877) (1997 1999 2003) (2081 2083 2087) (2237 2239 2243) (2267 2269 2273) (2657 2659 2663) (2687 2689 2693) (3251 3253 3257) (3461 3463 3467) (3527 3529 3533) (3671 3673 3677) (3917 3919 3923) (4001 4003 4007) (4127 4129 4133) (4517 4519 4523) (4637 4639 4643) (4787 4789 4793) (4931 4933 4937) (4967 4969 4973) (5231 5233 5237) (5477 5479 5483)) |
|||
</pre> |
|||
===Infinite List=== |
|||
A more efficient and versatile approach is to generate an infinite list of triple primes, using this info from https://oeis.org/A022004 : |
|||
:All terms are congruent to 5 (mod 6). |
|||
<lang perl6>constant @triples = (5, *+6 … *).map: -> \n { $_ if .all.is-prime given (n, n+2, n+6) } |
|||
my $count = @triples.first: :k, *.[0] >= 5500; |
|||
say .fmt('%4d') for @triples.head($count);</lang> |
|||
{{out}} |
|||
<pre> |
|||
5 7 11 |
|||
11 13 17 |
|||
17 19 23 |
|||
41 43 47 |
|||
101 103 107 |
|||
107 109 113 |
|||
191 193 197 |
|||
227 229 233 |
|||
311 313 317 |
|||
347 349 353 |
|||
461 463 467 |
|||
641 643 647 |
|||
821 823 827 |
|||
857 859 863 |
|||
881 883 887 |
|||
1091 1093 1097 |
|||
1277 1279 1283 |
|||
1301 1303 1307 |
|||
1427 1429 1433 |
|||
1481 1483 1487 |
|||
1487 1489 1493 |
|||
1607 1609 1613 |
|||
1871 1873 1877 |
|||
1997 1999 2003 |
|||
2081 2083 2087 |
|||
2237 2239 2243 |
|||
2267 2269 2273 |
|||
2657 2659 2663 |
|||
2687 2689 2693 |
|||
3251 3253 3257 |
|||
3461 3463 3467 |
|||
3527 3529 3533 |
|||
3671 3673 3677 |
|||
3917 3919 3923 |
|||
4001 4003 4007 |
|||
4127 4129 4133 |
|||
4517 4519 4523 |
|||
4637 4639 4643 |
|||
4787 4789 4793 |
|||
4931 4933 4937 |
|||
4967 4969 4973 |
|||
5231 5233 5237 |
|||
5477 5479 5483</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
Revision as of 21:39, 13 April 2021
- Task
Find and show members of prime triples (p, p+2, p+6), where p < 5500
Go
<lang go>package main
import (
"fmt" "rcu"
)
func main() {
c := rcu.PrimeSieve(5505, false) var triples [][3]int fmt.Println("Prime triplets: p, p + 2, p + 6 where p < 5,500:") for i := 3; i < 5500; i += 2 { if !c[i] && !c[i+2] && !c[i+6] { triples = append(triples, [3]int{i, i + 2, i + 6}) } } for _, triple := range triples { var t [3]string for i := 0; i < 3; i++ { t[i] = rcu.Commatize(triple[i]) } fmt.Printf("%5s %5s %5s\n", t[0], t[1], t[2]) } fmt.Println("\nFound", len(triples), "such prime triplets.")
}</lang>
- Output:
Same as Wren entry.
Raku
Adapted from Cousin primes
Filter
Favoring brevity over efficiency due to the small range of n, the most concise solution is: <lang perl6>say grep *.all.is-prime, map { $_, $_+2, $_+6 }, 2..5500;</lang>
- Output:
((5 7 11) (11 13 17) (17 19 23) (41 43 47) (101 103 107) (107 109 113) (191 193 197) (227 229 233) (311 313 317) (347 349 353) (461 463 467) (641 643 647) (821 823 827) (857 859 863) (881 883 887) (1091 1093 1097) (1277 1279 1283) (1301 1303 1307) (1427 1429 1433) (1481 1483 1487) (1487 1489 1493) (1607 1609 1613) (1871 1873 1877) (1997 1999 2003) (2081 2083 2087) (2237 2239 2243) (2267 2269 2273) (2657 2659 2663) (2687 2689 2693) (3251 3253 3257) (3461 3463 3467) (3527 3529 3533) (3671 3673 3677) (3917 3919 3923) (4001 4003 4007) (4127 4129 4133) (4517 4519 4523) (4637 4639 4643) (4787 4789 4793) (4931 4933 4937) (4967 4969 4973) (5231 5233 5237) (5477 5479 5483))
Infinite List
A more efficient and versatile approach is to generate an infinite list of triple primes, using this info from https://oeis.org/A022004 :
- All terms are congruent to 5 (mod 6).
<lang perl6>constant @triples = (5, *+6 … *).map: -> \n { $_ if .all.is-prime given (n, n+2, n+6) }
my $count = @triples.first: :k, *.[0] >= 5500;
say .fmt('%4d') for @triples.head($count);</lang>
- Output:
5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1091 1093 1097 1277 1279 1283 1301 1303 1307 1427 1429 1433 1481 1483 1487 1487 1489 1493 1607 1609 1613 1871 1873 1877 1997 1999 2003 2081 2083 2087 2237 2239 2243 2267 2269 2273 2657 2659 2663 2687 2689 2693 3251 3253 3257 3461 3463 3467 3527 3529 3533 3671 3673 3677 3917 3919 3923 4001 4003 4007 4127 4129 4133 4517 4519 4523 4637 4639 4643 4787 4789 4793 4931 4933 4937 4967 4969 4973 5231 5233 5237 5477 5479 5483
Ring
<lang ring> load "stdlib.ring" see "working..." + nl see "Initial members of prime triples (p, p+2, p+6) are:" + nl
row = 0 limit = 5500
for n = 1 to limit
if isprime(n) and isprime(n+2) and isprime(n+6) row = row + 1 see "" + n + " " if row%5 = 0 see nl ok ok
next
see nl + "Found " + row + " primes" + nl see "done..." + nl </lang>
- Output:
working... Initial members of prime triples (p, p+2, p+6): 5 11 17 41 101 107 191 227 311 347 461 641 821 857 881 1091 1277 1301 1427 1481 1487 1607 1871 1997 2081 2237 2267 2657 2687 3251 3461 3527 3671 3917 4001 4127 4517 4637 4787 4931 4967 5231 5477 Found 43 primes done...
Wren
<lang ecmascript>import "/math" for Int import "/fmt" for Fmt
var c = Int.primeSieve(5505, false) var triples = [] System.print("Prime triplets: p, p + 2, p + 6 where p < 5,500:") var i = 3 while (i < 5500) {
if (!c[i] && !c[i+2] && !c[i+6]) triples.add([i, i+2, i+6]) i = i + 2
} for (triple in triples) Fmt.print("$,6d", triple) System.print("\nFound %(triples.count) such prime triplets.")</lang>
- Output:
Prime triplets: p, p + 2, p + 6 where p < 5,500: 5 7 11 11 13 17 17 19 23 41 43 47 101 103 107 107 109 113 191 193 197 227 229 233 311 313 317 347 349 353 461 463 467 641 643 647 821 823 827 857 859 863 881 883 887 1,091 1,093 1,097 1,277 1,279 1,283 1,301 1,303 1,307 1,427 1,429 1,433 1,481 1,483 1,487 1,487 1,489 1,493 1,607 1,609 1,613 1,871 1,873 1,877 1,997 1,999 2,003 2,081 2,083 2,087 2,237 2,239 2,243 2,267 2,269 2,273 2,657 2,659 2,663 2,687 2,689 2,693 3,251 3,253 3,257 3,461 3,463 3,467 3,527 3,529 3,533 3,671 3,673 3,677 3,917 3,919 3,923 4,001 4,003 4,007 4,127 4,129 4,133 4,517 4,519 4,523 4,637 4,639 4,643 4,787 4,789 4,793 4,931 4,933 4,937 4,967 4,969 4,973 5,231 5,233 5,237 5,477 5,479 5,483 Found 43 such prime triplets.