Strange unique prime triplets: Difference between revisions

Line 1,407:
224.940756 seconds (35 allocations: 218.156 KiB)
</pre>
 
=={{header|Nim}}==
<lang Nim>import strformat, strutils, sugar
 
func isPrime(n: Positive): bool =
if n < 2: return false
if n mod 2 == 0: return n == 2
if n mod 3 == 0: return n == 3
var d = 5
while d * d <= n:
if n mod d == 0: return false
inc d, 2
if n mod d == 0: return false
inc d, 4
result = true
 
 
iterator triplets(primes: openArray[int]): (int, int, int) =
## Yield the triplets.
for i in 0..primes.high-2:
let n = primes[i]
for j in (i+1)..primes.high-1:
let m = primes[j]
for k in (j+1)..primes.high:
let p = primes[k]
if (n + m + p).isPrime:
yield (n, m, p)
 
 
const Primes30 = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
echo "List of strange unique prime triplets for n < m < p < 30:"
for (n, m, p) in Primes30.triplets():
echo &"{n:2} + {m:2} + {p:2} = {n+m+p}"
 
echo()
const Primes1000 = collect(newSeq):
for n in 2..999:
if n.isPrime: n
var count = 0
for _ in Primes1000.triplets(): inc count
echo "Count of strange unique prime triplets for n < m < p < 1000: ", ($count).insertSep()</lang>
 
{{out}}
<pre>List of strange unique prime triplets for n < m < p < 30:
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 for n < m < p < 1000: 241_580</pre>
 
=={{header|Pascal}}==
{{works with|Free Pascal}}
Anonymous user