Cousin primes: Difference between revisions

m
m (→‎{{header|J}}: minor note about code structure)
m (→‎{{header|Wren}}: Minor tidy)
 
(3 intermediate revisions by 3 users not shown)
Line 1,233:
 
41 pairs found.
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
do -- find primes p where p+4 is also prime
local MAX_PRIME = 1000
local p = {} -- sieve the odd primes to MAX_PRIME
for i = 3, MAX_PRIME, 2 do p[ i ] = true end
for i = 3, math.floor( math.sqrt( MAX_PRIME ) ), 2 do
if p[ i ] then
for s = i * i, MAX_PRIME, i + i do p[ s ] = false end
end
end
local function fmt ( n ) return string.format( "%3d", n ) end
io.write( "Cousin primes under ", MAX_PRIME, ":\n" )
local cCount = 0
for i = 3, MAX_PRIME - 4, 2 do
if p[ i ] and p[ i + 4 ] then
cCount = cCount + 1
io.write( "[ ", fmt( i ), " ", fmt( i + 4 ), " ]"
, ( cCount % 8 == 0 and "\n" or " " )
)
end
end
io.write( "\nFound ", cCount, " cousin primes\n" )
end
</syntaxhighlight>
{{out}}
<pre>
Cousin primes under 1000:
[ 3 7 ] [ 7 11 ] [ 13 17 ] [ 19 23 ] [ 37 41 ] [ 43 47 ] [ 67 71 ] [ 79 83 ]
[ 97 101 ] [ 103 107 ] [ 109 113 ] [ 127 131 ] [ 163 167 ] [ 193 197 ] [ 223 227 ] [ 229 233 ]
[ 277 281 ] [ 307 311 ] [ 313 317 ] [ 349 353 ] [ 379 383 ] [ 397 401 ] [ 439 443 ] [ 457 461 ]
[ 463 467 ] [ 487 491 ] [ 499 503 ] [ 613 617 ] [ 643 647 ] [ 673 677 ] [ 739 743 ] [ 757 761 ]
[ 769 773 ] [ 823 827 ] [ 853 857 ] [ 859 863 ] [ 877 881 ] [ 883 887 ] [ 907 911 ] [ 937 941 ]
[ 967 971 ]
Found 41 cousin primes
</pre>
 
Line 1,822 ⟶ 1,859:
found 81 unique cousin primes.
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
≪ { } → cousins
≪ 2 3 5
'''DO'''
ROT DROP DUP NEXTPRIME
'''CASE'''
DUP 4 PICK - 4 == '''THEN''' PICK3 OVER R→C 'cousins' SWAP STO+ '''END'''
DUP2 - -4 == '''THEN''' DUP2 R→C 'cousins' SWAP STO+ '''END'''
'''END'''
'''UNTIL''' DUP 1000 ≥ '''END'''
3 DROPN
cousins DUP SIZE
≫ ≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
2: { (3., 7.) (7., 11.) (13., 17.) (19., 23.) (37., 41.) (43., 47.) (67., 71.) (79., 83.) (97., 101.) (103., 107.) (109., 113.) (127., 131.) (163., 167.) (193., 197.) (223., 227.) (229., 233.) (277., 281.) (307., 311.) (313., 317.) (349., 353.) (379., 383.) (397., 401.) (439., 443.) (457., 461.) (463., 467.) (487., 491.) (499., 503.) (613., 617.) (643., 647.) (673., 677.) (739., 743.) (757., 761.) (769., 773.) (823., 827.) (853., 857.) (859., 863.) (877., 881.) (883., 887.) (907., 911.) (937., 941.) (967., 971.) }
1: 41
</pre>
 
Line 1,995 ⟶ 2,052:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
var c = Int.primeSieve(999, false)
Line 2,024 ⟶ 2,081:
 
41 pairs found
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">include xpllib; \For IsPrime and Print
int N, C;
[C:= 0;
for N:= 2 to 1000-1-4 do
[if IsPrime(N) then
if IsPrime(N+4) then
[Print("(%3.0f, %3.0f) ", float(N), float(N+4));
C:= C+1;
if rem(C/6) = 0 then CrLf(0);
];
];
Print("\nThere are %d cousin primes less than 1000.\n", C);
]</syntaxhighlight>
{{out}}
<pre>
( 3, 7) ( 7, 11) ( 13, 17) ( 19, 23) ( 37, 41) ( 43, 47)
( 67, 71) ( 79, 83) ( 97, 101) (103, 107) (109, 113) (127, 131)
(163, 167) (193, 197) (223, 227) (229, 233) (277, 281) (307, 311)
(313, 317) (349, 353) (379, 383) (397, 401) (439, 443) (457, 461)
(463, 467) (487, 491) (499, 503) (613, 617) (643, 647) (673, 677)
(739, 743) (757, 761) (769, 773) (823, 827) (853, 857) (859, 863)
(877, 881) (883, 887) (907, 911) (937, 941) (967, 971)
There are 41 cousin primes less than 1000.
</pre>
9,485

edits