Cousin primes: Difference between revisions

Added Algol 68
(Add C)
(Added Algol 68)
Line 18:
:*   the MathWorld entry:   [https://mathworld.wolfram.com/CousinPrimes.html cousin primes].
<br><br>
 
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # find cousin primes - pairs of primes that differ by 4 #
INT max number = 1000;
# sieve the primes to max number #
[ 1 : max number ]BOOL prime;
prime[ 1 ] := FALSE; prime[ 2 ] := TRUE;
FOR i FROM 3 BY 2 TO max number DO prime[ i ] := TRUE OD;
FOR i FROM 4 BY 2 TO max number DO prime[ i ] := FALSE OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( max number ) DO
IF prime[ i ] THEN
FOR s FROM i * i BY i + i TO max number DO prime[ s ] := FALSE OD
FI
OD;
# returns text right padded to length, if it is shorter #
PROC right pad = ( STRING text, INT length )STRING:
IF INT t length = ( UPB text - LWB text ) + 1;
t length >= length
THEN text
ELSE text + ( ( length - t length ) * " " )
FI # right pad # ;
# look through the primes for cousins #
INT p count := 0;
FOR i TO max number - 4 DO
IF prime[ i ] THEN
IF prime[ i + 4 ] THEN
# have a pair of cousin primes #
p count +:= 1;
IF ODD p count THEN
print( ( whole( p count, -5 ), ": ", whole( i, -5 ), "-", right pad( whole( i + 4, 0 ), 5 ) ) )
ELSE
print( ( " ", whole( i, -5 ), "-", whole( i + 4, 0 ), newline ) )
FI
FI
FI
OD;
print( ( newline, "Found ", whole( p count, 0 ), " cousin primes", newline ) )
END</lang>
{{out}}
<pre>
1: 3-7 7-11
3: 13-17 19-23
5: 37-41 43-47
7: 67-71 79-83
9: 97-101 103-107
11: 109-113 127-131
13: 163-167 193-197
15: 223-227 229-233
17: 277-281 307-311
19: 313-317 349-353
21: 379-383 397-401
23: 439-443 457-461
25: 463-467 487-491
27: 499-503 613-617
29: 643-647 673-677
31: 739-743 757-761
33: 769-773 823-827
35: 853-857 859-863
37: 877-881 883-887
39: 907-911 937-941
41: 967-971
Found 41 cousin primes
</pre>
 
=={{header|APL}}==
3,044

edits