Safe and Sophie Germain primes: Difference between revisions

Content added Content deleted
(Added Sidef)
m (syntax highlighting fixup automation)
Line 17: Line 17:


=={{header|11l}}==
=={{header|11l}}==
<lang 11l>F is_prime(a)
<syntaxhighlight lang="11l">F is_prime(a)
I a == 2
I a == 2
R 1B
R 1B
Line 33: Line 33:
I ++cnt == 50
I ++cnt == 50
L.break
L.break
print()</lang>
print()</syntaxhighlight>


{{out}}
{{out}}
Line 42: Line 42:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find some Sophie Germain primes: primes p such that 2p + 1 is prime #
<syntaxhighlight lang="algol68">BEGIN # find some Sophie Germain primes: primes p such that 2p + 1 is prime #
PR read "primes.incl.a68" PR
PR read "primes.incl.a68" PR
[]BOOL prime = PRIMESIEVE 10 000; # hopefully, enough primes #
[]BOOL prime = PRIMESIEVE 10 000; # hopefully, enough primes #
Line 54: Line 54:
FI
FI
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 66: Line 66:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>sophieG?: function [p][
<syntaxhighlight lang="rebol">sophieG?: function [p][
and? [prime? p][prime? 1 + 2*p]
and? [prime? p][prime? 1 + 2*p]
]
]
Line 79: Line 79:


loop split.every:10 sophieGermaines 'a ->
loop split.every:10 sophieGermaines 'a ->
print map a => [pad to :string & 4]</lang>
print map a => [pad to :string & 4]</syntaxhighlight>


{{out}}
{{out}}
Line 90: Line 90:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SAFE_AND_SOPHIE_GERMAIN_PRIMES.AWK
# syntax: GAWK -f SAFE_AND_SOPHIE_GERMAIN_PRIMES.AWK
BEGIN {
BEGIN {
Line 117: Line 117:
return(1)
return(1)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 130: Line 130:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2022-04-03}}
{{works with|Factor|0.99 2022-04-03}}
<lang factor>USING: lists lists.lazy math math.primes math.primes.lists prettyprint ;
<syntaxhighlight lang="factor">USING: lists lists.lazy math math.primes math.primes.lists prettyprint ;


50 lprimes [ 2 * 1 + prime? ] lfilter ltake [ . ] leach</lang>
50 lprimes [ 2 * 1 + prime? ] lfilter ltake [ . ] leach</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 145: Line 145:


=={{header|Fermat}}==
=={{header|Fermat}}==
<lang fermat>c:=1;
<syntaxhighlight lang="fermat">c:=1;
n:=3;
n:=3;
!!2;
!!2;
Line 154: Line 154:
fi;
fi;
n:+2;
n:+2;
od;</lang>
od;</syntaxhighlight>


=={{header|BASIC}}==
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
==={{header|FreeBASIC}}===
<lang freebasic>function isprime(n as integer) as boolean
<syntaxhighlight lang="freebasic">function isprime(n as integer) as boolean
if n < 2 then return false
if n < 2 then return false
if n < 4 then return true
if n < 4 then return true
Line 184: Line 184:
if c mod 10 = 0 then print
if c mod 10 = 0 then print
end if
end if
wend</lang>
wend</syntaxhighlight>
{{out}}<pre>2 3 5 11 23 29 41 53 83 89
{{out}}<pre>2 3 5 11 23 29 41 53 83 89
113 131 173 179 191 233 239 251 281 293
113 131 173 179 191 233 239 251 281 293
Line 192: Line 192:


==={{header|GW-BASIC}}===
==={{header|GW-BASIC}}===
<lang gwbasic>10 PRINT "2 ";
<syntaxhighlight lang="gwbasic">10 PRINT "2 ";
20 C = 1
20 C = 1
30 N = 3
30 N = 3
Line 220: Line 220:
270 WEND
270 WEND
280 Z = 1
280 Z = 1
290 RETURN</lang>
290 RETURN</syntaxhighlight>


==={{header|BASIC256}}===
==={{header|BASIC256}}===
<lang freebasic>function isPrime(v)
<syntaxhighlight lang="freebasic">function isPrime(v)
if v < 2 then return False
if v < 2 then return False
if v mod 2 = 0 then return v = 2
if v mod 2 = 0 then return v = 2
Line 250: Line 250:
end if
end if
end while
end while
end</lang>
end</syntaxhighlight>


==={{header|PureBasic}}===
==={{header|PureBasic}}===
<lang PureBasic>Procedure isPrime(v.i)
<syntaxhighlight lang="purebasic">Procedure isPrime(v.i)
If v <= 1 : ProcedureReturn #False
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
ElseIf v < 4 : ProcedureReturn #True
Line 290: Line 290:
Wend
Wend
Input()
Input()
CloseConsole()</lang>
CloseConsole()</syntaxhighlight>


==={{header|Yabasic}}===
==={{header|Yabasic}}===
<lang freebasic>sub isPrime(v)
<syntaxhighlight lang="freebasic">sub isPrime(v)
if v < 2 then return False : fi
if v < 2 then return False : fi
if mod(v, 2) = 0 then return v = 2 : fi
if mod(v, 2) = 0 then return v = 2 : fi
Line 320: Line 320:
endif
endif
wend
wend
end</lang>
end</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
{{trans|Wren}}
{{trans|Wren}}
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 354: Line 354:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 372: Line 372:
See e.g. [[Find_adjacent_primes_which_differ_by_a_square_integer#jq]]
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.
for suitable implementions of `is_prime/0` and `primes/0` as used here.
<lang jq>limit(50; primes | select(2*. + 1|is_prime))</lang>
<syntaxhighlight lang="jq">limit(50; primes | select(2*. + 1|is_prime))</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 385: Line 385:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes
<syntaxhighlight lang="julia">using Primes


for (i, p) in enumerate(filter(x -> isprime(2x + 1), primes(1500)))
for (i, p) in enumerate(filter(x -> isprime(2x + 1), primes(1500)))
print(lpad(p, 5), i % 10 == 0 ? "\n" : "")
print(lpad(p, 5), i % 10 == 0 ? "\n" : "")
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
2 3 5 11 23 29 41 53 83 89
2 3 5 11 23 29 41 53 83 89
Line 399: Line 399:
</pre>
</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>nextSafe[n_] :=
<syntaxhighlight lang="mathematica">nextSafe[n_] :=
NestWhile[NextPrime, n + 1, ! (PrimeQ[2 # + 1] && PrimeQ[#]) &]
NestWhile[NextPrime, n + 1, ! (PrimeQ[2 # + 1] && PrimeQ[#]) &]
Labeled[Grid[Partition[NestList[nextSafe, 2, 49], 10],
Labeled[Grid[Partition[NestList[nextSafe, 2, 49], 10],
Alignment -> {Right,
Alignment -> {Right,
Baseline}], "First 50 Sophie Germain primes:", Top]</lang>
Baseline}], "First 50 Sophie Germain primes:", Top]</syntaxhighlight>


{{out}}<pre>
{{out}}<pre>
Line 415: Line 415:


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>issg(n)=if(isprime(n)&&isprime(1+2*n),1,0)
<syntaxhighlight lang="parigp">issg(n)=if(isprime(n)&&isprime(1+2*n),1,0)
c = 0
c = 0
n = 2
n = 2
while(c<50,if(issg(n),print(n);c=c+1);n=n+1)</lang>
while(c<50,if(issg(n),print(n);c=c+1);n=n+1)</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Safe_and_Sophie_Germain_primes
use strict; # https://rosettacode.org/wiki/Safe_and_Sophie_Germain_primes
Line 430: Line 430:
my @want;
my @want;
forprimes { is_prime(2 * $_ + 1) and (50 == push @want, $_)
forprimes { is_prime(2 * $_ + 1) and (50 == push @want, $_)
and print("@want\n" =~ s/.{65}\K /\n/gr) + exit } 2, 1e9;</lang>
and print("@want\n" =~ s/.{65}\K /\n/gr) + exit } 2, 1e9;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 439: Line 439:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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 453: Line 453:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 460: Line 460:


=={{header|Python}}==
=={{header|Python}}==
<lang python>
<syntaxhighlight lang="python">
print("working...")
print("working...")
row = 0
row = 0
Line 483: Line 483:
print(Sophie)
print(Sophie)
print("done...")
print("done...")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 493: Line 493:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>put join "\n", (^∞ .grep: { .is-prime && ($_*2+1).is-prime } )[^50].batch(10)».fmt: "%4d";</lang>
<syntaxhighlight lang="raku" line>put join "\n", (^∞ .grep: { .is-prime && ($_*2+1).is-prime } )[^50].batch(10)».fmt: "%4d";</syntaxhighlight>
{{out}}
{{out}}
<pre> 2 3 5 11 23 29 41 53 83 89
<pre> 2 3 5 11 23 29 41 53 83 89
Line 502: Line 502:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"
see "working..." +nl
see "working..." +nl
Line 527: Line 527:


see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 541: Line 541:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>^Inf -> lazy.grep{|p| all_prime(p, 2*p + 1) }.first(50).slices(10).each{
<syntaxhighlight lang="ruby">^Inf -> lazy.grep{|p| all_prime(p, 2*p + 1) }.first(50).slices(10).each{
.join(', ').say
.join(', ').say
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 557: Line 557:
{{libheader|Wren-seq}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "./math" for Int
<syntaxhighlight lang="ecmascript">import "./math" for Int
import "./seq" for Lst
import "./seq" for Lst
import "./fmt" for Fmt
import "./fmt" for Fmt
Line 572: Line 572:
}
}
System.print("The first 50 Sophie Germain primes are:")
System.print("The first 50 Sophie Germain primes are:")
for (chunk in Lst.chunks(sgp, 10)) Fmt.print("$,5d", chunk)</lang>
for (chunk in Lst.chunks(sgp, 10)) Fmt.print("$,5d", chunk)</syntaxhighlight>


{{out}}
{{out}}
Line 585: Line 585:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func IsPrime(N); \Return 'true' if N is a prime number
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
int N, I;
[for I:= 2 to sqrt(N) do
[for I:= 2 to sqrt(N) do
Line 602: Line 602:
N:= N+1;
N:= N+1;
until Count >= 50;
until Count >= 50;
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}