Double Twin Primes: Difference between revisions

Added Sidef
(Added Sidef)
 
(5 intermediate revisions by 5 users not shown)
Line 345:
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Classes,SysUtils,StdCtrls}}
Uses standard TList as FIFO to hold and test groups of four sequentail prime numbers.
 
<syntaxhighlight lang="Delphi">
 
 
function IsPrime(N: integer): boolean;
{Fast, optimised prime test}
var I,Stop: integer;
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));
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;
 
 
function GetNextPrime(var Start: integer): integer;
{Get the next prime number after Start}
{Start is passed by "reference," so the
{original variable is incremented}
begin
repeat Inc(Start)
until IsPrime(Start);
Result:=Start;
end;
 
procedure ShowDoubleTwinPrimes(Memo: TMemo);
{Find sets of four primes P1,P2,P3,P4, where}
{P2-P1=2 P4-P3=2 and P3-P2=4 }
{Use TList as FIFO to test all four-prime combinations}
var LS: TList;
var Start: integer;
begin
LS:=TList.Create;
try
Start:=0;
while true do
begin
{Put four primes in the list}
repeat LS.Add(Pointer(GetNextPrime(Start)))
until LS.Count=4;
if Integer(LS[3])>=1000 then break;
{Test if they are double twin prime}
if (Integer(LS[1])-Integer(LS[0])=2) and
(Integer(LS[3])-Integer(LS[2])=2) and
(Integer(LS[2])-Integer(LS[1])=4) then
begin
{Display the result}
Memo.Lines.Add(IntToStr(Integer(LS[0]))+' '+
IntToStr(Integer(LS[1]))+' '+
IntToStr(Integer(LS[2]))+' '+
IntToStr(Integer(LS[3])));
end;
{Delete the first prime}
LS.Delete(0);
end;
finally LS.Free; end;
end;
 
</syntaxhighlight>
{{out}}
<pre>
5 7 11 13
11 13 17 19
101 103 107 109
191 193 197 199
821 823 827 829
</pre>
 
 
=={{header|FreeBASIC}}==
Line 465 ⟶ 548:
'''Also works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
# Input: a positive integer
# return an array such that .[$i] is $i if $i is prime and false otherwise
# Output: an array, $a, of length .+1 such that
# $a[$i] is $i if $i is prime, and false otherwise.
def primeSieve:
# erase(i) sets .[i*j] to false for integral j > 1
def erase(i):
if .[i] then
reduce (range(2*i; (1 + length) /; i)) as $j (.; .[i * $j] = false)
else .
end;
Line 514 ⟶ 599:
printdt(1000)
</syntaxhighlight>{{out}} Same as C example.
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/strformat
 
func isPrime(n: Positive): bool =
if n < 2: return false
if (n and 1) == 0: return n == 2
if n mod 3 == 0: return n == 3
var k = 5
var delta = 2
while k * k <= n:
if n mod k == 0: return false
inc k, delta
delta = 6 - delta
result = true
 
echo "Double twin primes under 1000:"
for n in countup(3, 991, 2):
if isPrime(n) and isPrime(n + 2) and isPrime(n + 6) and isPrime(n + 8):
echo &"({n:>3}, {n+2:>3}, {n+6:>3}, {n+8:>3})"
</syntaxhighlight>
 
{{out}}
<pre>( 5, 7, 11, 13)
( 11, 13, 17, 19)
(101, 103, 107, 109)
(191, 193, 197, 199)
(821, 823, 827, 829)
</pre>
 
=={{header|Perl}}==
Line 641 ⟶ 755:
821, 823, 827, 829
</pre>
 
=={{header|Sidef}}==
 
<syntaxhighlight lang="ruby">1000.primes.each_cons(4, {|*a|
if (a.diffs == [2, 4, 2]) {
say a
}
})</syntaxhighlight>
{{out}}
<pre>
[5, 7, 11, 13]
[11, 13, 17, 19]
[101, 103, 107, 109]
[191, 193, 197, 199]
[821, 823, 827, 829]
</pre>
 
=={{header|V (Vlang)}}==
{{trans|Ring}}
<syntaxhighlight lang="Zig">
import math
 
fn main() {
limit := 1000
mut parr := []int{}
for n in 1..limit {
if is_prime(n) {parr << n}
}
for m in 1..parr.len - 3 {
if is_prime(parr[m]) && is_prime(parr[m + 1]) && is_prime(parr[m + 2]) && is_prime(parr[m + 3]) {
if parr[m + 1] - parr[m] == 2 && parr[m + 2] - parr[m + 1] == 4 && parr[m + 3] - parr[m + 2] == 2 {
println("${parr[m]} ${parr[m + 1]} ${parr[m + 2]} ${parr[m + 3]}")
}
}
}
}
 
fn is_prime(num i64) bool {
if num <= 1 {return false}
if num % 2 == 0 && num != 2 {return false}
for idx := 3; idx <= math.floor(num / 2) - 1; idx += 2 {
if num % idx == 0 {return false}
}
return true
}
</syntaxhighlight>
 
{{out}}
<pre>
5 7 11 13
11 13 17 19
101 103 107 109
191 193 197 199
821 823 827 829
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
2,747

edits