Cousin primes: Difference between revisions
m
syntax highlighting fixup automation
(→{{header|ALGOL 68}}: Make output more compact) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 22:
{{trans|Nim}}
<
F isPrime(n)
Line 43:
L(cousins) cousinList
print(String(cousins).center(10), end' I (L.index + 1) % 7 == 0 {"\n"} E ‘ ’)
print()</
{{out}}
Line 58:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<
PROC Main()
Line 75:
OD
PrintF("%E%EThere are %I pairs",count)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Cousin_primes.png Screenshot from Atari 8-bit computer]
Line 89:
=={{header|Ada}}==
<
procedure Cousin_Primes is
Line 134:
New_Line;
Put_Line (Count'Image & " pairs.");
end Cousin_Primes;</
{{out}}
<pre>[ 3, 7] [ 7, 11] [ 13, 17] [ 19, 23] [ 37, 41] [ 43, 47] [ 67, 71] [ 79, 83]
Line 146:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<
# sieve the primes as required by the task #
PR read "primes.incl.a68" PR
Line 170:
OD;
print( ( newline, "Found ", whole( p count, 0 ), " cousin primes", newline ) )
END</
{{out}}
<pre>
Line 182:
=={{header|ALGOL W}}==
<
integer MAX_PRIME;
MAX_PRIME := 1000;
Line 209:
write( i_w := 1, s_w := 0, "Found ", cCount, " cousin prime pairs up to ", MAX_PRIME )
end
end.</
{{out}}
<pre>
Line 221:
=={{header|APL}}==
<
{{out}}
Line 269:
=={{header|AppleScript}}==
<
script o
property numberList : {missing value}
Line 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)}</
{{output}}
<
=={{header|Arturo}}==
<
primesUpto: select 0..upto => prime?
return select primesUpto => [prime? & + 4]
]
print map cousins 1000 'c -> @[c, c + 4]</
{{out}}
Line 313:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f COUSIN_PRIMES.AWK
BEGIN {
Line 337:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 349:
=={{header|BASIC}}==
<
20 FOR P=2 TO SQR(L)
30 IF S(P) THEN 50
Line 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</
{{out}}
Line 406:
=={{header|BCPL}}==
<
manifest $( LIMIT = 1000 $)
Line 437:
$)
writef("*N%N pairs found.*N", count)
$)</
{{out}}
<pre style="height:14em;">3, 7
Line 484:
=={{header|C}}==
<
#include <string.h>
Line 512:
printf("There are %d cousin prime pairs below %d.\n", count, LIMIT);
return 0;
}</
{{out}}
Line 560:
=={{header|COBOL}}==
<
PROGRAM-ID. COUSIN-PRIMES.
Line 610:
FLAG-PRIME. MOVE 1 TO PRIME-FLAG(Q).
UNFLAG-PRIME. MOVE 0 TO PRIME-FLAG(Q).
DONE. EXIT.</
{{out}}
<pre style='height:14em;'> 3 7
Line 656:
=={{header|Cowgol}}==
<
const LIMIT := 1000;
Line 693:
print(" cousin prime pairs below ");
print_i16(LIMIT);
print_nl();</
{{out}}
Line 742:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions 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 ""
</syntaxhighlight>
{{out}}
<pre>
Line 752:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
sequences ;
Line 759:
[ [ prime? ] all? ] lfilter ;
lcousins [ last 1000 < ] lwhile [ . ] leach</
{{out}}
<pre style="height:14em">
Line 808:
=={{header|FOCAL}}==
<
01.20 T %4
01.30 F N=3,2,996;D 2
Line 826:
03.50 S K=K+1
03.60 G 3.2
03.70 S A=0</
{{out}}
Line 875:
=={{header|Forth}}==
{{works with|Gforth}}
<
: not-prime! ( n -- ) here + 1 swap c! ;
Line 909:
1000 cousin-primes
bye</
{{out}}
Line 928:
Use one of the primality testing examples as an include.
<
dim as uinteger c=0, i
Line 936:
print using "Pair ##: #### and ####"; c; i; i+4
end if
next i</
{{out}}
Line 985:
=={{header|Go}}==
{{trans|Wren}}
<
import "fmt"
Line 1,031:
}
fmt.Printf("\n\n%d pairs found\n", count)
}</
{{out}}
Line 1,047:
=={{header|Haskell}}==
<
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (isPrime, primes)
Line 1,078:
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</
{{Out}}
<pre>41 cousin prime pairs:
Line 1,093:
=={{header|J}}==
<
{{out}}
<pre style="height:14em;"> 3 7
Line 1,142:
'''Works with gojq, the Go implementation of jq'''
For the definition of `is_prime` used here, see https://rosettacode.org/wiki/Additive_primes<
def cousins:
# [2,6] is not a cousin so we can start at 3
Line 1,149:
| [., .+4];
997 | cousins</
{{out}}
See below.
Line 1,155:
'''The Count'''
To compute the pairs and the count at the same time without saving them as an array:<
foreach ((997|cousins),null) as $c (-1; .+1; if $c == null then "\nCount is \(.)" else $c end)</
{{out}}
<pre>
Line 1,206:
=={{header|Julia}}==
{{trans|Wren}}
<
let
Line 1,220:
println("\n\n$pcount pairs found.")
end
</
<pre>
Cousin prime pairs under 1,000:
Line 1,234:
=={{header|MAD}}==
<
BOOLEAN PRIME
DIMENSION PRIME(1000)
Line 1,260:
VECTOR VALUES COUSIN = $I4,2H: ,I4*$
VECTOR VALUES TOTAL = $15HTOTAL COUSINS: ,I2*$
END OF PROGRAM </
{{out}}
Line 1,308:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
primes = {primes, primes + 4} // Transpose;
Select[primes, AllTrue[PrimeQ]]
Length[%]</
{{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}}
Line 1,318:
=={{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.
<
const N = 1000
Line 1,344:
stdout.write ($cousins).center(10)
stdout.write if (i+1) mod 7 == 0: '\n' else: ' '
echo()</
{{out}}
Line 1,358:
{{works with|Free Pascal}}
{{works with|Delphi}}Sieving only odd numbers.
<
//Free Pascal Compiler version 3.2.1 [2020/11/03] for x86_64fpc
{$IFDEF FPC}
Line 1,468:
setlength(primes,0);
END.</
{{out}}
<pre>
Line 1,491:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use feature 'say';
use ntheory 'is_prime';
Line 1,497:
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;</
{{out}}
<pre>41 cousin prime pairs < 1000:
Line 1,508:
=={{header|Phix}}==
<!--<
<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,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: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
<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,529:
=={{header|Python}}==
<
from itertools import chain, takewhile
Line 1,654:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>41 cousin pairs below 1000:
Line 1,672:
=={{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.
<
parse arg hi cols . /*get optional number of primes to find*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then assume default.*/
Line 1,708:
#= # + 1; @.#= j; !.j= 1 /*bump prime count; assign prime & flag*/
end /*j*/
return</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,724:
===Filter===
Favoring brevity over efficiency due to the small range of n, the most concise solution is:
<syntaxhighlight lang="raku"
{{out}}
<pre>
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 :
:Apart from the first term, all terms are of the form 6n + 1.
<syntaxhighlight lang="raku"
my $count = @cousins.first: :k, *.[0] > 1000;
.say for @cousins.head($count).batch(9);</
{{out}}
<pre>
Line 1,746:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 1,780:
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,800:
=={{header|Ruby}}==
<
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]]
Line 1,811:
=={{header|Seed7}}==
<
const func boolean: isPrime (in integer: number) is func
Line 1,843:
end for;
writeln("\n" <& count <& " cousin prime pairs found < 1000.");
end func;</
{{out}}
<pre style="height:14em">
Line 1,892:
=={{header|Sidef}}==
<
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"</
{{out}}
<pre>
Line 1,907:
=={{header|Swift}}==
<
func primeSieve(limit: Int) -> [Bool] {
Line 1,951:
}
}
print("\nNumber of cousin prime pairs < \(limit): \(count)")</
{{out}}
Line 1,970:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 1,986:
i = i + 2
}
System.print("\n\n%(count) pairs found")</
{{out}}
|