Safe and Sophie Germain primes: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(6 intermediate revisions by 6 users not shown)
Line 127:
1049 1103 1223 1229 1289 1409 1439 1451 1481 1499
</pre>
 
=={{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 SophieGermainPrimes(Memo: TMemo);
var I,Cnt: integer;
var S: string;
begin
Cnt:=0;
S:='';
for I:=0 to high(integer) do
if IsPrime(I) then
if IsPrime(2 * I + 1) then
begin
Inc(Cnt);
S:=S+Format('%5D',[I]);
if Cnt>=50 then break;
If (Cnt mod 5)=0 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
Memo.Lines.Add('Count = '+IntToStr(Cnt));
end;
 
</syntaxhighlight>
{{out}}
<pre>
2 3 5 11 23
29 41 53 83 89
113 131 173 179 191
233 239 251 281 293
359 419 431 443 491
509 593 641 653 659
683 719 743 761 809
911 953 1013 1019 1031
1049 1103 1223 1229 1289
1409 1439 1451 1481 1499
Count = 50
Elapsed Time: 2.520 ms.
 
</pre>
 
 
=={{header|Factor}}==
Line 365 ⟶ 431:
1,049 1,103 1,223 1,229 1,289 1,409 1,439 1,451 1,481 1,499
</pre>
 
=={{header|J}}==
 
<syntaxhighlight lang=J> 5 10$(#~ 1 2&p. e. ])p:i.1e5
2 3 5 11 23 29 41 53 83 89
113 131 173 179 191 233 239 251 281 293
359 419 431 443 491 509 593 641 653 659
683 719 743 761 809 911 953 1013 1019 1031
1049 1103 1223 1229 1289 1409 1439 1451 1481 1499</syntaxhighlight>
 
=={{header|jq}}==
Line 412 ⟶ 487:
683 719 743 761 809 911 953 1013 1019 1031
1049 1103 1223 1229 1289 1409 1439 1451 1481 1499
</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that generate the pairs below n */
sg_s_pairs(n):=block(
L:makelist([i,2*i+1],i,1,n),
L1:[],
for i from 1 thru length(L) do if map(primep,L[i])=[true,true] then push(L[i],L1),
reverse(L1))$
 
/* Test case */
/* The first of the pairs is a Sophie Germain pair, first element of the pairs must be extracted */
map(first,sg_s_pairs(1500));
</syntaxhighlight>
{{out}}
<pre>
[2,3,5,11,23,29,41,53,83,89,113,131,173,179,191,233,239,251,281,293,359,419,431,443,491,509,593,641,653,659,683,719,743,761,809,911,953,1013,1019,1031,1049,1103,1223,1229,1289,1409,1439,1451,1481,1499]
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/strutils
 
func isPrime(n: Natural): 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
 
iterator sophieGermainPrimes(): int =
var n = 2
while true:
if isPrime(n) and isPrime(2 * n + 1):
yield n
inc n
 
echo "First 50 Sophie Germain primes:"
var count = 0
for n in sophieGermainPrimes():
inc count
stdout.write align($n, 4)
stdout.write if count mod 10 == 0: '\n' else: ' '
if count == 50: break
</syntaxhighlight>
 
{{out}}
<pre>First 50 Sophie Germain primes:
2 3 5 11 23 29 41 53 83 89
113 131 173 179 191 233 239 251 281 293
359 419 431 443 491 509 593 641 653 659
683 719 743 761 809 911 953 1013 1019 1031
1049 1103 1223 1229 1289 1409 1439 1451 1481 1499
</pre>
 
Line 491 ⟶ 624:
done...
</pre>
 
=={{header|Quackery}}==
 
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ temp put [] 0
[ 1+
dup isprime until
dup 2 * 1+ isprime until
dup dip join
over size temp share = until ]
drop
temp release ] is sgprimes ( n --> [ )
 
50 sgprimes witheach [ echo sp ]</syntaxhighlight>
 
{{out}}
 
<pre>2 3 5 11 23 29 41 53 83 89 113 131 173 179 191 233 239 251 281 293 359 419 431 443 491 509 593 641 653 659 683 719 743 761 809 911 953 1013 1019 1031 1049 1103 1223 1229 1289 1409 1439 1451 1481 1499 </pre>
 
=={{header|Raku}}==
Line 538 ⟶ 690:
1049 1103 1223 1229 1289 1409 1439 1451 1481 1499
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49g}}
≪ DUP + 1 + ISPRIME?
≫ '<span style="color:blue">SOPHIE?</span>' STO
≪ → function count
≪ { } 2
'''WHILE''' OVER SIZE count < '''REPEAT '''
'''IF''' DUP function EVAL '''THEN''' SWAP OVER + SWAP '''END'''
NEXTPRIME
'''END'''
DROP
≫ ≫ '<span style="color:blue">FIRSTSEQ</span>' STO
 
≪ <span style="color:blue">SOPHIE?</span> ≫ 50 <span style="color:blue">FIRSTSEQ</span>
{{out}
<pre>
1: {2 3 5 11 23 29 41 53 83 89 113 131 173 179 191 233 239 251 281 293 359 419 431 443 491 509 593 641 653 659 683 719 743 761 809 911 953 1013 1019 1031 1049 1103 1223 1229 1289 1409 1439 1451 1481 1499}
</pre>
 
Line 555 ⟶ 727:
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./seq" for Lst
import "./fmt" for Fmt
 
Line 572 ⟶ 742:
}
System.print("The first 50 Sophie Germain primes are:")
for (chunk in Lst.chunks(sgp, 10)) Fmt.printtprint("$,5d", chunksgp, 10)</syntaxhighlight>
 
{{out}}
9,476

edits