Cousin primes

From Rosetta Code
Revision as of 19:40, 18 March 2021 by PureFox (talk | contribs) (Clarified task description.)
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 less than 1,000.

Optionally, show the number of such pairs.


Pascal

Works with: Free Pascal

Sieving only odd numbers.

<lang pascal>program CousinPrimes; //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

 MAXZAHL = 1000;// > 3
 MAXLIMIT = (MAXZAHL-1) DIV 2;
 //estimate
 CountOfPrimes = trunc(MAXZAHL/(ln(MAXZAHL)-1.08))+100;

type

 tChkprimes = array[0..MAXLIMIT] of byte;//prime == 1 , nonprime == 0

var

 Chkprimes:tChkprimes;  
 primes : array[0..CountOfPrimes]of Uint32;//here starting with 3
 primeCount:NativeInt;

procedure InitPrimes; //sieve of eratothenes var

 i,j : NativeInt;

begin

 // i = 2*j+1
 fillchar(Chkprimes,SizeOf(tChkprimes),#1);
 Chkprimes[0] := 0;
 i := 1;
 repeat
   if Chkprimes[(i-1) DIV 2] <> 0 then
   Begin
     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);
   end;  

// writeln('prime count ',j+1,' til ',MAXZAHL); //count "2"

 primeCount := j-1;
 IF CountOfPrimes-primeCount < 0 then 
 begin
   writeln(' Need more space for primes ', primeCount-CountOfPrimes);
   HALT(-243);
 end;

end; var

 i,cnt : NativeInt;

BEGIN

 InitPrimes;
 //only exception, that the index difference is greater 1
write(primes[0]:3,':',primes[2]:3,' ');  
 cnt := 1; 
 For i := 1 to primeCount do 
   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;    
 writeln;
 writeln(cnt,' cousin primes found');  

END. </lang>

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 found

Ring

<lang ring> load "stdlib.ring"

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

ind = 0 row = 0 limit = 1000

for n = 1 to limit

   if isprime(n) and isprime(n+4)
      row = row + 1
      see "(" + n + ", " + (n+4) + ") "
         if row%5 = 0
            see nl
         ok
   ok

next

see nl + "done..." + nl </lang>

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

Wren

Library: Wren-math
Library: Wren-fmt

<lang ecmascript>import "/math" for Int import "/fmt" for Fmt

var c = Int.primeSieve(999, false) var count = 0 System.print("Cousin primes under 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")</lang>

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