Prime triplets

From Rosetta Code
Revision as of 19:14, 13 April 2021 by PureFox (talk | contribs) (Added Wren)
Prime triplets is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Find and show members of prime triples (p, p+2, p+6), where p < 5500

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

Library: Wren-math
Library: Wren-fmt

<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.