Cousin primes: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(21 intermediate revisions by 15 users not shown)
Line 18:
:*   the MathWorld entry:   [https://mathworld.wolfram.com/CousinPrimes.html cousin primes].
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">V LIMIT = 1000
 
F isPrime(n)
I (n [&] 1) == 0
R n == 2
V m = 3
L m * m <= n
I n % m == 0
R 0B
m += 2
R 1B
 
V PrimeList = (2 .< LIMIT).filter(n -> isPrime(n))
 
V PrimeSet = Set(PrimeList)
 
V cousinList = PrimeList.filter(n -> (n + 4) C PrimeSet).map(n -> (n, n + 4))
 
print(‘Found #. cousin primes less than #.:’.format(cousinList.len, LIMIT))
L(cousins) cousinList
print(String(cousins).center(10), end' I (L.index + 1) % 7 == 0 {"\n"} E ‘ ’)
print()</syntaxhighlight>
 
{{out}}
<pre>
Found 41 cousin primes less than 1000:
(3, 7) (7, 11) (13, 17) (19, 23) (37, 41) (43, 47) (67, 71)
(79, 83) (97, 101) (103, 107) (109, 113) (127, 131) (163, 167) (193, 197)
(223, 227) (229, 233) (277, 281) (307, 311) (313, 317) (349, 353) (379, 383)
(397, 401) (439, 443) (457, 461) (463, 467) (487, 491) (499, 503) (613, 617)
(643, 647) (673, 677) (739, 743) (757, 761) (769, 773) (823, 827) (853, 857)
(859, 863) (877, 881) (883, 887) (907, 911) (937, 941) (967, 971)
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT"
 
PROC Main()
DEFINE MAX="999"
BYTE ARRAY primes(MAX+1)
INT i,count=[0]
 
Put(125) PutE() ;clear the screen
Sieve(primes,MAX+1)
FOR i=2 TO MAX-4
DO
IF primes(i)=1 AND primes(i+4)=1 THEN
PrintF("(%I,%I) ",i,i+4)
count==+1
FI
OD
PrintF("%E%EThere are %I pairs",count)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Cousin_primes.png Screenshot from Atari 8-bit computer]
<pre>
(3,7) (7,11) (13,17) (19,23) (37,41) (43,47) (67,71) (79,83) (97,101) (103,107)
(109,113) (127,131) (163,167) (193,197) (223,227) (229,233) (277,281) (307,311)
(313,317) (349,353) (379,383) (397,401) (439,443) (457,461) (463,467) (487,491)
(499,503) (613,617) (643,647) (673,677) (739,743) (757,761) (769,773) (823,827)
(853,857) (859,863) (877,881) (883,887) (907,911) (937,941) (967,971)
 
There are 41 pairs
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
 
procedure Cousin_Primes is
Line 65 ⟶ 134:
New_Line;
Put_Line (Count'Image & " pairs.");
end Cousin_Primes;</langsyntaxhighlight>
{{out}}
<pre>[ 3, 7] [ 7, 11] [ 13, 17] [ 19, 23] [ 37, 41] [ 43, 47] [ 67, 71] [ 79, 83]
Line 76 ⟶ 145:
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find cousin primes - pairs of primes that differ by 4 #
<syntaxhighlight lang="algol68">BEGIN # find cousin primes - pairs of primes that differ by 4 #
INT max number = 1000;
# sieve the primes toas maxrequired numberby the task #
PR read "primes.incl.a68" PR
[ 1 : max number ]BOOL prime;
prime[ 1 ] := FALSE;BOOL prime[ 2= ] :=PRIMESIEVE TRUE1000;
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:
Line 97 ⟶ 159:
# look through the primes for cousins #
INT p count := 0;
FOR i TO maxUPB numberprime - 4 DO
IF prime[ i ] THEN
IF prime[ i + 4 ] THEN
# have a pair of cousin primes #
p count +:= 1;
IFprint( ODD( pwhole( counti, THEN-5 ), "-", right pad( whole( i + 4, 0 ), 5 ) ) );
print( ( whole(IF p count, -5MOD ),10 ":= 0 ",THEN wholeprint( i, -5 ), "-", right pad( whole( i + 4, 0newline ), 5 ) ) )FI
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</langsyntaxhighlight>
{{out}}
<pre>
1:3-7 3-7-11 13-17 7 19-1123 37-41 43-47 67-71 79-83 97-101 103-107
109-113 127-131 163-167 193-197 223-227 229-233 277-281 307-311 313-317 349-353
3: 13-17 19-23
379-383 397-401 439-443 457-461 463-467 487-491 499-503 613-617 643-647 673-677
5: 37-41 43-47
739-743 757-761 769-773 823-827 853-857 859-863 877-881 883-887 907-911 937-941
7: 67-71 79-83
967-971
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|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin % find some cousin primes: primes p where p + 4 is also a prime %
integer MAX_PRIME;
MAX_PRIME := 1000;
Line 166 ⟶ 209:
write( i_w := 1, s_w := 0, "Found ", cCount, " cousin prime pairs up to ", MAX_PRIME )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 178 ⟶ 221:
 
=={{header|APL}}==
<langsyntaxhighlight APLlang="apl">(⎕←'Amount:',⊃⍴P)⊢P,4+P←⍪((P+4)∊P)/P←(~P∊P∘.×P)/P←1↓⍳1000</langsyntaxhighlight>
 
{{out}}
Line 226 ⟶ 269:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">on sieveOfEratosthenes(limit)
script o
property numberList : {missing value}
Line 251 ⟶ 294:
if (p - 4 is in primes) then set end of output to {p - 4, p's contents}
end repeat
return {|cousin prime pairs < 1000|:output, |count thereof|:(count output)}</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{|cousin prime pairs < 1000|:{{3, 7}, {7, 11}, {13, 17}, {19, 23}, {37, 41}, {43, 47}, {67, 71}, {79, 83}, {97, 101}, {103, 107}, {109, 113}, {127, 131}, {163, 167}, {193, 197}, {223, 227}, {229, 233}, {277, 281}, {307, 311}, {313, 317}, {349, 353}, {379, 383}, {397, 401}, {439, 443}, {457, 461}, {463, 467}, {487, 491}, {499, 503}, {613, 617}, {643, 647}, {673, 677}, {739, 743}, {757, 761}, {769, 773}, {823, 827}, {853, 857}, {859, 863}, {877, 881}, {883, 887}, {907, 911}, {937, 941}, {967, 971}}, |count thereof|:41}</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">cousins: function [upto][
primesUpto: select 0..upto => prime?
return select primesUpto => [prime? & + 4]
]
 
print map cousins 1000 'c -> @[c, c + 4]</langsyntaxhighlight>
 
{{out}}
Line 270 ⟶ 313:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f COUSIN_PRIMES.AWK
BEGIN {
Line 294 ⟶ 337:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 306 ⟶ 349:
 
=={{header|BASIC}}==
<langsyntaxhighlight BASIClang="basic">10 DEFINT A-Z: L=1000: DIM S(L)
20 FOR P=2 TO SQR(L)
30 IF S(P) THEN 50
Line 315 ⟶ 358:
80 IF S(P)+S(P+4)=0 THEN N=N+1: PRINT P,P+4
90 NEXT
100 PRINT "There are";N;"cousin prime pairs below";L</langsyntaxhighlight>
 
{{out}}
Line 363 ⟶ 406:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
manifest $( LIMIT = 1000 $)
Line 394 ⟶ 437:
$)
writef("*N%N pairs found.*N", count)
$)</langsyntaxhighlight>
{{out}}
<pre style="height:14em;">3, 7
Line 441 ⟶ 484:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
Line 469 ⟶ 512:
printf("There are %d cousin prime pairs below %d.\n", count, LIMIT);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 515 ⟶ 558:
967: 971
There are 41 cousin prime pairs below 1000.</pre>
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. COUSIN-PRIMES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 PRIME-SIEVE.
02 PRIME-FLAG PIC 9 OCCURS 1000 INDEXED BY P, Q.
88 PRIME VALUE 1.
02 STEP-SIZE PIC 999.
02 X PIC 999.
02 P-START PIC 999.
02 AMOUNT PIC 999 VALUE 0.
01 OUTPUT-FORMAT.
02 COUSIN1 PIC ZZ9.
02 COUSIN2 PIC ZZ9.
PROCEDURE DIVISION.
BEGIN.
PERFORM SIEVE.
PERFORM TEST-COUSINS VARYING P FROM 2 BY 1
UNTIL P IS GREATER THAN 996.
MOVE AMOUNT TO COUSIN1.
DISPLAY COUSIN1 ' pairs found.'
STOP RUN.
TEST-COUSINS.
IF PRIME(P) AND PRIME(P + 4)
SET X TO P
MOVE X TO COUSIN1
ADD X, 4 GIVING COUSIN2
DISPLAY COUSIN1 ' ' COUSIN2
ADD 1 TO AMOUNT.
SIEVE SECTION.
BEGIN.
PERFORM FLAG-PRIME VARYING Q FROM 1 BY 1
UNTIL Q IS GREATER THAN 1000.
PERFORM SIEVE-PRIME VARYING P FROM 2 BY 1
UNTIL P IS GREATER THAN 32.
GO TO DONE.
SIEVE-PRIME.
IF PRIME(P)
SET X TO P
COMPUTE P-START = X ** 2
PERFORM UNFLAG-PRIME VARYING Q FROM P-START BY X
UNTIL Q IS GREATER THAN 1000.
FLAG-PRIME. MOVE 1 TO PRIME-FLAG(Q).
UNFLAG-PRIME. MOVE 0 TO PRIME-FLAG(Q).
DONE. EXIT.</syntaxhighlight>
{{out}}
<pre style='height:14em;'> 3 7
7 11
13 17
19 23
37 41
43 47
67 71
79 83
97 101
103 107
109 113
127 131
163 167
193 197
223 227
229 233
277 281
307 311
313 317
349 353
379 383
397 401
439 443
457 461
463 467
487 491
499 503
613 617
643 647
673 677
739 743
757 761
769 773
823 827
853 857
859 863
877 881
883 887
907 911
937 941
967 971
41 pairs found.</pre>
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
const LIMIT := 1000;
Line 554 ⟶ 693:
print(" cousin prime pairs below ");
print_i16(LIMIT);
print_nl();</langsyntaxhighlight>
 
{{out}}
Line 603 ⟶ 742:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Cousin Primes: Nigel Galloway. April 2nd., 2021
primes32()|>Seq.pairwise|>Seq.takeWhile(fun(_,n)->n<1000)|>Seq.filter(fun(n,g)->g-n=4)|>Seq.iter(fun(n,g)->printf "(%d,%d) "n g); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 613 ⟶ 752:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: kernel lists lists.lazy math math.primes prettyprint
sequences ;
 
Line 620 ⟶ 759:
[ [ prime? ] all? ] lfilter ;
 
lcousins [ last 1000 < ] lwhile [ . ] leach</langsyntaxhighlight>
{{out}}
<pre style="height:14em">
Line 669 ⟶ 808:
 
=={{header|FOCAL}}==
<langsyntaxhighlight FOCALlang="focal">01.10 S C=0
01.20 T %4
01.30 F N=3,2,996;D 2
Line 687 ⟶ 826:
03.50 S K=K+1
03.60 G 3.2
03.70 S A=0</langsyntaxhighlight>
 
{{out}}
Line 733 ⟶ 872:
= 967= 971
AMOUNT OF COUSIN PRIME PAIRS= 41</pre>
 
=={{header|Forth}}==
{{works with|Gforth}}
<syntaxhighlight lang="forth">: prime? ( n -- ? ) here + c@ 0= ;
: not-prime! ( n -- ) here + 1 swap c! ;
 
: prime-sieve ( n -- )
here over erase
0 not-prime!
1 not-prime!
2
begin
2dup dup * >
while
dup prime? if
2dup dup * do
i not-prime!
dup +loop
then
1+
repeat
2drop ;
 
: cousin-primes ( n -- )
dup prime-sieve
0
over 4 - 0 do
i prime? if i 4 + prime? if
1+
." (" i 3 .r ." , " i 4 + 3 .r ." )"
dup 5 mod 0= if cr else space then
then then
loop
swap
cr ." Number of cousin prime pairs < " . ." is " . cr ;
 
1000 cousin-primes
bye</syntaxhighlight>
 
{{out}}
<pre>
( 3, 7) ( 7, 11) ( 13, 17) ( 19, 23) ( 37, 41)
( 43, 47) ( 67, 71) ( 79, 83) ( 97, 101) (103, 107)
(109, 113) (127, 131) (163, 167) (193, 197) (223, 227)
(229, 233) (277, 281) (307, 311) (313, 317) (349, 353)
(379, 383) (397, 401) (439, 443) (457, 461) (463, 467)
(487, 491) (499, 503) (613, 617) (643, 647) (673, 677)
(739, 743) (757, 761) (769, 773) (823, 827) (853, 857)
(859, 863) (877, 881) (883, 887) (907, 911) (937, 941)
(967, 971)
Number of cousin prime pairs < 1000 is 41
</pre>
 
=={{header|FreeBASIC}}==
Use one of the primality testing examples as an include.
 
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
dim as uinteger c=0, i
Line 745 ⟶ 936:
print using "Pair ##: #### and ####"; c; i; i+4
end if
next i</langsyntaxhighlight>
 
{{out}}
Line 794 ⟶ 985:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 840 ⟶ 1,031:
}
fmt.Printf("\n\n%d pairs found\n", count)
}</langsyntaxhighlight>
 
{{out}}
Line 856 ⟶ 1,047:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (intercalate, transpose)
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (isPrime, primes)
Line 887 ⟶ 1,078:
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</langsyntaxhighlight>
{{Out}}
<pre>41 cousin prime pairs:
Line 902 ⟶ 1,093:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j"> (":,'Amount: ',":@#) (,.[,.4+,.]) (]#~1:p:4:+]) p:i.168&.(p:inv)1000</langsyntaxhighlight>
{{out}}
<pre style="height:14em;"> 3 7
Line 946 ⟶ 1,137:
967 971
Amount: 41</pre>
 
(In this example, we can get away with finding primes where adding 4 gives us another prime. But if the task had asked for cousin prime pairs less than 100, we would want to avoid the pair 97,101. And the simplest way of addressing that issue would have been to find primes where subtracting 4 gives us another prime.)
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
For the definition of `is_prime` used here, see https://rosettacode.org/wiki/Additive_primes<syntaxhighlight lang="jq"># Output: a stream
def cousins:
# [2,6] is not a cousin so we can start at 3
range(3;.;2)
| select(is_prime and (.+4 | is_prime))
| [., .+4];
 
997 | cousins</syntaxhighlight>
{{out}}
See below.
 
'''The Count'''
 
To compute the pairs and the count at the same time without saving them as an array:<syntaxhighlight lang="jq"># Use null as the EOS marker
foreach ((997|cousins),null) as $c (-1; .+1; if $c == null then "\nCount is \(.)" else $c end)</syntaxhighlight>
{{out}}
<pre>
[3,7]
[7,11]
[13,17]
[19,23]
[37,41]
[43,47]
[67,71]
[79,83]
[97,101]
[103,107]
[109,113]
[127,131]
[163,167]
[193,197]
[223,227]
[229,233]
[277,281]
[307,311]
[313,317]
[349,353]
[379,383]
[397,401]
[439,443]
[457,461]
[463,467]
[487,491]
[499,503]
[613,617]
[643,647]
[673,677]
[739,743]
[757,761]
[769,773]
[823,827]
[853,857]
[859,863]
[877,881]
[883,887]
[907,911]
[937,941]
[967,971]
 
Count is 41
</pre>
 
=={{header|Julia}}==
{{trans|Wren}}
<langsyntaxhighlight lang="julia">using Primes
 
let
Line 963 ⟶ 1,222:
println("\n\n$pcount pairs found.")
end
</langsyntaxhighlight>{{out}}
<pre>
Cousin prime pairs under 1,000:
Line 974 ⟶ 1,233:
 
41 pairs found.
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
do -- find primes p where p+4 is also prime
local MAX_PRIME = 1000
local p = {} -- sieve the odd primes to MAX_PRIME
for i = 3, MAX_PRIME, 2 do p[ i ] = true end
for i = 3, math.floor( math.sqrt( MAX_PRIME ) ), 2 do
if p[ i ] then
for s = i * i, MAX_PRIME, i + i do p[ s ] = false end
end
end
local function fmt ( n ) return string.format( "%3d", n ) end
io.write( "Cousin primes under ", MAX_PRIME, ":\n" )
local cCount = 0
for i = 3, MAX_PRIME - 4, 2 do
if p[ i ] and p[ i + 4 ] then
cCount = cCount + 1
io.write( "[ ", fmt( i ), " ", fmt( i + 4 ), " ]"
, ( cCount % 8 == 0 and "\n" or " " )
)
end
end
io.write( "\nFound ", cCount, " cousin primes\n" )
end
</syntaxhighlight>
{{out}}
<pre>
Cousin primes under 1000:
[ 3 7 ] [ 7 11 ] [ 13 17 ] [ 19 23 ] [ 37 41 ] [ 43 47 ] [ 67 71 ] [ 79 83 ]
[ 97 101 ] [ 103 107 ] [ 109 113 ] [ 127 131 ] [ 163 167 ] [ 193 197 ] [ 223 227 ] [ 229 233 ]
[ 277 281 ] [ 307 311 ] [ 313 317 ] [ 349 353 ] [ 379 383 ] [ 397 401 ] [ 439 443 ] [ 457 461 ]
[ 463 467 ] [ 487 491 ] [ 499 503 ] [ 613 617 ] [ 643 647 ] [ 673 677 ] [ 739 743 ] [ 757 761 ]
[ 769 773 ] [ 823 827 ] [ 853 857 ] [ 859 863 ] [ 877 881 ] [ 883 887 ] [ 907 911 ] [ 937 941 ]
[ 967 971 ]
Found 41 cousin primes
</pre>
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
BOOLEAN PRIME
DIMENSION PRIME(1000)
Line 1,003 ⟶ 1,299:
VECTOR VALUES COUSIN = $I4,2H: ,I4*$
VECTOR VALUES TOTAL = $15HTOTAL COUSINS: ,I2*$
END OF PROGRAM </langsyntaxhighlight>
 
{{out}}
Line 1,049 ⟶ 1,345:
967: 971
TOTAL COUSINS: 41</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">primes = Prime@Range[PrimePi[1000] - 1];
primes = {primes, primes + 4} // Transpose;
Select[primes, AllTrue[PrimeQ]]
Length[%]</syntaxhighlight>
{{out}}
<pre>{{3,7},{7,11},{13,17},{19,23},{37,41},{43,47},{67,71},{79,83},{97,101},{103,107},{109,113},{127,131},{163,167},{193,197},{223,227},{229,233},{277,281},{307,311},{313,317},{349,353},{379,383},{397,401},{439,443},{457,461},{463,467},{487,491},{499,503},{613,617},{643,647},{673,677},{739,743},{757,761},{769,773},{823,827},{853,857},{859,863},{877,881},{883,887},{907,911},{937,941},{967,971}}
41</pre>
 
=={{header|Nim}}==
We use a simple primality test (which is in fact executed at compile time). For large values of N, it would be better to use a sieve of Erathostenes and to replace the constants “PrimeList” and “PrimeSet” by read-only variables.
<langsyntaxhighlight Nimlang="nim">import sets, strutils, sugar
 
const N = 1000
Line 1,078 ⟶ 1,383:
stdout.write ($cousins).center(10)
stdout.write if (i+1) mod 7 == 0: '\n' else: ' '
echo()</langsyntaxhighlight>
 
{{out}}
Line 1,092 ⟶ 1,397:
{{works with|Free Pascal}}
{{works with|Delphi}}Sieving only odd numbers.
<langsyntaxhighlight lang="pascal">program Cousin_primes;
//Free Pascal Compiler version 3.2.1 [2020/11/03] for x86_64fpc
{$IFDEF FPC}
Line 1,202 ⟶ 1,507:
setlength(primes,0);
END.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,225 ⟶ 1,530:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use warnings;
use feature 'say';
use ntheory 'is_prime';
Line 1,231 ⟶ 1,536:
my($limit, @cp) = 1000;
is_prime($_) and is_prime($_+4) and push @cp, "$_/@{[$_+4]}" for 2..$limit;
say @cp . " cousin prime pairs < $limit:\n" . (sprintf "@{['%8s' x @cp]}", @cp) =~ s/(.{56})/$1\n/gr;</langsyntaxhighlight>
{{out}}
<pre>41 cousin prime pairs < 1000:
Line 1,242 ⟶ 1,547:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">has_cousin</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">+</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">7</span> <span style="color: #008080;">do</span>
Line 1,250 ⟶ 1,555:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%,d cousin prime pairs less than %,d found: %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #000000;">tn</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)))})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
<small>(Uses tn-9 instead of the more obvious tn-4 since none of 96,95,94,93,92 or similar with 9..99999 prefix could ever be prime. Note that {97,101} is deliberately excluded from < 100.)</small>
{{out}}
Line 1,263 ⟶ 1,568:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''Cousin primes'''
 
from itertools import chain, takewhile
Line 1,388 ⟶ 1,693:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>41 cousin pairs below 1000:
Line 1,403 ⟶ 1,708:
(877, 881) (883, 887) (907, 911) (937, 941)
(967, 971)</pre>
 
=={{header|Quackery}}==
 
<code>eratosthenes</code> and <code>isprime</code> are defined at [[Sieve of Eratosthenes#Quackery]].
 
<syntaxhighlight lang="Quackery"> 1000 eratosthenes
[] 1000 4 - times
[ i^ isprime
i^ 4 + isprime
and if
[ i^ dup 4 + join
nested join ] ]
dup echo cr cr
size echo
</syntaxhighlight>
 
{{out}}
 
<pre>[ [ 3 7 ] [ 7 11 ] [ 13 17 ] [ 19 23 ] [ 37 41 ] [ 43 47 ] [ 67 71 ] [ 79 83 ] [ 97 101 ] [ 103 107 ] [ 109 113 ] [ 127 131 ] [ 163 167 ] [ 193 197 ] [ 223 227 ] [ 229 233 ] [ 277 281 ] [ 307 311 ] [ 313 317 ] [ 349 353 ] [ 379 383 ] [ 397 401 ] [ 439 443 ] [ 457 461 ] [ 463 467 ] [ 487 491 ] [ 499 503 ] [ 613 617 ] [ 643 647 ] [ 673 677 ] [ 739 743 ] [ 757 761 ] [ 769 773 ] [ 823 827 ] [ 853 857 ] [ 859 863 ] [ 877 881 ] [ 883 887 ] [ 907 911 ] [ 937 941 ] [ 967 971 ] ]
 
41
</pre>
 
=={{header|REXX}}==
This REXX version allows the limit to be specified, &nbsp; as well as the number of cousin prime pairs to be shown per line.
<langsyntaxhighlight lang="rexx">/*REXX program counts/shows the number of cousin prime pairs under a specified number N.*/
parse arg hi cols . /*get optional number of primes to find*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then assume default.*/
Line 1,442 ⟶ 1,770:
#= # + 1; @.#= j; !.j= 1 /*bump prime count; assign prime & flag*/
end /*j*/
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,458 ⟶ 1,786:
===Filter===
Favoring brevity over efficiency due to the small range of n, the most concise solution is:
<syntaxhighlight lang="raku" perl6line>say grep *.all.is-prime, map { $_, $_+4 }, 2..999;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,466 ⟶ 1,794:
A more efficient and versatile approach is to generate an infinite list of cousin primes, using this info from https://oeis.org/A023200 :
:Apart from the first term, all terms are of the form 6n + 1.
<syntaxhighlight lang="raku" perl6line>constant @cousins = (3, 7, *+6 … *).map: -> \n { (n, n+4) if (n & n+4).is-prime };
 
my $count = @cousins.first: :k, *.[0] > 1000;
 
.say for @cousins.head($count).batch(9);</langsyntaxhighlight>
{{out}}
<pre>
Line 1,480 ⟶ 1,808:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,514 ⟶ 1,842:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,531 ⟶ 1,859:
found 81 unique cousin primes.
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
≪ { } → cousins
≪ 2 3 5
'''DO'''
ROT DROP DUP NEXTPRIME
'''CASE'''
DUP 4 PICK - 4 == '''THEN''' PICK3 OVER R→C 'cousins' SWAP STO+ '''END'''
DUP2 - -4 == '''THEN''' DUP2 R→C 'cousins' SWAP STO+ '''END'''
'''END'''
'''UNTIL''' DUP 1000 ≥ '''END'''
3 DROPN
cousins DUP SIZE
≫ ≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
2: { (3., 7.) (7., 11.) (13., 17.) (19., 23.) (37., 41.) (43., 47.) (67., 71.) (79., 83.) (97., 101.) (103., 107.) (109., 113.) (127., 131.) (163., 167.) (193., 197.) (223., 227.) (229., 233.) (277., 281.) (307., 311.) (313., 317.) (349., 353.) (379., 383.) (397., 401.) (439., 443.) (457., 461.) (463., 467.) (487., 491.) (499., 503.) (613., 617.) (643., 647.) (673., 677.) (739., 743.) (757., 761.) (769., 773.) (823., 827.) (853., 857.) (859., 863.) (877., 881.) (883., 887.) (907., 911.) (937., 941.) (967., 971.) }
1: 41
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
primes = Prime.each(1000).to_a
p cousins = primes.filter_map{|pr| [pr, pr+4] if primes.include?(pr+4) }
puts "#{cousins.size} cousins found."
</syntaxhighlight>
{{out}}
<pre>[[3, 7], [7, 11], [13, 17], [19, 23], [37, 41], [43, 47], [67, 71], [79, 83], [97, 101], [103, 107], [109, 113], [127, 131], [163, 167], [193, 197], [223, 227], [229, 233], [277, 281], [307, 311], [313, 317], [349, 353], [379, 383], [397, 401], [439, 443], [457, 461], [463, 467], [487, 491], [499, 503], [613, 617], [643, 647], [673, 677], [739, 743], [757, 761], [769, 773], [823, 827], [853, 857], [859, 863], [877, 881], [883, 887], [907, 911], [937, 941], [967, 971]]
41 cousins found.
</pre>
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func boolean: isPrime (in integer: number) is func
result
var boolean: prime is FALSE;
local
var integer: upTo is 0;
var integer: testNum is 3;
begin
if number = 2 then
prime := TRUE;
elsif odd(number) and number > 2 then
upTo := sqrt(number);
while number rem testNum <> 0 and testNum <= upTo do
testNum +:= 2;
end while;
prime := testNum > upTo;
end if;
end func;
 
const proc: main is func
local
var integer: n is 0;
var integer: count is 0;
begin
for n range 7 to 999 step 2 do
if isPrime(n) and isPrime(n - 4) then
writeln(n - 4 lpad 3 <& ", " <& n lpad 3);
incr(count);
end if;
end for;
writeln("\n" <& count <& " cousin prime pairs found < 1000.");
end func;</syntaxhighlight>
{{out}}
<pre style="height:14em">
3, 7
7, 11
13, 17
19, 23
37, 41
43, 47
67, 71
79, 83
97, 101
103, 107
109, 113
127, 131
163, 167
193, 197
223, 227
229, 233
277, 281
307, 311
313, 317
349, 353
379, 383
397, 401
439, 443
457, 461
463, 467
487, 491
499, 503
613, 617
643, 647
673, 677
739, 743
757, 761
769, 773
823, 827
853, 857
859, 863
877, 881
883, 887
907, 911
937, 941
967, 971
 
41 cousin prime pairs found < 1000.
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var limit = 1000
var pairs = (limit-5).primes.map { [_, _+4] }.grep { .tail.is_prime }
 
say "Cousin prime pairs whose elements are less than #{limit.commify}:"
say pairs
say "\n#{pairs.len} pairs found"</syntaxhighlight>
{{out}}
<pre>
Cousin prime pairs whose elements are less than 1,000:
[[3, 7], [7, 11], [13, 17], [19, 23], [37, 41], [43, 47], [67, 71], [79, 83], [97, 101], [103, 107], [109, 113], [127, 131], [163, 167], [193, 197], [223, 227], [229, 233], [277, 281], [307, 311], [313, 317], [349, 353], [379, 383], [397, 401], [439, 443], [457, 461], [463, 467], [487, 491], [499, 503], [613, 617], [643, 647], [673, 677], [739, 743], [757, 761], [769, 773], [823, 827], [853, 857], [859, 863], [877, 881], [883, 887], [907, 911], [937, 941], [967, 971]]
 
41 pairs found
</pre>
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">import Foundation
 
func primeSieve(limit: Int) -> [Bool] {
guard limit > 0 else {
return []
}
var sieve = Array(repeating: true, count: limit)
sieve[0] = false
if limit > 1 {
sieve[1] = false
}
if limit > 4 {
for i in stride(from: 4, to: limit, by: 2) {
sieve[i] = false
}
}
var p = 3
var sq = p * p
while sq < limit {
if sieve[p] {
for i in stride(from: sq, to: limit, by: p * 2) {
sieve[i] = false
}
}
sq += (p + 1) * 4;
p += 2
}
return sieve
}
 
func toString(_ number: Int) -> String {
return String(format: "%3d", number)
}
 
let limit = 1000
let sieve = primeSieve(limit: limit)
var count = 0
for p in 0..<limit - 4 {
if sieve[p] && sieve[p + 4] {
print("(\(toString(p)), \(toString(p + 4)))", terminator: "")
count += 1
print(count % 5 == 0 ? "\n" : " ", terminator: "")
}
}
print("\nNumber of cousin prime pairs < \(limit): \(count)")</syntaxhighlight>
 
{{out}}
<pre>
( 3, 7) ( 7, 11) ( 13, 17) ( 19, 23) ( 37, 41)
( 43, 47) ( 67, 71) ( 79, 83) ( 97, 101) (103, 107)
(109, 113) (127, 131) (163, 167) (193, 197) (223, 227)
(229, 233) (277, 281) (307, 311) (313, 317) (349, 353)
(379, 383) (397, 401) (439, 443) (457, 461) (463, 467)
(487, 491) (499, 503) (613, 617) (643, 647) (673, 677)
(739, 743) (757, 761) (769, 773) (823, 827) (853, 857)
(859, 863) (877, 881) (883, 887) (907, 911) (937, 941)
(967, 971)
Number of cousin prime pairs < 1000: 41
</pre>
 
Line 1,536 ⟶ 2,052:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./fmt" for Fmt
 
var c = Int.primeSieve(999, false)
Line 1,552 ⟶ 2,068:
i = i + 2
}
System.print("\n\n%(count) pairs found")</langsyntaxhighlight>
 
{{out}}
Line 1,565 ⟶ 2,081:
 
41 pairs found
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">include xpllib; \For IsPrime and Print
int N, C;
[C:= 0;
for N:= 2 to 1000-1-4 do
[if IsPrime(N) then
if IsPrime(N+4) then
[Print("(%3.0f, %3.0f) ", float(N), float(N+4));
C:= C+1;
if rem(C/6) = 0 then CrLf(0);
];
];
Print("\nThere are %d cousin primes less than 1000.\n", C);
]</syntaxhighlight>
{{out}}
<pre>
( 3, 7) ( 7, 11) ( 13, 17) ( 19, 23) ( 37, 41) ( 43, 47)
( 67, 71) ( 79, 83) ( 97, 101) (103, 107) (109, 113) (127, 131)
(163, 167) (193, 197) (223, 227) (229, 233) (277, 281) (307, 311)
(313, 317) (349, 353) (379, 383) (397, 401) (439, 443) (457, 461)
(463, 467) (487, 491) (499, 503) (613, 617) (643, 647) (673, 677)
(739, 743) (757, 761) (769, 773) (823, 827) (853, 857) (859, 863)
(877, 881) (883, 887) (907, 911) (937, 941) (967, 971)
There are 41 cousin primes less than 1000.
</pre>
9,476

edits