Double Twin Primes: Difference between revisions

Added Sidef
(Added Sidef)
 
(10 intermediate revisions by 8 users not shown)
Line 345:
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Classes,SysUtils,StdCtrls}}
Uses standard TList as FIFO to hold and test groups of four sequentail prime numbers.
 
<syntaxhighlight lang="Delphi">
 
 
function IsPrime(N: integer): boolean;
{Fast, optimised prime test}
var I,Stop: integer;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
 
 
function GetNextPrime(var Start: integer): integer;
{Get the next prime number after Start}
{Start is passed by "reference," so the
{original variable is incremented}
begin
repeat Inc(Start)
until IsPrime(Start);
Result:=Start;
end;
 
procedure ShowDoubleTwinPrimes(Memo: TMemo);
{Find sets of four primes P1,P2,P3,P4, where}
{P2-P1=2 P4-P3=2 and P3-P2=4 }
{Use TList as FIFO to test all four-prime combinations}
var LS: TList;
var Start: integer;
begin
LS:=TList.Create;
try
Start:=0;
while true do
begin
{Put four primes in the list}
repeat LS.Add(Pointer(GetNextPrime(Start)))
until LS.Count=4;
if Integer(LS[3])>=1000 then break;
{Test if they are double twin prime}
if (Integer(LS[1])-Integer(LS[0])=2) and
(Integer(LS[3])-Integer(LS[2])=2) and
(Integer(LS[2])-Integer(LS[1])=4) then
begin
{Display the result}
Memo.Lines.Add(IntToStr(Integer(LS[0]))+' '+
IntToStr(Integer(LS[1]))+' '+
IntToStr(Integer(LS[2]))+' '+
IntToStr(Integer(LS[3])));
end;
{Delete the first prime}
LS.Delete(0);
end;
finally LS.Free; end;
end;
 
</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|FreeBASIC}}==
Line 368 ⟶ 451:
191 193 197 199
821 823 827 829</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn IsPrime( n as long ) as BOOL
long i
BOOL result = YES
if ( n < 2 ) then result = NO : exit fn
for i = 2 to n + 1
if ( i * i <= n ) and ( n mod i == 0 )
result = NO : exit fn
end if
next
end fn = result
 
local fn DoubleTwinPrimes( limit as long )
NSUInteger num = 3
printf @"Double twin primes < %ld:", limit
do
if fn IsPrime( num )
if fn IsPrime( num + 2 )
if fn IsPrime( num + 6 )
if fn IsPrime( num + 8 ) then printf @"%4lu%7lu%7lu%7lu", num, num + 2, num + 6, num + 8
end if
end if
end if
num += 2
until ( num > limit )
end fn
 
fn DoubleTwinPrimes( 2000 )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Double twin primes < 2000:
5 7 11 13
11 13 17 19
101 103 107 109
191 193 197 199
821 823 827 829
1481 1483 1487 1489
1871 1873 1877 1879
</pre>
 
 
=={{header|Go}}==
Line 397 ⟶ 526:
[ 191 193 197 199]
[ 821 823 827 829]
</pre>
 
=={{header|J}}==
<syntaxhighlight lang=J> _6 _4 0 2+/~(#~ 0,4=2-~/\])p:~.,0 1+/~/I.2=2 -~/\ i.&.(p:inv) 1000
5 7 11 13
11 13 17 19
101 103 107 109
191 193 197 199
821 823 827 829</syntaxhighlight>
 
Breaking this down:
 
<syntaxhighlight lang=J> primes=: i.&.(p:inv) 1000
twinprimes=: p:~.,0 1+/~/I.2=2 -~/\ primes
doubletwinprimes=: _6 _4 0 2+/~(#~ 0,4=2-~/\])</syntaxhighlight>
 
The first two expressions rely on the primitive <code>p:</code> which translates between the index of a prime number and the prime itself. The final expression instead filters the remaining primes (because the sequence of primes which was its argument had already been filtered enough to have made indices into that sequence into relevant information which was not worth recalculating).
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
# Input: a positive integer
# Output: an array, $a, of length .+1 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; length; i)) as $j (.; .[$j] = false)
else .
end;
(. + 1) as $n
| (($n|sqrt) / 2) as $s
| [null, null, range(2; $n)]
| reduce (2, 1 + (2 * range(1; $s))) as $i (.; erase($i)) ;
 
def double_twin_primes($n):
[$n|primeSieve|range(0;length) as $i | select(.[$i]) | $i] as $p
| range(1; $p|length-3) as $i
| select( ($p[$i+1] - $p[$i]) == 2 and ($p[$i+2] - $p[$i+1]) == 4 and ($p[$i+3] - $p[$i+2]) == 2 )
| [$p[$i, $i+1, $i+2, $i+3]] ;
 
 
"Double twin primes under 1,000:",
double_twin_primes(1000)
</syntaxhighlight>
{{output}}
<pre>
Double twin primes under 1,000:
[5,7,11,13]
[11,13,17,19]
[101,103,107,109]
[191,193,197,199]
[821,823,827,829]
</pre>
 
Line 415 ⟶ 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 524 ⟶ 737:
821 823 827 829
done...
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
res = Prime.each(1000).each_cons(4).select do |p1, p2, p3, p4|
p1+2 == p2 && p2+4 == p3 && p3+2 == p4
end
 
res.each{|slice| puts slice.join(", ")}
</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|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>
 
Line 529 ⟶ 814:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
2,747

edits