Double Twin Primes: Difference between revisions

Content added Content deleted
(added Arturo)
(→‎{{header|ALGOL 68}}: Replaced with simpler version not based on the successive prime differences task)
Line 20: Line 20:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Reusing some code from the [[Successive prime differences#ALGOL_68|Successive prime differences task]].
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-rows}}
<syntaxhighlight lang="algol68">
<syntaxhighlight lang="algol68">
BEGIN # find some sequences of primes where the gaps between the elements #
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 #
# 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 #
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 #
FOR p FROM LWB prime TO UPB prime - 8 DO
PROC try differences = ( []INT primes, []INT pattern, INT max prime )VOID:
BEGIN
IF prime[ p ] THEN
INT pattern length = ( UPB pattern - LWB pattern ) + 1;
IF prime[ p + 2 ] THEN
[ 1 : pattern length + 1 ]INT first; FOR i TO UPB first DO first[ i ] := 0 OD;
IF prime[ p + 6 ] THEN
[ 1 : pattern length + 1 ]INT last; FOR i TO UPB last DO last[ i ] := 0 OD;
IF prime[ p + 8 ] THEN
INT count := 0;
count +:= 1;
FOR p FROM LWB primes + pattern length TO UPB primes DO
print( ( "["
BOOL matched := TRUE;
, whole( p, -4 ), whole( p + 2, -4 )
INT e pos := LWB pattern;
, whole( p + 6, -4 ), whole( p + 8, -4 )
FOR e FROM p - pattern length TO p - 1
, " ]"
WHILE matched := primes[ e + 1 ] - primes[ e ] = pattern[ e pos ]
, newline
DO
)
e pos +:= 1
)
OD;
FI
IF matched THEN
# found a matching sequence #
count +:= 1;
print( ( "[" ) );
SHOW primes[ p - pattern length : p ];
print( ( " ]", newline ) )
FI
FI
OD;
FI
FI
print( ( "Found ", whole( count, 0 ), " prime sequences with differences: [" ) );
OD;
SHOW pattern;
print( ( " ] up to ", whole( max prime, 0 ) ) );
print( ( "Found ", whole( count, 0 ), " double twin primes beflow ", whole( UPB prime, 0 ), newline ) )
print( ( 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
END
</syntaxhighlight>
</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
[ 5 7 11 13 ]
[ 5 7 11 13 ]
[ 11 13 17 19 ]
[ 11 13 17 19 ]
[ 101 103 107 109 ]
[ 101 103 107 109 ]
[ 191 193 197 199 ]
[ 191 193 197 199 ]
[ 821 823 827 829 ]
[ 821 823 827 829 ]
Found 5 prime sequences with differences: [ 2 4 2 ] up to 1000
Found 5 double twin primes beflow 1000
</pre>
</pre>