Prime triplets: Difference between revisions

m
m (fixed where I messed up)
m (→‎{{header|Wren}}: Minor tidy)
 
(11 intermediate revisions by 6 users not shown)
Line 366:
next p
end</syntaxhighlight>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
 
function IsPrime(N: int64): boolean;
{Fast, optimised prime test}
var I,Stop: int64;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N+0.0));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
 
 
 
 
procedure ShowTriple026Prime(Memo: TMemo);
var N,Sum,Cnt: integer;
var NS,S: string;
begin
Cnt:=0;
S:='';
for N:=1 to 5500-1 do
if IsPrime(N) then
if IsPrime(N+2) and IsPrime(N+6) then
begin
Inc(Cnt);
S:=S+Format('%6d%6d%6d',[N,N+2,N+6]);
S:=S+CRLF;
end;
Memo.Lines.Add(' P P+2 P+6');
Memo.Lines.Add('------------------');
Memo.Lines.Add(S);
Memo.Lines.Add('Count = '+IntToStr(Cnt));
end;
 
</syntaxhighlight>
{{out}}
<pre>
P P+2 P+6
------------------
5 7 11
11 13 17
17 19 23
41 43 47
101 103 107
107 109 113
191 193 197
227 229 233
311 313 317
347 349 353
461 463 467
641 643 647
821 823 827
857 859 863
881 883 887
1091 1093 1097
1277 1279 1283
1301 1303 1307
1427 1429 1433
1481 1483 1487
1487 1489 1493
1607 1609 1613
1871 1873 1877
1997 1999 2003
2081 2083 2087
2237 2239 2243
2267 2269 2273
2657 2659 2663
2687 2689 2693
3251 3253 3257
3461 3463 3467
3527 3529 3533
3671 3673 3677
3917 3919 3923
4001 4003 4007
4127 4129 4133
4517 4519 4523
4637 4639 4643
4787 4789 4793
4931 4933 4937
4967 4969 4973
5231 5233 5237
5477 5479 5483
 
Count = 43
Elapsed Time: 13.263 ms.
 
</pre>
 
 
 
Line 438 ⟶ 545:
{{out}}<pre>
[ 5 7 11] [ 11 13 17] [ 17 19 23] [ 41 43 47] [ 101 103 107] [ 107 109 113] [ 191 193 197] [ 227 229 233] [ 311 313 317] [ 347 349 353] [ 461 463 467] [ 641 643 647] [ 821 823 827] [ 857 859 863] [ 881 883 887] [1091 1093 1097] [1277 1279 1283] [1301 1303 1307] [1427 1429 1433] [1481 1483 1487] [1487 1489 1493] [1607 1609 1613] [1871 1873 1877] [1997 1999 2003] [2081 2083 2087] [2237 2239 2243] [2267 2269 2273] [2657 2659 2663] [2687 2689 2693] [3251 3253 3257] [3461 3463 3467] [3527 3529 3533] [3671 3673 3677] [3917 3919 3923] [4001 4003 4007] [4127 4129 4133] [4517 4519 4523] [4637 4639 4643] [4787 4789 4793] [4931 4933 4937] [4967 4969 4973] [5231 5233 5237] [5477 5479 5483]
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn IsPrime( n as NSUInteger ) as BOOL
BOOL isPrime = YES
NSUInteger i
if n < 2 then exit fn = NO
if n = 2 then exit fn = YES
if n mod 2 == 0 then exit fn = NO
for i = 3 to int(n^.5) step 2
if n mod i == 0 then exit fn = NO
next
end fn = isPrime
 
local fn PrimeTriplets( limit as NSUInteger )
NSUInteger p, i = 1
printf @"---------------------"
printf @"Index P P+2 P+6"
printf @"---------------------"
for p = 3 to limit step 2
if fn IsPrime( p+6 ) == NO then continue
if fn IsPrime( p+2 ) == NO then continue
if fn IsPrime( p ) == NO then continue
printf @"%2lu. %5lu %5lu %5lu", i, p, p+2, p+6
i++
next
end fn
 
fn PrimeTriplets( 5500 )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
---------------------
Index P P+2 P+6
---------------------
1. 5 7 11
2. 11 13 17
3. 17 19 23
4. 41 43 47
5. 101 103 107
6. 107 109 113
7. 191 193 197
8. 227 229 233
9. 311 313 317
10. 347 349 353
11. 461 463 467
12. 641 643 647
13. 821 823 827
14. 857 859 863
15. 881 883 887
16. 1091 1093 1097
17. 1277 1279 1283
18. 1301 1303 1307
19. 1427 1429 1433
20. 1481 1483 1487
21. 1487 1489 1493
22. 1607 1609 1613
23. 1871 1873 1877
24. 1997 1999 2003
25. 2081 2083 2087
26. 2237 2239 2243
27. 2267 2269 2273
28. 2657 2659 2663
29. 2687 2689 2693
30. 3251 3253 3257
31. 3461 3463 3467
32. 3527 3529 3533
33. 3671 3673 3677
34. 3917 3919 3923
35. 4001 4003 4007
36. 4127 4129 4133
37. 4517 4519 4523
38. 4637 4639 4643
39. 4787 4789 4793
40. 4931 4933 4937
41. 4967 4969 4973
42. 5231 5233 5237
43. 5477 5479 5483
</pre>
 
Line 492 ⟶ 682:
1030 IF I*I > P THEN RETURN
1040 GOTO 1010</syntaxhighlight>
 
=={{header|J}}==
<syntaxhighlight lang="j">0 2 6 +/~ ((# }:)~ 2 4 E. 2 -~/\ ]) i.&.(p:inv) 5500</syntaxhighlight>
Shorter, but slower:
<syntaxhighlight lang="j">0 2 6 +/~ I. (#: 81) E. 1 p: i. 5500</syntaxhighlight>
{{out}}
<pre>
5 7 11
11 13 17
17 19 23
41 43 47
101 103 107
107 109 113
191 193 197
227 229 233
311 313 317
347 349 353
461 463 467
641 643 647
821 823 827
857 859 863
881 883 887
1091 1093 1097
1277 1279 1283
1301 1303 1307
1427 1429 1433
1481 1483 1487
1487 1489 1493
1607 1609 1613
1871 1873 1877
1997 1999 2003
2081 2083 2087
2237 2239 2243
2267 2269 2273
2657 2659 2663
2687 2689 2693
3251 3253 3257
3461 3463 3467
3527 3529 3533
3671 3673 3677
3917 3919 3923
4001 4003 4007
4127 4129 4133
4517 4519 4523
4637 4639 4643
4787 4789 4793
4931 4933 4937
4967 4969 4973
5231 5233 5237
5477 5479 5483
</pre>
 
=={{header|jq}}==
Line 609 ⟶ 850:
[5477, 5479, 5483]
</pre>
=={{header|Lua}}==
<syntaxhighlight lang="lua">
do -- find primes p where p+2 and p+6 are also prime
local MAX_PRIME = 5500
local pList = {}
do -- set pList to a list of primes up to MAX_PRIME
-- sieve the odd primes to n and construct the list
local p = {}
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
pList[ 1 ] = 2
for i = 3, MAX_PRIME, 2 do
if p[ i ] then pList[ #pList + 1 ] = i end
end
end
local function fmt ( n ) return string.format( "%4d", n ) end
io.write( "Prime triplets under ", MAX_PRIME, ":", "\n" )
local tCount = 0
for i = 1, #pList - 2 do
local p1, p2, p3 = pList[ i ], pList[ i + 1 ], pList[ i + 2 ]
if p2 - p1 == 2 and p3 - p2 == 4 then
tCount = tCount + 1
io.write( "[ ", fmt( p1 ), " ", fmt( p2 ), " ", fmt( p3 ), " ]"
, ( tCount % 5 == 0 and "\n" or " " )
)
end
end
io.write( "\n", "Found ", tCount, " prime triplets\n" )
end
</syntaxhighlight>
{{out}}
<pre>
Prime triplets under 5500:
[ 5 7 11 ] [ 11 13 17 ] [ 17 19 23 ] [ 41 43 47 ] [ 101 103 107 ]
[ 107 109 113 ] [ 191 193 197 ] [ 227 229 233 ] [ 311 313 317 ] [ 347 349 353 ]
[ 461 463 467 ] [ 641 643 647 ] [ 821 823 827 ] [ 857 859 863 ] [ 881 883 887 ]
[ 1091 1093 1097 ] [ 1277 1279 1283 ] [ 1301 1303 1307 ] [ 1427 1429 1433 ] [ 1481 1483 1487 ]
[ 1487 1489 1493 ] [ 1607 1609 1613 ] [ 1871 1873 1877 ] [ 1997 1999 2003 ] [ 2081 2083 2087 ]
[ 2237 2239 2243 ] [ 2267 2269 2273 ] [ 2657 2659 2663 ] [ 2687 2689 2693 ] [ 3251 3253 3257 ]
[ 3461 3463 3467 ] [ 3527 3529 3533 ] [ 3671 3673 3677 ] [ 3917 3919 3923 ] [ 4001 4003 4007 ]
[ 4127 4129 4133 ] [ 4517 4519 4523 ] [ 4637 4639 4643 ] [ 4787 4789 4793 ] [ 4931 4933 4937 ]
[ 4967 4969 4973 ] [ 5231 5233 5237 ] [ 5477 5479 5483 ]
Found 43 prime triplets</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Cases[Partition[Most@NestWhileList[NextPrime, 2, # < 5500 &], 3,
Line 809 ⟶ 1,098:
</pre>
 
 
=={{header|PL/0}}==
PL/0 can only output 1 numeric value per line (and doesn't handle character data at all), so to avoid confusing output, only the first prime of each triplet is shown here.
<syntaxhighlight lang="pascal">
var n, a, prime;
procedure isnprime;
var p;
begin
prime := 1;
if n < 2 then prime := 0;
if n > 2 then begin
prime := 0;
if odd( n ) then prime := 1;
p := 3;
while p * p <= n * prime do begin
if n - ( ( n / p ) * p ) = 0 then prime := 0;
p := p + 2;
end
end
end;
begin
a := 1;
while a < 5495 do begin
a := a + 2;
n := a;
call isnprime;
if prime = 1 then begin
n := a + 2;
call isnprime;
if prime = 1 then begin
n := a + 6;
call isnprime;
if prime = 1 then ! a
end
end
end
end.
</syntaxhighlight>
{{out}}
<pre>
5
11
17
41
101
107
191
227
311
347
461
641
821
857
881
1091
1277
1301
1427
1481
1487
1607
1871
1997
2081
2237
2267
2657
2687
3251
3461
3527
3671
3917
4001
4127
4517
4637
4787
4931
4967
5231
5477
</pre>
 
=={{header|Python}}==
Line 1,094 ⟶ 1,467:
done...
 
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
« { } 2 3 5
'''WHILE''' OVER 5500 < '''REPEAT'''
ROT DROP DUP NEXTPRIME
3 DUPN 3 →LIST
'''IF''' DUP ΔLIST { 2 4 } == '''THEN'''
5 ROLL SWAP 1 →LIST + 4 ROLLD
'''ELSE''' DROP '''END'''
'''END'''
3 DROPN
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: { { 5 7 11 } { 11 13 17 } { 17 19 23 } { 41 43 47 } { 101 103 107 } { 107 109 113 } { 191 193 197 } { 227 229 233 } { 311 313 317 } { 347 349 353 } { 461 463 467 } { 641 643 647 } { 821 823 827 } { 857 859 863 } { 881 883 887 } { 1091 1093 1097 } { 1277 1279 1283 } { 1301 1303 1307 } { 1427 1429 1433 } { 1481 1483 1487 } { 1487 1489 1493 } { 1607 1609 1613 } { 1871 1873 1877 } { 1997 1999 2003 } { 2081 2083 2087 } { 2237 2239 2243 } { 2267 2269 2273 } { 2657 2659 2663 } { 2687 2689 2693 } { 3251 3253 3257 } { 3461 3463 3467 } { 3527 3529 3533 } { 3671 3673 3677 } { 3917 3919 3923 } { 4001 4003 4007 } { 4127 4129 4133 } { 4517 4519 4523 } { 4637 4639 4643 } { 4787 4789 4793 } { 4931 4933 4937 } { 4967 4969 4973 } { 5231 5233 5237 } { 5477 5479 5483 } }
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">puts Prime.each(5500).each_cons(3).filter_map{|p1,p2,p3|[p1,p2,p3].join(" ") if p2-p1 == 2 && p3-p1 == 6}
</syntaxhighlight>
{{out}}
<pre>5 7 11
11 13 17
17 19 23
41 43 47
101 103 107
107 109 113
191 193 197
227 229 233
311 313 317
347 349 353
461 463 467
641 643 647
821 823 827
857 859 863
881 883 887
1091 1093 1097
1277 1279 1283
1301 1303 1307
1427 1429 1433
1481 1483 1487
1487 1489 1493
1607 1609 1613
1871 1873 1877
1997 1999 2003
2081 2083 2087
2237 2239 2243
2267 2269 2273
2657 2659 2663
2687 2689 2693
3251 3253 3257
3461 3463 3467
3527 3529 3533
3671 3673 3677
3917 3919 3923
4001 4003 4007
4127 4129 4133
4517 4519 4523
4637 4639 4643
4787 4789 4793
4931 4933 4937
4967 4969 4973
5231 5233 5237
5477 5479 5483
</pre>
 
Line 1,132 ⟶ 1,571:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
var c = Int.primeSieve(5505, false)
9,476

edits