Quadrat special primes: Difference between revisions
Content added Content deleted
m (→{{header|ALGOL W}}: tweaked the code) |
(Added Algol 68) |
||
Line 6: | Line 6: | ||
where '''n''' < '''16000'''. |
where '''n''' < '''16000'''. |
||
<br><br> |
<br><br> |
||
=={{header|ALGOL 68}}== |
|||
{{Trans|ALGOL W}} |
|||
<lang algol68>BEGIN # find some primes where the gap between the current prime and the next is a square # |
|||
# reurns a sieve of primes up to n # |
|||
PROC eratosthenes = ( INT n )[]BOOL: |
|||
BEGIN |
|||
[ 1 : n ]BOOL p; |
|||
p[ 1 ] := FALSE; p[ 2 ] := TRUE; |
|||
FOR i FROM 3 BY 2 TO n DO p[ i ] := TRUE OD; |
|||
FOR i FROM 4 BY 2 TO n DO p[ i ] := FALSE OD; |
|||
FOR i FROM 3 BY 2 TO ENTIER sqrt( n ) DO |
|||
IF p[ i ] THEN FOR s FROM i * i BY i + i TO n DO p[ s ] := FALSE OD FI |
|||
OD; |
|||
p |
|||
END # eratosthenes # ; |
|||
# an array of squares # |
|||
PROC get squares = ( INT n )[]INT: |
|||
BEGIN |
|||
[ 1 : n ]INT s; |
|||
FOR i TO n DO s[ i ] := i * i OD; |
|||
s |
|||
END # get squares # ; |
|||
INT max number = 16000; |
|||
[]BOOL prime = eratosthenes( max number ); |
|||
[]INT square = get squares( max number ); |
|||
INT p count, this prime, next prime; |
|||
# the first square gap is 1 (between 2 and 3) the gap between all other primes is even # |
|||
# so we treat 2-3 as a special case # |
|||
p count := 1; this prime := 2; next prime := 3; |
|||
print( ( " ", whole( this prime, -5 ) ) ); |
|||
WHILE next prime < max number DO |
|||
this prime := next prime; |
|||
p count +:= 1; |
|||
print( ( " ", whole( this prime, -5 ) ) ); |
|||
IF p count MOD 12 = 0 THEN print( ( newline ) ) FI; |
|||
INT sq pos := 2; |
|||
WHILE next prime := this prime + square[ sq pos ]; |
|||
IF next prime < max number THEN NOT prime[ next prime ] ELSE FALSE FI |
|||
DO sq pos +:= 2 OD |
|||
OD |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
2 3 7 11 47 83 227 263 587 911 947 983 |
|||
1019 1163 1307 1451 1487 1523 1559 2459 3359 4259 4583 5483 |
|||
5519 5843 5879 6203 6779 7103 7247 7283 7607 7643 8219 8363 |
|||
10667 11243 11279 11423 12323 12647 12791 13367 13691 14591 14627 14771 |
|||
15671 |
|||
</pre> |
|||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |