Double Twin Primes: Difference between revisions

Added Sidef
(Delphi version of double twin primes)
(Added Sidef)
 
(4 intermediate revisions by 4 users not shown)
Line 548:
'''Also works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
# Input: a positive integer
# return an array such that .[$i] is $i if $i is prime and false otherwise
# Output: an array, $a, of length .+1 such that
# return an array such that . $a[$i] is $i if $i is prime, and false otherwise.
def primeSieve:
# erase(i) sets .[i*j] to false for integral j > 1
def erase(i):
if .[i] then
reduce (range(2*i; (1 + length) /; i)) as $j (.; .[i * $j] = false)
else .
end;
Line 597 ⟶ 599:
printdt(1000)
</syntaxhighlight>{{out}} Same as C example.
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/strformat
 
func isPrime(n: Positive): bool =
if n < 2: return false
if (n and 1) == 0: return n == 2
if n mod 3 == 0: return n == 3
var k = 5
var delta = 2
while k * k <= n:
if n mod k == 0: return false
inc k, delta
delta = 6 - delta
result = true
 
echo "Double twin primes under 1000:"
for n in countup(3, 991, 2):
if isPrime(n) and isPrime(n + 2) and isPrime(n + 6) and isPrime(n + 8):
echo &"({n:>3}, {n+2:>3}, {n+6:>3}, {n+8:>3})"
</syntaxhighlight>
 
{{out}}
<pre>( 5, 7, 11, 13)
( 11, 13, 17, 19)
(101, 103, 107, 109)
(191, 193, 197, 199)
(821, 823, 827, 829)
</pre>
 
=={{header|Perl}}==
Line 724 ⟶ 755:
821, 823, 827, 829
</pre>
 
=={{header|Sidef}}==
 
<syntaxhighlight lang="ruby">1000.primes.each_cons(4, {|*a|
if (a.diffs == [2, 4, 2]) {
say a
}
})</syntaxhighlight>
{{out}}
<pre>
[5, 7, 11, 13]
[11, 13, 17, 19]
[101, 103, 107, 109]
[191, 193, 197, 199]
[821, 823, 827, 829]
</pre>
 
=={{header|V (Vlang)}}==
{{trans|Ring}}
<syntaxhighlight lang="Zig">
import math
 
fn main() {
limit := 1000
mut parr := []int{}
for n in 1..limit {
if is_prime(n) {parr << n}
}
for m in 1..parr.len - 3 {
if is_prime(parr[m]) && is_prime(parr[m + 1]) && is_prime(parr[m + 2]) && is_prime(parr[m + 3]) {
if parr[m + 1] - parr[m] == 2 && parr[m + 2] - parr[m + 1] == 4 && parr[m + 3] - parr[m + 2] == 2 {
println("${parr[m]} ${parr[m + 1]} ${parr[m + 2]} ${parr[m + 3]}")
}
}
}
}
 
fn is_prime(num i64) bool {
if num <= 1 {return false}
if num % 2 == 0 && num != 2 {return false}
for idx := 3; idx <= math.floor(num / 2) - 1; idx += 2 {
if num % idx == 0 {return false}
}
return true
}
</syntaxhighlight>
 
{{out}}
<pre>
5 7 11 13
11 13 17 19
101 103 107 109
191 193 197 199
821 823 827 829
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
2,747

edits