Safe and Sophie Germain primes: Difference between revisions

m
m (Add link to OEIS)
m (→‎{{header|Wren}}: Minor tidy)
 
(9 intermediate revisions by 9 users not shown)
Line 17:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
Line 33:
I ++cnt == 50
L.break
print()</langsyntaxhighlight>
 
{{out}}
Line 42:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find some Sophie Germain primes: primes p such that 2p + 1 is prime #
PR read "primes.incl.a68" PR
[]BOOL prime = PRIMESIEVE 10 000; # hopefully, enough primes #
Line 54:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 66:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">sophieG?: function [p][
and? [prime? p][prime? 1 + 2*p]
]
Line 79:
 
loop split.every:10 sophieGermaines 'a ->
print map a => [pad to :string & 4]</langsyntaxhighlight>
 
{{out}}
Line 90:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SAFE_AND_SOPHIE_GERMAIN_PRIMES.AWK
BEGIN {
Line 117:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 126:
683 719 743 761 809 911 953 1013 1019 1031
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}}==
{{works with|Factor|0.99 2022-04-03}}
<syntaxhighlight lang="factor">USING: lists lists.lazy math math.primes math.primes.lists prettyprint ;
 
50 lprimes [ 2 * 1 + prime? ] lfilter ltake [ . ] leach</syntaxhighlight>
{{out}}
<pre>
2
3
5
...
1451
1481
1499
</pre>
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">c:=1;
n:=3;
!!2;
Line 138 ⟶ 220:
fi;
n:+2;
od;</langsyntaxhighlight>
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">function isprime(n as integer) as boolean
if n < 2 then return false
if n < 4 then return true
Line 168 ⟶ 250:
if c mod 10 = 0 then print
end if
wend</langsyntaxhighlight>
{{out}}<pre>2 3 5 11 23 29 41 53 83 89
113 131 173 179 191 233 239 251 281 293
Line 176 ⟶ 258:
 
==={{header|GW-BASIC}}===
<langsyntaxhighlight lang="gwbasic">10 PRINT "2 ";
20 C = 1
30 N = 3
Line 204 ⟶ 286:
270 WEND
280 Z = 1
290 RETURN</langsyntaxhighlight>
 
==={{header|BASIC256}}===
<langsyntaxhighlight lang="freebasic">function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
Line 234 ⟶ 316:
end if
end while
end</langsyntaxhighlight>
 
==={{header|PureBasic}}===
<langsyntaxhighlight PureBasiclang="purebasic">Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
Line 274 ⟶ 356:
Wend
Input()
CloseConsole()</langsyntaxhighlight>
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang="freebasic">sub isPrime(v)
if v < 2 then return False : fi
if mod(v, 2) = 0 then return v = 2 : fi
Line 304 ⟶ 386:
endif
wend
end</langsyntaxhighlight>
 
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 338 ⟶ 420:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 349 ⟶ 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 356 ⟶ 447:
See e.g. [[Find_adjacent_primes_which_differ_by_a_square_integer#jq]]
for suitable implementions of `is_prime/0` and `primes/0` as used here.
<langsyntaxhighlight lang="jq">limit(50; primes | select(2*. + 1|is_prime))</langsyntaxhighlight>
{{out}}
<pre>
Line 369 ⟶ 460:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
for (i, p) in enumerate(filter(x -> isprime(2x + 1), primes(1500)))
print(lpad(p, 5), i % 10 == 0 ? "\n" : "")
end
</langsyntaxhighlight>{{out}}
<pre>
2 3 5 11 23 29 41 53 83 89
Line 383 ⟶ 474:
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">nextSafe[n_] :=
NestWhile[NextPrime, n + 1, ! (PrimeQ[2 # + 1] && PrimeQ[#]) &]
Labeled[Grid[Partition[NestList[nextSafe, 2, 49], 10],
Alignment -> {Right,
Baseline}], "First 50 Sophie Germain primes:", Top]</langsyntaxhighlight>
 
{{out}}<pre>
Line 396 ⟶ 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>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">issg(n)=if(isprime(n)&&isprime(1+2*n),1,0)
c = 0
n = 2
while(c<50,if(issg(n),print(n);c=c+1);n=n+1)</langsyntaxhighlight>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Safe_and_Sophie_Germain_primes
Line 414 ⟶ 563:
my @want;
forprimes { is_prime(2 * $_ + 1) and (50 == push @want, $_)
and print("@want\n" =~ s/.{65}\K /\n/gr) + exit } 2, 1e9;</langsyntaxhighlight>
{{out}}
<pre>
Line 423 ⟶ 572:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">sophie_germain</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
Line 437 ⟶ 586:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<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;">"First 50: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">),</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 444 ⟶ 593:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
print("working...")
row = 0
Line 467 ⟶ 616:
print(Sophie)
print("done...")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 475 ⟶ 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}}==
<syntaxhighlight lang="raku" perl6line>put join "\n", (^∞ .grep: { .is-prime && ($_*2+1).is-prime } )[^50].batch(10)».fmt: "%4d";</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 11 23 29 41 53 83 89
Line 486 ⟶ 654:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." +nl
Line 511 ⟶ 679:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 522 ⟶ 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>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">^Inf -> lazy.grep{|p| all_prime(p, 2*p + 1) }.first(50).slices(10).each{
.join(', ').say
}</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|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./seq" for Lst
import "./fmt" for Fmt
 
Line 543 ⟶ 742:
}
System.print("The first 50 Sophie Germain primes are:")
for (chunk in Lst.chunks(sgp, 10)) Fmt.printtprint("$,5d", chunksgp, 10)</langsyntaxhighlight>
 
{{out}}
Line 556 ⟶ 755:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[for I:= 2 to sqrt(N) do
Line 573 ⟶ 772:
N:= N+1;
until Count >= 50;
]</langsyntaxhighlight>
 
{{out}}
9,477

edits