Cousin primes: Difference between revisions
Content added Content deleted
(→{{header|ALGOL 68}}: Make output more compact) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 22: | Line 22: | ||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang="11l">V LIMIT = 1000 |
||
F isPrime(n) |
F isPrime(n) |
||
Line 43: | Line 43: | ||
L(cousins) cousinList |
L(cousins) cousinList |
||
print(String(cousins).center(10), end' I (L.index + 1) % 7 == 0 {"\n"} E ‘ ’) |
print(String(cousins).center(10), end' I (L.index + 1) % 7 == 0 {"\n"} E ‘ ’) |
||
print()</ |
print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 58: | Line 58: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
{{libheader|Action! Sieve of Eratosthenes}} |
{{libheader|Action! Sieve of Eratosthenes}} |
||
< |
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT" |
||
PROC Main() |
PROC Main() |
||
Line 75: | Line 75: | ||
OD |
OD |
||
PrintF("%E%EThere are %I pairs",count) |
PrintF("%E%EThere are %I pairs",count) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Cousin_primes.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Cousin_primes.png Screenshot from Atari 8-bit computer] |
||
Line 89: | Line 89: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_Io; |
||
procedure Cousin_Primes is |
procedure Cousin_Primes is |
||
Line 134: | Line 134: | ||
New_Line; |
New_Line; |
||
Put_Line (Count'Image & " pairs."); |
Put_Line (Count'Image & " pairs."); |
||
end Cousin_Primes;</ |
end Cousin_Primes;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[ 3, 7] [ 7, 11] [ 13, 17] [ 19, 23] [ 37, 41] [ 43, 47] [ 67, 71] [ 79, 83] |
<pre>[ 3, 7] [ 7, 11] [ 13, 17] [ 19, 23] [ 37, 41] [ 43, 47] [ 67, 71] [ 79, 83] |
||
Line 146: | Line 146: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{libheader|ALGOL 68-primes}} |
{{libheader|ALGOL 68-primes}} |
||
< |
<syntaxhighlight lang="algol68">BEGIN # find cousin primes - pairs of primes that differ by 4 # |
||
# sieve the primes as required by the task # |
# sieve the primes as required by the task # |
||
PR read "primes.incl.a68" PR |
PR read "primes.incl.a68" PR |
||
Line 170: | Line 170: | ||
OD; |
OD; |
||
print( ( newline, "Found ", whole( p count, 0 ), " cousin primes", newline ) ) |
print( ( newline, "Found ", whole( p count, 0 ), " cousin primes", newline ) ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 182: | Line 182: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
< |
<syntaxhighlight lang="algolw">begin % find some cousin primes: primes p where p + 4 is also a prime % |
||
integer MAX_PRIME; |
integer MAX_PRIME; |
||
MAX_PRIME := 1000; |
MAX_PRIME := 1000; |
||
Line 209: | Line 209: | ||
write( i_w := 1, s_w := 0, "Found ", cCount, " cousin prime pairs up to ", MAX_PRIME ) |
write( i_w := 1, s_w := 0, "Found ", cCount, " cousin prime pairs up to ", MAX_PRIME ) |
||
end |
end |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 221: | Line 221: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
< |
<syntaxhighlight lang="apl">(⎕←'Amount:',⊃⍴P)⊢P,4+P←⍪((P+4)∊P)/P←(~P∊P∘.×P)/P←1↓⍳1000</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 269: | Line 269: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang="applescript">on sieveOfEratosthenes(limit) |
||
script o |
script o |
||
property numberList : {missing value} |
property numberList : {missing value} |
||
Line 294: | Line 294: | ||
if (p - 4 is in primes) then set end of output to {p - 4, p's contents} |
if (p - 4 is in primes) then set end of output to {p - 4, p's contents} |
||
end repeat |
end repeat |
||
return {|cousin prime pairs < 1000|:output, |count thereof|:(count output)}</ |
return {|cousin prime pairs < 1000|:output, |count thereof|:(count output)}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
< |
<syntaxhighlight 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}</syntaxhighlight> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">cousins: function [upto][ |
||
primesUpto: select 0..upto => prime? |
primesUpto: select 0..upto => prime? |
||
return select primesUpto => [prime? & + 4] |
return select primesUpto => [prime? & + 4] |
||
] |
] |
||
print map cousins 1000 'c -> @[c, c + 4]</ |
print map cousins 1000 'c -> @[c, c + 4]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 313: | Line 313: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f COUSIN_PRIMES.AWK |
# syntax: GAWK -f COUSIN_PRIMES.AWK |
||
BEGIN { |
BEGIN { |
||
Line 337: | Line 337: | ||
return(1) |
return(1) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 349: | Line 349: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
< |
<syntaxhighlight lang="basic">10 DEFINT A-Z: L=1000: DIM S(L) |
||
20 FOR P=2 TO SQR(L) |
20 FOR P=2 TO SQR(L) |
||
30 IF S(P) THEN 50 |
30 IF S(P) THEN 50 |
||
Line 358: | Line 358: | ||
80 IF S(P)+S(P+4)=0 THEN N=N+1: PRINT P,P+4 |
80 IF S(P)+S(P+4)=0 THEN N=N+1: PRINT P,P+4 |
||
90 NEXT |
90 NEXT |
||
100 PRINT "There are";N;"cousin prime pairs below";L</ |
100 PRINT "There are";N;"cousin prime pairs below";L</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 406: | Line 406: | ||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
< |
<syntaxhighlight lang="bcpl">get "libhdr" |
||
manifest $( LIMIT = 1000 $) |
manifest $( LIMIT = 1000 $) |
||
Line 437: | Line 437: | ||
$) |
$) |
||
writef("*N%N pairs found.*N", count) |
writef("*N%N pairs found.*N", count) |
||
$)</ |
$)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:14em;">3, 7 |
<pre style="height:14em;">3, 7 |
||
Line 484: | Line 484: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 512: | Line 512: | ||
printf("There are %d cousin prime pairs below %d.\n", count, LIMIT); |
printf("There are %d cousin prime pairs below %d.\n", count, LIMIT); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 560: | Line 560: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. COUSIN-PRIMES. |
PROGRAM-ID. COUSIN-PRIMES. |
||
Line 610: | Line 610: | ||
FLAG-PRIME. MOVE 1 TO PRIME-FLAG(Q). |
FLAG-PRIME. MOVE 1 TO PRIME-FLAG(Q). |
||
UNFLAG-PRIME. MOVE 0 TO PRIME-FLAG(Q). |
UNFLAG-PRIME. MOVE 0 TO PRIME-FLAG(Q). |
||
DONE. EXIT.</ |
DONE. EXIT.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style='height:14em;'> 3 7 |
<pre style='height:14em;'> 3 7 |
||
Line 656: | Line 656: | ||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
< |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
const LIMIT := 1000; |
const LIMIT := 1000; |
||
Line 693: | Line 693: | ||
print(" cousin prime pairs below "); |
print(" cousin prime pairs below "); |
||
print_i16(LIMIT); |
print_i16(LIMIT); |
||
print_nl();</ |
print_nl();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 742: | Line 742: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)] |
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)] |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Cousin Primes: Nigel Galloway. April 2nd., 2021 |
// 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 "" |
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}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 752: | Line 752: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2021-02-05}} |
{{works with|Factor|0.99 2021-02-05}} |
||
< |
<syntaxhighlight lang="factor">USING: kernel lists lists.lazy math math.primes prettyprint |
||
sequences ; |
sequences ; |
||
Line 759: | Line 759: | ||
[ [ prime? ] all? ] lfilter ; |
[ [ prime? ] all? ] lfilter ; |
||
lcousins [ last 1000 < ] lwhile [ . ] leach</ |
lcousins [ last 1000 < ] lwhile [ . ] leach</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:14em"> |
<pre style="height:14em"> |
||
Line 808: | Line 808: | ||
=={{header|FOCAL}}== |
=={{header|FOCAL}}== |
||
< |
<syntaxhighlight lang="focal">01.10 S C=0 |
||
01.20 T %4 |
01.20 T %4 |
||
01.30 F N=3,2,996;D 2 |
01.30 F N=3,2,996;D 2 |
||
Line 826: | Line 826: | ||
03.50 S K=K+1 |
03.50 S K=K+1 |
||
03.60 G 3.2 |
03.60 G 3.2 |
||
03.70 S A=0</ |
03.70 S A=0</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 875: | Line 875: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
{{works with|Gforth}} |
{{works with|Gforth}} |
||
< |
<syntaxhighlight lang="forth">: prime? ( n -- ? ) here + c@ 0= ; |
||
: not-prime! ( n -- ) here + 1 swap c! ; |
: not-prime! ( n -- ) here + 1 swap c! ; |
||
Line 909: | Line 909: | ||
1000 cousin-primes |
1000 cousin-primes |
||
bye</ |
bye</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 928: | Line 928: | ||
Use one of the primality testing examples as an include. |
Use one of the primality testing examples as an include. |
||
< |
<syntaxhighlight lang="freebasic">#include "isprime.bas" |
||
dim as uinteger c=0, i |
dim as uinteger c=0, i |
||
Line 936: | Line 936: | ||
print using "Pair ##: #### and ####"; c; i; i+4 |
print using "Pair ##: #### and ####"; c; i; i+4 |
||
end if |
end if |
||
next i</ |
next i</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 985: | Line 985: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,031: | Line 1,031: | ||
} |
} |
||
fmt.Printf("\n\n%d pairs found\n", count) |
fmt.Printf("\n\n%d pairs found\n", count) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,047: | Line 1,047: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (intercalate, transpose) |
||
import Data.List.Split (chunksOf) |
import Data.List.Split (chunksOf) |
||
import Data.Numbers.Primes (isPrime, primes) |
import Data.Numbers.Primes (isPrime, primes) |
||
Line 1,078: | Line 1,078: | ||
let ws = maximum . fmap length <$> transpose rows |
let ws = maximum . fmap length <$> transpose rows |
||
pw = printf . flip intercalate ["%", "s"] . show |
pw = printf . flip intercalate ["%", "s"] . show |
||
in unlines $ intercalate gap . zipWith pw ws <$> rows</ |
in unlines $ intercalate gap . zipWith pw ws <$> rows</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>41 cousin prime pairs: |
<pre>41 cousin prime pairs: |
||
Line 1,093: | Line 1,093: | ||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">(":,'Amount: ',":@#) (,.,.4+,.) (]#~1:p:4:+]) p:i.168</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:14em;"> 3 7 |
<pre style="height:14em;"> 3 7 |
||
Line 1,142: | Line 1,142: | ||
'''Works with gojq, the Go implementation of jq''' |
'''Works with gojq, the Go implementation of jq''' |
||
For the definition of `is_prime` used here, see https://rosettacode.org/wiki/Additive_primes< |
For the definition of `is_prime` used here, see https://rosettacode.org/wiki/Additive_primes<syntaxhighlight lang="jq"># Output: a stream |
||
def cousins: |
def cousins: |
||
# [2,6] is not a cousin so we can start at 3 |
# [2,6] is not a cousin so we can start at 3 |
||
Line 1,149: | Line 1,149: | ||
| [., .+4]; |
| [., .+4]; |
||
997 | cousins</ |
997 | cousins</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
See below. |
See below. |
||
Line 1,155: | Line 1,155: | ||
'''The Count''' |
'''The Count''' |
||
To compute the pairs and the count at the same time without saving them as an array:< |
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)</ |
foreach ((997|cousins),null) as $c (-1; .+1; if $c == null then "\nCount is \(.)" else $c end)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,206: | Line 1,206: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="julia">using Primes |
||
let |
let |
||
Line 1,220: | Line 1,220: | ||
println("\n\n$pcount pairs found.") |
println("\n\n$pcount pairs found.") |
||
end |
end |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Cousin prime pairs under 1,000: |
Cousin prime pairs under 1,000: |
||
Line 1,234: | Line 1,234: | ||
=={{header|MAD}}== |
=={{header|MAD}}== |
||
< |
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER |
||
BOOLEAN PRIME |
BOOLEAN PRIME |
||
DIMENSION PRIME(1000) |
DIMENSION PRIME(1000) |
||
Line 1,260: | Line 1,260: | ||
VECTOR VALUES COUSIN = $I4,2H: ,I4*$ |
VECTOR VALUES COUSIN = $I4,2H: ,I4*$ |
||
VECTOR VALUES TOTAL = $15HTOTAL COUSINS: ,I2*$ |
VECTOR VALUES TOTAL = $15HTOTAL COUSINS: ,I2*$ |
||
END OF PROGRAM </ |
END OF PROGRAM </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,308: | Line 1,308: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">primes = Prime@Range[PrimePi[1000] - 1]; |
||
primes = {primes, primes + 4} // Transpose; |
primes = {primes, primes + 4} // Transpose; |
||
Select[primes, AllTrue[PrimeQ]] |
Select[primes, AllTrue[PrimeQ]] |
||
Length[%]</ |
Length[%]</syntaxhighlight> |
||
{{out}} |
{{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}} |
<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}} |
||
Line 1,318: | Line 1,318: | ||
=={{header|Nim}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="nim">import sets, strutils, sugar |
||
const N = 1000 |
const N = 1000 |
||
Line 1,344: | Line 1,344: | ||
stdout.write ($cousins).center(10) |
stdout.write ($cousins).center(10) |
||
stdout.write if (i+1) mod 7 == 0: '\n' else: ' ' |
stdout.write if (i+1) mod 7 == 0: '\n' else: ' ' |
||
echo()</ |
echo()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,358: | Line 1,358: | ||
{{works with|Free Pascal}} |
{{works with|Free Pascal}} |
||
{{works with|Delphi}}Sieving only odd numbers. |
{{works with|Delphi}}Sieving only odd numbers. |
||
< |
<syntaxhighlight lang="pascal">program Cousin_primes; |
||
//Free Pascal Compiler version 3.2.1 [2020/11/03] for x86_64fpc |
//Free Pascal Compiler version 3.2.1 [2020/11/03] for x86_64fpc |
||
{$IFDEF FPC} |
{$IFDEF FPC} |
||
Line 1,468: | Line 1,468: | ||
setlength(primes,0); |
setlength(primes,0); |
||
END.</ |
END.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,491: | Line 1,491: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
use ntheory 'is_prime'; |
use ntheory 'is_prime'; |
||
Line 1,497: | Line 1,497: | ||
my($limit, @cp) = 1000; |
my($limit, @cp) = 1000; |
||
is_prime($_) and is_prime($_+4) and push @cp, "$_/@{[$_+4]}" for 2..$limit; |
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;</ |
say @cp . " cousin prime pairs < $limit:\n" . (sprintf "@{['%8s' x @cp]}", @cp) =~ s/(.{56})/$1\n/gr;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>41 cousin prime pairs < 1000: |
<pre>41 cousin prime pairs < 1000: |
||
Line 1,508: | Line 1,508: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="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;">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> |
<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,516: | Line 1,516: | ||
<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: #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> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
<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> |
<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}} |
{{out}} |
||
Line 1,529: | Line 1,529: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">'''Cousin primes''' |
||
from itertools import chain, takewhile |
from itertools import chain, takewhile |
||
Line 1,654: | Line 1,654: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>41 cousin pairs below 1000: |
<pre>41 cousin pairs below 1000: |
||
Line 1,672: | Line 1,672: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
This REXX version allows the limit to be specified, as well as the number of cousin prime pairs to be shown per line. |
This REXX version allows the limit to be specified, as well as the number of cousin prime pairs to be shown per line. |
||
< |
<syntaxhighlight 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*/ |
parse arg hi cols . /*get optional number of primes to find*/ |
||
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then assume default.*/ |
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then assume default.*/ |
||
Line 1,708: | Line 1,708: | ||
#= # + 1; @.#= j; !.j= 1 /*bump prime count; assign prime & flag*/ |
#= # + 1; @.#= j; !.j= 1 /*bump prime count; assign prime & flag*/ |
||
end /*j*/ |
end /*j*/ |
||
return</ |
return</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 1,724: | Line 1,724: | ||
===Filter=== |
===Filter=== |
||
Favoring brevity over efficiency due to the small range of n, the most concise solution is: |
Favoring brevity over efficiency due to the small range of n, the most concise solution is: |
||
<lang |
<syntaxhighlight lang="raku" line>say grep *.all.is-prime, map { $_, $_+4 }, 2..999;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,732: | Line 1,732: | ||
A more efficient and versatile approach is to generate an infinite list of cousin primes, using this info from https://oeis.org/A023200 : |
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. |
:Apart from the first term, all terms are of the form 6n + 1. |
||
<lang |
<syntaxhighlight lang="raku" line>constant @cousins = (3, 7, *+6 … *).map: -> \n { (n, n+4) if (n & n+4).is-prime }; |
||
my $count = @cousins.first: :k, *.[0] > 1000; |
my $count = @cousins.first: :k, *.[0] > 1000; |
||
.say for @cousins.head($count).batch(9);</ |
.say for @cousins.head($count).batch(9);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,746: | Line 1,746: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
Line 1,780: | Line 1,780: | ||
see "done..." + nl |
see "done..." + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,800: | Line 1,800: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">require 'prime' |
||
primes = Prime.each(1000).to_a |
primes = Prime.each(1000).to_a |
||
p cousins = primes.filter_map{|pr| [pr, pr+4] if primes.include?(pr+4) } |
p cousins = primes.filter_map{|pr| [pr, pr+4] if primes.include?(pr+4) } |
||
puts "#{cousins.size} cousins found." |
puts "#{cousins.size} cousins found." |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{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]] |
<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]] |
||
Line 1,811: | Line 1,811: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const func boolean: isPrime (in integer: number) is func |
const func boolean: isPrime (in integer: number) is func |
||
Line 1,843: | Line 1,843: | ||
end for; |
end for; |
||
writeln("\n" <& count <& " cousin prime pairs found < 1000."); |
writeln("\n" <& count <& " cousin prime pairs found < 1000."); |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:14em"> |
<pre style="height:14em"> |
||
Line 1,892: | Line 1,892: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var limit = 1000 |
||
var pairs = (limit-5).primes.map { [_, _+4] }.grep { .tail.is_prime } |
var pairs = (limit-5).primes.map { [_, _+4] }.grep { .tail.is_prime } |
||
say "Cousin prime pairs whose elements are less than #{limit.commify}:" |
say "Cousin prime pairs whose elements are less than #{limit.commify}:" |
||
say pairs |
say pairs |
||
say "\n#{pairs.len} pairs found"</ |
say "\n#{pairs.len} pairs found"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,907: | Line 1,907: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
func primeSieve(limit: Int) -> [Bool] { |
func primeSieve(limit: Int) -> [Bool] { |
||
Line 1,951: | Line 1,951: | ||
} |
} |
||
} |
} |
||
print("\nNumber of cousin prime pairs < \(limit): \(count)")</ |
print("\nNumber of cousin prime pairs < \(limit): \(count)")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,970: | Line 1,970: | ||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/math" for Int |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 1,986: | Line 1,986: | ||
i = i + 2 |
i = i + 2 |
||
} |
} |
||
System.print("\n\n%(count) pairs found")</ |
System.print("\n\n%(count) pairs found")</syntaxhighlight> |
||
{{out}} |
{{out}} |