Double Twin Primes: Difference between revisions

Added Sidef
(Double Twin Primes in various dialects BASIC (BASIC256, Gambas, PureBasic, Run BASIC, XBasic and Yabasic))
(Added Sidef)
 
(22 intermediate revisions by 13 users not shown)
Line 13:
<br>
Find and show here all Double Twin Primes under 1000.
<br><br>
;See also
:* [[oeis:A007530|OEIS:A007530]]
:* [[wp:Prime_k-tuple|Wikipedia:Prime_k-tuple]]
<br><br>
 
=={{header|ALGOL 68}}==
Reusing some code from the [[Successive prime differences#ALGOL_68|Successive prime differences task]].
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-rows}}
<syntaxhighlight lang="algol68">
BEGIN # find some sequences of primes where the gaps between the elements #
# are 2, 4, 2 - i.e., n, n+2, n+6 and n+8 are all prime #
PR read "primes.incl.a68" PR # include prime utilities #
[]BOOL prime = PRIMESIEVE 1 000;
PR read "rows.incl.a68" PR # include row (array) utilities #
INT count := 0;
# attempts to find patterns in the differences of primes and prints the results #
INT p := 3; # 2 cannot be a twin prime, so start with 3 #
PROC try differences = ( []INT primes, []INT pattern, INT max prime )VOID:
WHILE p <= UPB prime BEGIN- 8 DO
BOOL is double twin := FALSE;
INT pattern length = ( UPB pattern - LWB pattern ) + 1;
IF prime[ p ] THEN
[ 1 : pattern length + 1 ]INT first; FOR i TO UPB first DO first[ i ] := 0 OD;
IF prime[ p + 2 ] THEN
[ 1 : pattern length + 1 ]INT last; FOR i TO UPB last DO last[ i ] := 0 OD;
INT countIF :=prime[ 0;p + 6 ] THEN
FOR p FROM LWB primes + pattern length TOIF prime[ p + UPB8 primes] DOTHEN
BOOL matched count +:= TRUE1;
INT e pos is double twin := LWB patternTRUE;
FOR e FROM p - pattern length TO pprint( -( 1"["
WHILE matched := primes[ e + 1 ] - primes[ e ] = pattern[ e pos, whole( p, -4 ), whole( p + 2, -4 ])
DO , whole( p + 6, -4 ), whole( p + 8, -4 )
e pos +:= 1 , " ]"
OD; , newline
IF matched THEN )
# found a matching sequence # )
count +:= 1;FI
print( ( "[" ) );
SHOW primes[ p - pattern length : p ];
print( ( " ]", newline ) )
FI
OD;FI
FI;
print( ( "Found ", whole( count, 0 ), " prime sequences with differences: [" ) );
p +:= IF is SHOWdouble pattern;twin THEN 6 ELSE 2 FI
OD;
print( ( " ] up to ", whole( max prime, 0 ) ) );
print( ( "Found ", whole( count, 0 ), print(" double twin primes below ", whole( UPB prime, 0 ), newline ) )
END # try differences # ;
INT max number = 1 000;
[]INT p list = EXTRACTPRIMESUPTO max number FROMPRIMESIEVE PRIMESIEVE max number;
try differences( p list, ( 2, 4, 2 ), max number )
END
</syntaxhighlight>
{{out}}
<pre>
[ 5 7 11 13 ]
[ 11 13 17 19 ]
[ 101 103 107 109 ]
[ 191 193 197 199 ]
[ 821 823 827 829 ]
Found 5 primedouble sequencestwin withprimes differences: [ 2 4 2 ] up tobeflow 1000
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">r: range .step: 2 1 1000
r | map 'x -> @[x x+2 x+6 x+8]
| select => [every? & => prime?]
| loop => print</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|BASIC}}==
Line 73 ⟶ 83:
 
num = 3
while num <= 1000-8992
if isPrime(num) then
if isPrime(num+2) then
Line 101 ⟶ 111:
End If
num += 2
Loop Until num >= 1000 - 8992
 
End
Line 143 ⟶ 153:
OpenConsole()
num.I = 3
While num <= 1000992
If isPrime(num):
If isPrime(num+2):
Line 175 ⟶ 185:
 
num = 3
while num <= 1000992
if isPrime(num) then
if isPrime(num+2) then
Line 187 ⟶ 197:
end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{Header|Tiny BASIC}}===
<syntaxhighlight lang="qbasic">REM Rosetta Code problem: https://rosettacode.org/wiki/Double_Twin_Primes
REM by Jjuanhdez, 03/2023
 
LET I = 3
10 LET X = I
GOSUB 100
IF Z = 1 THEN LET X = I + 2
IF Z = 1 THEN GOSUB 100
IF Z = 1 THEN LET X = I + 6
IF Z = 1 THEN GOSUB 100
IF Z = 1 THEN LET X = I + 8
IF Z = 1 THEN GOSUB 100
IF Z = 1 THEN PRINT I, " ", I + 2, " ", I + 6, " ", I + 8
LET I = I + 2
IF I > 992 THEN GOTO 20
GOTO 10
20 END
 
100 REM is X a prime? Z=1 for yes, 0 for no
LET Z = 1
IF X = 3 THEN RETURN
IF X = 2 THEN RETURN
LET A = 1
110 LET A = A + 1
IF (X / A) * A = X THEN GOTO 120
IF A * A <= X THEN GOTO 110
RETURN
120 LET Z = 0
RETURN</syntaxhighlight>
<pre>Same as FreeBASIC entry.</pre>
 
Line 211 ⟶ 253:
ENDIF
num%% = num%% + 2
LOOP UNTIL num%% >= 1000-8992
END FUNCTION
 
Line 236 ⟶ 278:
if isPrime(num) if isPrime(num+2) if isPrime(num+6) if isPrime(num+8) print num, " ", num+2, " ", num+6, " ", num+8
num = num + 2
until num >= 1000-8992
end</syntaxhighlight>
{{out}}
Line 278 ⟶ 320:
821 823 827 829
</pre>
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">import 'dart:math';
 
void main() {
for (int num = 3; num < 992; num += 2) {
if (isPrime(num) &&
isPrime(num + 2) &&
isPrime(num + 6) &&
isPrime(num + 8)) {
print("$num ${num + 2} ${num + 6} ${num + 8}");
}
}
}
 
bool isPrime(int n) {
if (n <= 1) return false;
if (n == 2) return true;
for (int i = 2; i <= sqrt(n); ++i) {
if (n % i == 0) return false;
}
return true;
}</syntaxhighlight>
{{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 292 ⟶ 442:
End If
num += 2
Loop Until num >= 1000-8992
 
Sleep</syntaxhighlight>
Line 301 ⟶ 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 330 ⟶ 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 348 ⟶ 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}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl" line>use v5.36;
use ntheory 'is_prime';
 
sub dt ($p) { map { $p + $_ } <0 2 6 8> }
for my $n (1..1000) { say "@{[dt $n]}" if 4 == +(grep { is_prime $_ } dt $n) }</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|Phix}}==
Line 406 ⟶ 700:
=={{header|Ring}}==
<syntaxhighlight lang="ring">
see "worksworking..." + nl
primesP = []
 
limit = 1000
for n =1 to limit
if isPrimeisP(n)
add(primesP,n)
ok
next
lenPrimeslenP = len(primesP)-3
for m = 1 to lenPrimeslenP
if isPrimeisP(primesP[m]) andAND isPrimeisP(primesP[m+1]) andAND isP(P[m+2]) AND isP(P[m+3])
isPrimeif (primesP[m+1] - P[m] = 2) AND (P[m+2] - P[m+1] = 4) andAND isPrime(primesP[m+3] - P[m+2] = 2)
if (primes see " " + P[m+1]+ -" " + primesP[m+1] =+ 2)" and" (primes+ P[m+2] -+ " " + primesP[m+13] = 4) and+ nl
(primes[m+3] - primes[m+2] = 2)
see " " + primes[m]+ " " + primes[m+1] + " " +
primes[m+2] + " " + primes[m+3] + nl
ok
ok
next
 
see "done..." + nl
 
func isPrimeisP num
if (num <= 1) return 0 ok
if (num % 2 = 0 andAND num != 2) return 0 ok
for i = 3 to floor(num / 2) -1 step 2
if (num % i = 0) return 0 ok
next
return 1
</syntaxhighlight>
{{out}}
Line 444 ⟶ 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 449 ⟶ 814:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
2,747

edits