Cousin primes

From Rosetta Code
Cousin primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Definitions

In mathematics, cousin primes are prime numbers that differ by four.

For the purposes of this task a cousin prime pair is a pair of non-negative integers of the form [n, n + 4] whose elements are both primes.


Task

Write a program to determine (and show here) all cousin prime pairs whose elements are both less than 1,000.

Optionally, show the number of such pairs.


Also see



11l

Translation of: Nim
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()
Output:
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) 

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
Output:

Screenshot from Atari 8-bit computer

(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

Ada

with Ada.Text_Io;

procedure Cousin_Primes is

   type Number is new Long_Integer range 0 .. Long_Integer'Last;
   package Number_Io is new Ada.Text_Io.Integer_Io (Number);

   function Is_Prime (A : Number) return Boolean is
      D : Number;
   begin
      if A < 2       then return False; end if;
      if A in 2 .. 3 then return True;  end if;
      if A mod 2 = 0 then return False; end if;
      if A mod 3 = 0 then return False; end if;
      D := 5;
      while D * D <= A loop
         if A mod D = 0 then
            return False;
         end if;
         D := D + 2;
         if A mod D = 0 then
            return False;
         end if;
         D := D + 4;
      end loop;
      return True;
   end Is_Prime;

   use Ada.Text_Io;
   Count : Natural := 0;
begin
   for N in Number range 1 .. 999 - 4 loop
      if Is_Prime (N) and then Is_Prime (N + 4) then
         Count := Count + 1;
         Put("[");
         Number_Io.Put (N, Width => 3); Put (",");
         Number_Io.Put (N + 4, Width => 3);
         Put("]  ");
         if Count mod 8 = 0 then
            New_Line;
         end if;
      end if;
   end loop;
   New_Line;
   Put_Line (Count'Image & " pairs.");
end Cousin_Primes;
Output:
[  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.

ALGOL 68

BEGIN # find cousin primes - pairs of primes that differ by 4 #
    # sieve the primes as required by the task #
    PR read "primes.incl.a68" PR
    []BOOL prime = PRIMESIEVE 1000;
    # 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 UPB prime - 4 DO
        IF prime[ i ] THEN
            IF prime[ i + 4 ] THEN
                # have a pair of cousin primes #
                p count +:= 1;
                print( ( whole( i, -5 ), "-", right pad( whole( i + 4, 0 ), 5 ) ) );
                IF p count MOD 10 = 0 THEN print( ( newline ) ) FI
            FI
        FI
    OD;
    print( ( newline, "Found ", whole( p count, 0 ), " cousin primes", newline ) )
END
Output:
    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

ALGOL W

begin % find some cousin primes: primes p where p + 4 is also a prime %
    integer MAX_PRIME;
    MAX_PRIME := 1000;
    begin
        logical array prime( 1 :: MAX_PRIME );
        integer       cCount;
        % sieve the primes to MAX_PRIME %
        prime( 1 ) := false; prime( 2 ) := true;
        for i := 3 step 2 until MAX_PRIME do prime( i ) := true;
        for i := 4 step 2 until MAX_PRIME do prime( i ) := false;
        for i := 3 step 2 until truncate( sqrt( MAX_PRIME ) ) do begin
            integer ii; ii := i + i;
            if prime( i ) then for np := i * i step ii until MAX_PRIME do prime( np ) := false
        end for_i ;
        % find the cousin primes %
        cCount := 0;
        % two is not a cousin prime so we can start at 3 %
        for i := 3 step 2 until MAX_PRIME - 4 do begin
            if prime( i ) and prime( i + 4 ) then begin
                % have another cousin prime pair %
                writeon( i_w := 1, s_w := 0, " (", i, " ", i + 4, ")" );
                cCount := cCount + 1;
                if cCount rem 10 = 0 then write()
            end if_have_a_cousin_prime_pair
        end for_i ;
        write( i_w := 1, s_w := 0, "Found ", cCount, " cousin prime pairs up to ", MAX_PRIME )
    end
end.
Output:
 (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 prime pairs up to 1000

APL

('Amount:',⊃⍴P)P,4+P((P+4)P)/P(~PP∘.×P)/P1↓⍳1000
Output:
Amount: 41
  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

AppleScript

on sieveOfEratosthenes(limit)
    script o
        property numberList : {missing value}
    end script
    
    repeat with n from 2 to limit
        set end of o's numberList to n
    end repeat
    repeat with n from 2 to (limit ^ 0.5 div 1)
        if (item n of o's numberList is n) then
            repeat with multiple from (n * n) to limit by n
                set item multiple of o's numberList to missing value
            end repeat
        end if
    end repeat
    
    return o's numberList's numbers
end sieveOfEratosthenes

local primes, output, p
set primes to sieveOfEratosthenes(999)
set output to {}
repeat with p in primes
    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)}
Output:
{|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}

Arturo

cousins: function [upto][
    primesUpto: select 0..upto => prime?
    return select primesUpto => [prime? & + 4]
]

print map cousins 1000 'c -> @[c, c + 4]
Output:
[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]

AWK

# syntax: GAWK -f COUSIN_PRIMES.AWK
BEGIN {
    start = 1
    stop = 1000
    for (i=start; i<=stop; i++) {
      if (is_prime(i) && is_prime(i+4)) {
        printf("%3d:%3d%1s",i,i+4,++count%10?"":"\n")
      }
    }
    printf("\nCousin primes %d-%d: %d\n",start,stop,count)
    exit(0)
}
function is_prime(x,  i) {
    if (x <= 1) {
      return(0)
    }
    for (i=2; i<=int(sqrt(x)); i++) {
      if (x % i == 0) {
        return(0)
      }
    }
    return(1)
}
Output:
  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
Cousin primes 1-1000: 41

BASIC

10 DEFINT A-Z: L=1000: DIM S(L)
20 FOR P=2 TO SQR(L)
30 IF S(P) THEN 50
40 FOR K=P*P TO L STEP P: S(K)=1: NEXT
50 NEXT
60 N=0
70 FOR P=2 TO L-4
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
Output:
 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 prime pairs below 1000

BCPL

get "libhdr"

manifest $( LIMIT = 1000 $)

let sieve(prime,max) be
$(  let i = 2
    0!prime := false
    1!prime := false
    for i = 2 to max do i!prime := true
    while i*i <= max do
    $(  if i!prime do
        $(  let j = i*i
            while j <= max do
            $(  j!prime := false
                j := j + i
            $)
        $)
        i := i + 1
    $)
$)

let start() be
$(  let prime = vec LIMIT
    let count = 0
    sieve(prime, LIMIT)
    for i = 2 to LIMIT-4 do
        if i!prime & (i+4)!prime do
        $(  count := count + 1
            writef("%N, %N*N", i, i+4)
        $)
    writef("*N%N pairs found.*N", count)
$)
Output:
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.

C

#include <stdio.h>
#include <string.h>

#define LIMIT 1000

void sieve(int max, char *s) {
    int p, k;
    memset(s, 0, max);
    for (p=2; p*p<=max; p++)
        if (!s[p]) 
            for (k=p*p; k<=max; k+=p) 
                s[k]=1;
}

int main(void) {
    char primes[LIMIT+1];
    int p, count=0;
    
    sieve(LIMIT, primes);
    for (p=2; p<=LIMIT; p++) {
        if (!primes[p] && !primes[p+4]) {
            count++;
            printf("%4d: %4d\n", p, p+4);
        }
    }
    
    printf("There are %d cousin prime pairs below %d.\n", count, LIMIT);
    return 0;
}
Output:
   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 prime pairs below 1000.

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.
Output:
  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.

Cowgol

include "cowgol.coh";

const LIMIT := 1000;
var sieve: uint8[LIMIT + 1];
MemZero(&sieve[0], @bytesof sieve);

var p: @indexof sieve := 2;

loop
    var n := p*p;
    if n >= LIMIT then break; end if;
    if sieve[p] == 0 then
        while n < LIMIT loop
            sieve[n] := 1;
            n := n + p;
        end loop;
    end if;
    p := p + 1;
end loop;

var count: uint8 := 0;
n := 2;
while n < LIMIT-4 loop
    if sieve[n] + sieve[n+4] == 0 then
        count := count + 1;
        print_i32(n as uint32);
        print_char('\t');
        print_i32(n as uint32+4);
        print_nl();
    end if;
    n := n + 1;
end loop;

print("There are ");
print_i8(count);
print(" cousin prime pairs below ");
print_i16(LIMIT);
print_nl();
Output:
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 prime pairs below 1000

F#

This task uses Extensible Prime Generator (F#)

// 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 ""
Output:
(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) (2http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]77,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)

Factor

Works with: Factor version 0.99 2021-02-05
USING: kernel lists lists.lazy math math.primes prettyprint
sequences ;

: lcousins ( -- list )
    L{ { 3 7 } } 7 11 [ [ 6 + ] lfrom-by ] bi@ lzip lappend-lazy
    [ [ prime? ] all? ] lfilter ;

lcousins [ last 1000 < ] lwhile [ . ] leach
Output:
{ 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 }


FOCAL

01.10 S C=0
01.20 T %4
01.30 F N=3,2,996;D 2
01.40 T "AMOUNT OF COUSIN PRIME PAIRS",C,!
01.50 Q

02.10 S P=N;D 3;S D=A
02.20 S P=N+4;D 3
02.30 I (-A*D)2.4;R
02.40 T N,P,!
02.50 S C=C+1

03.10 S K=2
03.20 I (K-P)3.3;S A=-1;R
03.30 S B=P/K
03.40 I (FITR(B)-B)3.5,3.7,3.5
03.50 S K=K+1
03.60 G 3.2
03.70 S A=0
Output:
=    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
AMOUNT OF COUSIN PRIME PAIRS=   41

Forth

Works with: Gforth
: 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
Output:
(  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 

FreeBASIC

Use one of the primality testing examples as an include.

#include "isprime.bas"

dim as uinteger c=0, i
for i = 3 to 995
    if isprime(i+4) andalso isprime(i) then
        c += 1
        print using "Pair ##: #### and ####"; c; i; i+4
    end if
next i
Output:
Pair  1:    3 and    7
Pair  2:    7 and   11
Pair  3:   13 and   17
Pair  4:   19 and   23
Pair  5:   37 and   41
Pair  6:   43 and   47
Pair  7:   67 and   71
Pair  8:   79 and   83
Pair  9:   97 and  101
Pair 10:  103 and  107
Pair 11:  109 and  113
Pair 12:  127 and  131
Pair 13:  163 and  167
Pair 14:  193 and  197
Pair 15:  223 and  227
Pair 16:  229 and  233
Pair 17:  277 and  281
Pair 18:  307 and  311
Pair 19:  313 and  317
Pair 20:  349 and  353
Pair 21:  379 and  383
Pair 22:  397 and  401
Pair 23:  439 and  443
Pair 24:  457 and  461
Pair 25:  463 and  467
Pair 26:  487 and  491
Pair 27:  499 and  503
Pair 28:  613 and  617
Pair 29:  643 and  647
Pair 30:  673 and  677
Pair 31:  739 and  743
Pair 32:  757 and  761
Pair 33:  769 and  773
Pair 34:  823 and  827
Pair 35:  853 and  857
Pair 36:  859 and  863
Pair 37:  877 and  881
Pair 38:  883 and  887
Pair 39:  907 and  911
Pair 40:  937 and  941
Pair 41:  967 and  971

Go

Translation of: Wren
package main

import "fmt"

func isPrime(n int) bool {
    switch {
    case n < 2:
        return false
    case n%2 == 0:
        return n == 2
    case n%3 == 0:
        return n == 3
    default:
        d := 5
        for d*d <= n {
            if n%d == 0 {
                return false
            }
            d += 2
            if n%d == 0 {
                return false
            }
            d += 4
        }
        return true
    }
}

func main() {
    count := 0
    fmt.Println("Cousin prime pairs whose elements are less than 1,000:")
    for i := 3; i <= 995; i += 2 {
        if isPrime(i) && isPrime(i+4) {
            fmt.Printf("%3d:%3d  ", i, i+4)
            count++
            if count%7 == 0 {
                fmt.Println()
            }
            if i != 3 {
                i += 4
            } else {
                i += 2
            }
        }
    }
    fmt.Printf("\n\n%d pairs found\n", count)
}
Output:
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

Haskell

import Data.List (intercalate, transpose)
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (isPrime, primes)
import Text.Printf (printf)

---------------------- COUSIN PRIMES ---------------------

cousinPrimes :: [(Integer, Integer)]
cousinPrimes = concat $ (zipWith go <*> fmap (+ 4)) primes
  where
    go a b = [(a, b) | isPrime b]


--------------------------- TEST -------------------------
main :: IO ()
main = do
  let cousins = takeWhile ((< 1000) . snd) cousinPrimes
  mapM_
    putStrLn
    [ (show . length) cousins <> " cousin prime pairs:",
      "",
      table "   " $
        chunksOf 5 $ show <$> cousins
    ]

------------------------ FORMATTING ----------------------

table :: String -> [[String]] -> String
table gap rows =
  let ws = maximum . fmap length <$> transpose rows
      pw = printf . flip intercalate ["%", "s"] . show
   in unlines $ intercalate gap . zipWith pw ws <$> rows
Output:
41 cousin prime pairs:

    (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)

J

   (":,'Amount: ',":@#) ([,.4+]) (]#~1:p:4:+]) i.&.(p:inv)1000
Output:
  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
Amount: 41

(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.)

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

# 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
Output:

See below.

The Count

To compute the pairs and the count at the same time without saving them as an array:

# Use null as the EOS marker
foreach ((997|cousins),null) as $c (-1; .+1; if $c == null then "\nCount is \(.)" else $c end)
Output:
[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

Julia

Translation of: Wren
using Primes

let
    p = primesmask(1000)
    println("Cousin prime pairs under 1,000:")
    pcount = 0
    for i in 2:996
        if p[i] && p[i + 4]
            pcount += 1
            print(lpad(i, 4), ":", rpad(i + 4, 4), pcount % 8 == 0 ? "\n" : "")
        end
    end
    println("\n\n$pcount pairs found.")
end
Output:
Cousin prime pairs under 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.

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
Output:
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

MAD

            NORMAL MODE IS INTEGER
            BOOLEAN PRIME
            DIMENSION PRIME(1000)
            
            THROUGH SET, FOR P=2, 1, P.G.1000
SET         PRIME(P) = 1B

            THROUGH SIEVE, FOR P=2, 1, P*P.G.1000
            WHENEVER PRIME(P)
                THROUGH MARK, FOR K=P*P, P, K.G.1000
MARK            PRIME(K) = 0B
            END OF CONDITIONAL
SIEVE       CONTINUE

            COUNT = 0
            THROUGH TEST, FOR P=2, 1, P.G.1000-4
            WHENEVER PRIME(P) .AND. PRIME(P+4)
                COUNT = COUNT + 1
                PRINT FORMAT COUSIN, P, P+4
            END OF CONDITIONAL
TEST        CONTINUE

            PRINT FORMAT TOTAL, COUNT
            
            VECTOR VALUES COUSIN = $I4,2H: ,I4*$
            VECTOR VALUES TOTAL = $15HTOTAL COUSINS: ,I2*$
            END OF PROGRAM
Output:
   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
TOTAL COUSINS: 41

Mathematica/Wolfram Language

primes = Prime@Range[PrimePi[1000] - 1];
primes = {primes, primes + 4} // Transpose;
Select[primes, AllTrue[PrimeQ]]
Length[%]
Output:
{{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

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.

import sets, strutils, sugar

const N = 1000

func isPrime(n: Positive): bool {.compileTime.} =
  if (n and 1) == 0: return n == 2
  var m = 3
  while m * m <= n:
    if n mod m == 0: return false
    inc m, 2
  result = true

const
  PrimeList = collect(newSeq):
                for n in 2..N:
                  if n.isPrime: n
  PrimeSet = PrimeList.toHashSet

let cousinList = collect(newSeq):
                   for n in PrimeList:
                     if (n + 4) in PrimeSet: (n, n + 4)

echo "Found $# cousin primes less than $#:".format(cousinList.len, N)
for i, cousins in cousinList:
  stdout.write ($cousins).center(10)
  stdout.write if (i+1) mod 7 == 0: '\n' else: ' '
echo()
Output:
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) 

Pascal

Works with: Free Pascal
Works with: Delphi

Sieving only odd numbers.

program Cousin_primes;
//Free Pascal Compiler version 3.2.1 [2020/11/03] for x86_64fpc
{$IFDEF FPC}
  {$MODE DELPHI}
  {$Optimization ON,ALL}
{$ELSE}
  {$APPTYPE CONSOLE}
{$ENDIF}    
const
  MAXNUMBER = 100*1000*1000;// > 3
  MAXLIMIT = (MAXNUMBER-1) DIV 2;

type
  tChkprimes = array of byte;//prime == 1 , nonprime == 0
  tPrimes = array of Uint32;

var
  primes :tPrimes; //here starting with 3
procedure OutCount(lmt,cnt:NativeInt);
Begin
  writeln(cnt,' cousin primes up to ',lmt);
end;

procedure InitPrimes;
var
  Chkprimes:tChkprimes;
//NativeUInt i DIV 2 is only SHR 1,otherwise extension to Int64 
  i,j,CountOfPrimes : NativeUInt;
begin
  SetLength(Chkprimes,MAXLIMIT+1);
  fillchar(Chkprimes[0],length(Chkprimes),#1);
  //estimate count of primes
  CountOfPrimes := trunc(MAXNUMBER/(ln(MAXNUMBER)-1.08))+100;
  SetLength(primes,CountOfPrimes+1);
  
  //sieve of eratosthenes only odd numbers
  // i = 2*j+1
  Chkprimes[0] := 0;// 0 -> 2*0+1 = 1
  i := 1;
  repeat
    if Chkprimes[(i-1) DIV 2] <> 0 then
    Begin
      // convert i*i into j
      j := (i*i-1) DIV 2;
      if j> MAXLIMIT then
        break;
      repeat
        Chkprimes[j]:= 0;
        inc(j,i);
      until j> MAXLIMIT;
    end;
    inc(i,2);
  until false;
 
  j := 0;
  For i := 1 to MAXLIMIT do
    IF Chkprimes[i]<>0 then
    Begin
      primes[j] := 2*i+1;
      inc(j);  
      if j>CountOfPrimes then
      Begin
        CountOfPrimes += 400;      
        setlength(Primes,CountOfPrimes);
      end;  
    end;
  setlength(primes,j);
  
  setlength(Chkprimes,0);  
end;

var
  i,lmt,cnt,primeCount : NativeInt;
BEGIN
  InitPrimes;
  //only exception, that the index difference is greater 1
  write(primes[0]:3,':',primes[2]:3,' ');
  cnt := 1;
  lmt := 1000;  
  For i := 1 to High(primes) do
  Begin
    if primes[i] >lmt then
      break;  
    IF primes[i]-primes[i-1] = 4 then
    Begin
      write(primes[i-1]:3,':',primes[i]:3,' ');
      inc(cnt);
      If cnt MOD 6 = 0 then
        writeln;
    end;
  end;  
  writeln;
  OutCount(lmt,cnt);
    
  writeln;
  cnt := 1;  
  lmt *= 10;
  primeCount := High(primes);
  For i := 1 to primeCount do
  Begin
    if primes[i] >lmt then
    Begin
      OutCount(lmt,cnt);
      lmt *= 10;
    end;
    inc(cnt,ORD(primes[i]-primes[i-1] = 4));
  end;
  OutCount(MAXNUMBER,cnt);  
  
  setlength(primes,0);  
END.
Output:
  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 primes up to 1000

203 cousin primes up to 10000
1216 cousin primes up to 100000
8144 cousin primes up to 1000000
58622 cousin primes up to 10000000
440258 cousin primes up to 100000000

real    0m0,484s

Perl

Library: ntheory
use warnings;
use feature 'say';
use ntheory 'is_prime';

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;
Output:
41 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

Phix

function has_cousin(integer p) return is_prime(p+4) end function
for n=2 to 7 do
    integer tn = power(10,n)
    sequence res = filter(get_primes_le(tn-9),has_cousin)
    res = columnize({res,sq_add(res,4)})
    printf(1,"%,d cousin prime pairs less than %,d found: %v\n",{length(res),tn,shorten(res,"",min(4,5-floor(n/2)))})
end for

(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.)

Output:
8 cousin prime pairs less than 100 found: {{3,7},{7,11},{13,17},{19,23},{37,41},{43,47},{67,71},{79,83}}
41 cousin prime pairs less than 1,000 found: {{3,7},{7,11},{13,17},{19,23},"...",{883,887},{907,911},{937,941},{967,971}}
203 cousin prime pairs less than 10,000 found: {{3,7},{7,11},{13,17},"...",{9787,9791},{9829,9833},{9883,9887}}
1,216 cousin prime pairs less than 100,000 found: {{3,7},{7,11},{13,17},"...",{99709,99713},{99829,99833},{99877,99881}}
8,144 cousin prime pairs less than 1,000,000 found: {{3,7},{7,11},"...",{999769,999773},{999979,999983}}
58,622 cousin prime pairs less than 10,000,000 found: {{3,7},{7,11},"...",{9999217,9999221},{9999397,9999401}}

Python

'''Cousin primes'''

from itertools import chain, takewhile


# cousinPrimes :: [Int]
def cousinPrimes():
    '''Non finite list of pairs of primes which differ by 4.
    '''
    def go(x):
        n = 4 + x
        return [(x, n)] if isPrime(n) else []

    return chain.from_iterable(
        map(go, primes())
    )


# ------------------------- TEST -------------------------
# main :: IO ()
def main():
    '''Cousin pairs where each value is below 1000'''

    pairs = list(
        takewhile(
            lambda ab: 1000 > ab[1],
            cousinPrimes()
        )
    )

    print(f'{len(pairs)} cousin pairs below 1000:\n')
    print(
        spacedTable(list(
            chunksOf(4)([
                repr(x) for x in pairs
            ])
        ))
    )


# ----------------------- GENERIC ------------------------

# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
    '''A series of lists of length n, subdividing the
       contents of xs. Where the length of xs is not evenly
       divible, the final list will be shorter than n.
    '''
    def go(xs):
        return (
            xs[i:n + i] for i in range(0, len(xs), n)
        ) if 0 < n else None
    return go


# isPrime :: Int -> Bool
def isPrime(n):
    '''True if n is prime.'''
    if n in (2, 3):
        return True
    if 2 > n or 0 == n % 2:
        return False
    if 9 > n:
        return True
    if 0 == n % 3:
        return False

    def p(x):
        return 0 == n % x or 0 == n % (2 + x)

    return not any(map(p, range(5, 1 + int(n ** 0.5), 6)))


# primes :: [Int]
def primes():
    ''' Non finite sequence of prime numbers.
    '''
    n = 2
    dct = {}
    while True:
        if n in dct:
            for p in dct[n]:
                dct.setdefault(n + p, []).append(p)
            del dct[n]
        else:
            yield n
            dct[n * n] = [n]
        n = 1 + n


# listTranspose :: [[a]] -> [[a]]
def listTranspose(xss):
    '''Transposition of a list of lists
    '''
    def go(xss):
        if xss:
            h, *t = xss
            return (
                [[h[0]] + [xs[0] for xs in t if xs]] + (
                    go([h[1:]] + [xs[1:] for xs in t])
                )
            ) if h and isinstance(h, list) else go(t)
        else:
            return []
    return go(xss)


# spacedTable :: [[String]] -> String
def spacedTable(rows):
    '''Tabulation with right-aligned cells'''
    columnWidths = [
        len(str(row[-1])) for row in listTranspose(rows)
    ]
    return '\n'.join([
        ' '.join(
            map(
                lambda w, s: s.rjust(w, ' '),
                columnWidths, row
            )
        ) for row in rows
    ])


# MAIN ---
if __name__ == '__main__':
    main()
Output:
41 cousin pairs below 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)

Quackery

eratosthenes and isprime are defined at Sieve of Eratosthenes#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
Output:
[ [ 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

REXX

This REXX version allows the limit to be specified,   as well as the number of cousin prime pairs to be shown per line.

/*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.*/
if cols=='' | cols==","  then cols=   10         /* "      "          "     "       "  .*/
Ocols= cols;                  cols= abs(cols)    /*Use the absolute value of cols.      */
call genP hi-1                                   /*generate all primes under  N.        */
pairs= 0;   dups= 0                              /*initialize # cousin prime pairs; dups*/
$=                                               /*a list of cousin prime pairs (so far)*/
       do j=1  while @.j\==.;  c= @.j - 4        /*lets hunt for cousin prime pairs.    */
       if \!.c  then iterate                     /*Not a lowe cousin pair? Then skip it.*/
       pairs= pairs + 1                          /*bump the count of cousin prime pairs.*/
       if @.j==11          then dups= dups + 1   /*take care to note if there is a dup. */
       if Ocols<1          then iterate          /*Build the list  (to be shown later)? */
       $= $ ' ('@.j-4","@.j')'                   /*add the cousin pair to the  $  list. */
       if pairs//cols\==0  then iterate          /*have we populated a line of output?  */
       say strip($);            $=               /*display what we have so far  (cols). */
       end   /*j*/

if $\==''  then say strip($)                     /*possible display some residual output*/
say
say 'found '     pairs            " cousin prime pairs."
say 'found '     pairs*2-dups     " unique cousin primes."
exit 0                                           /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: parse arg n;  @.=.; @.1=2;  @.2=3;  @.3=5;  @.4=7;  @.5=11;  @.6=13;  @.7=17;   #= 7
                    !.=0; !.2=1;  !.3=1;  !.5=1;  !.7=1;  !.11=1;  !.13=1;  !.17=1
            do j=@.7+2  by 2  while j<=hi        /*continue on with the next odd prime. */
            parse var  j  ''  -1  _              /*obtain the last digit of the  J  var.*/
            if _      ==5  then iterate          /*is this integer a multiple of five?  */
            if j // 3 ==0  then iterate          /* "   "     "    "     "     " three? */
                                                 /* [↓]  divide by the primes.   ___    */
                  do k=4  to #  while  k*k<=j    /*divide  J  by other primes ≤ √ J     */
                  if j//@.k == 0  then iterate j /*÷ by prev. prime?  ¬prime     ___    */
                  end   /*k*/                    /* [↑]   only divide up to     √ J     */
            #= # + 1;          @.#= j;  !.j= 1   /*bump prime count; assign prime & flag*/
            end   /*j*/
     return
output   when using the default inputs:
(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 prime pairs.
found  81  unique cousin primes.

Raku

Filter

Favoring brevity over efficiency due to the small range of n, the most concise solution is:

say grep *.all.is-prime, map { $_, $_+4 }, 2..999;
Output:
((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))

Infinite List

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.
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);
Output:
((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))

Ring

load "stdlib.ring"

see "working..." + nl
see "cousin primes are:" + nl

ind = 0
row = 0
limit = 1000
cousin = []

for n = 1 to limit
    if isprime(n) and isprime(n+4)
       row = row + 1
       ind1 = find(cousin,n)
       ind2 = find(cousin,n+4)
       if ind1 < 1
          add(cousin,n)
       ok
       if ind2 < 1
          add(cousin,n+4)
       ok  
       see "(" + n + ", " + (n+4) + ") "
          if row%5 = 0
             see nl
          ok
    ok
next

lencousin = len(cousin)
see nl + "found " + row + " cousin prime pairs." + nl
see "found " + lencousin + " unique cousin primes." + nl

see "done..." + nl
Output:
working...
cousin primes are:
(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 prime pairs.
found 81 unique cousin primes.
done...

RPL

Works with: HP version 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
 ≫ ≫ 'TASK' STO
Output:
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

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."
Output:
[[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.

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;
Output:
  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.

Sidef

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"
Output:
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

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)")
Output:
(  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

Wren

Library: Wren-math
Library: Wren-fmt
import "./math" for Int
import "./fmt" for Fmt

var c = Int.primeSieve(999, false)
var count = 0
System.print("Cousin prime pairs whose elements are less than 1,000:")
var i = 3
while (i <= 995) {
    if (!c[i] && !c[i + 4]) {
        Fmt.write("$3d:$3d  ", i, i + 4)
        count = count + 1
        if ((count % 7) == 0) System.print()
        i = (i != 3) ? i + 4 : i + 2
    }
    i = i + 2
}
System.print("\n\n%(count) pairs found")
Output:
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

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);
]
Output:
(  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.