Twin primes: Difference between revisions

m
(New post without using any external libraries, in addition to an existing post which uses the "primesieve.hpp" external library.)
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 4 users not shown)
Line 695:
Under 1,000,000,000 there are 3,424,506 pairs of twin primes.
</pre>
 
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight lang=easylang>
fastfunc isprim num .
if num mod 2 = 0 and num > 2
return 0
.
i = 3
while i <= sqrt num
if num mod i = 0
return 0
.
i += 2
.
return 1
.
func count limit .
p2 = 1
p3 = 1
for i = 5 to limit
p3 = p2
p2 = p1
p1 = isprim i
if p3 = 1 and p1 = 1
cnt += 1
.
.
return cnt
.
n = 1
for i = 1 to 6
n *= 10
print "twin prime pairs < " & n & " : " & count n
.
</syntaxhighlight>
 
=={{header|F Sharp|F#}}==
Line 1,044 ⟶ 1,080:
 
=={{header|J}}==
<syntaxhighlight lang="j">tp=: 3(_2 +/@:= '+2 -/\ (*i. _2&.(|.!.0p:inv)) 1 p: i. y'"0
 
NB. i.&.(p:inv) generate a list of primes below the given limit
NB. 3 : '' explicitly define a "monad" (a one-argument function)
NB. i.2 y-/\ listpairwise integerssubtract upadjacent toprimes the(create a list providedof argumentdifferences)
NB. _2 +/@:= compare these differences to -2, and
NB. 1 p: create list of 0s, 1s where those ints are prime
NB. sum up the resulting boolean list (get the number of twin pairs)</syntaxhighlight>
NB. _2&(|.!.0) "shift" that list to the right by two, filling left side with 0
NB. (*. g) y create a "hook". "and" together the original and shifted lists
NB. the result will have a 1 only if that i, and i-2, are both prime
NB. +/ sum the and-ed list (get the number of twin pairs)</syntaxhighlight>
{{out}}
<pre> tp 10010 ^ >: i. 7
2 8 35 205 1224 8169 58980
8
 
tp 1000
35
NB. test larger limits, and get their time/space usage
tp 100000001e8
58980
timespacex 'tp 10000000'
2.37363 1.50996e8
tp 100000000
440312
timespacex 'tp 1000000001e8'
0.657576 2.01377e8
51.0851 1.20796e9</pre>
tp 1e9
3424506
timespacex 'tp 1e9'
8.59713 1.61071e9
</pre>
 
=={{header|jq}}==
Line 1,339 ⟶ 1,372:
The time complexity here is all about building a table of primes. It turns out that using the builtin get_prime() is actually faster
than using an explicit sieve (as per Delphi/Go/Wren) due to retaining all the intermediate 0s, not that I particularly expect this to win any performance trophies.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<syntaxhighlight lang="phix">
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
with javascript_semantics
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
atom t0 = time()
<span style="color: #008080;">function</span> <span style="color: #000000;">twin_primes</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">maxp</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">both</span><span style="color: #0000FF;">=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
function twin_primes(integer maxp, bool both=true)
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- result</span>
integer n = 0, -- result
<span style="color: #000000;">pn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- next prime index</span>
pn = 2, -- next prime index
<span style="color: #000000;">p</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- a prime, &lt;= maxp</span>
p, -- a prime, <= maxp
<span style="color: #000000;">prev_p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
prev_p = 2
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
while true do
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pn</span><span style="color: #0000FF;">)</span>
p = get_prime(pn)
<span style="color: #008080;">if</span> <span style="color: #000000;">both</span> <span style="color: #008080;">and</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">maxp</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if both and p>=maxp then exit end if
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">prev_p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
n += (prev_p = p-2)
<span style="color: #008080;">if</span> <span style="color: #0000FF;">(</span><span style="color: #008080;">not</span> <span style="color: #000000;">both</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">maxp</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if (not both) and p>=maxp then exit end if
<span style="color: #000000;">prev_p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span>
prev_p = p
<span style="color: #000000;">pn</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
pn += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end while
<span style="color: #008080;">return</span> <span style="color: #000000;">n</span>
return n
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end function
<span style="color: #004080;">integer</span> <span style="color: #000000;">mp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">6</span> <span style="color: #000080;font-style:italic;">-- prompt_number("Enter limit:")</span>
integer mp = 6 -- prompt_number("Enter limit:")
<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;">"Twin prime pairs less than %,d: %,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">twin_primes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mp</span><span style="color: #0000FF;">)})</span>
printf(1,"Twin prime pairs less than %,d: %,d\n",{mp,twin_primes(mp)})
<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;">"Twin prime pairs less than %,d: %,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">twin_primes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mp</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)})</span>
printf(1,"Twin prime pairs less than %,d: %,d\n",{mp,twin_primes(mp,false)})
<span style="color: #008080;">for</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</span>
for p=1 to 9 do
<span style="color: #004080;">integer</span> <span style="color: #000000;">p10</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
integer p10 = power(10,p)
<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;">"Twin prime pairs less than %,d: %,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">p10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">twin_primes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p10</span><span style="color: #0000FF;">)})</span>
printf(1,"Twin prime pairs less than %,d: %,d\n",{p10,twin_primes(p10)})
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
?elapsed(time()-t0)
<!--</syntaxhighlight>-->
</syntaxhighlight>
{{out}}
<pre>
Line 1,381 ⟶ 1,415:
"16.2s"
</pre>
=== using primesieve ===
 
Windows 64-bit only, unless you can find/make a suitable dll/so.<br>
Note that unlike the above this version of twin_primes() carries on from where it left off.
<syntaxhighlight lang="phix">
requires(WINDOWS)
requires(64,true)
include builtins/primesieve.e
atom t0 = time()
integer n = 0, p = 2, prev_p = 2
function twin_primes(integer maxp, bool both=true)
while true do
if both and p>=maxp then exit end if
n += (prev_p = p-2)
if (not both) and p>=maxp then exit end if
prev_p = p
p = primesieve_next_prime()
end while
return n
end function
for i=1 to 11 do
integer p10 = power(10,i)
printf(1,"Twin prime pairs less than %,d: %,d\n",{p10,twin_primes(p10)})
end for
?elapsed(time()-t0)
</syntaxhighlight>
{{out}}
Same as above, which it completes in 7.2s (and 1e10 in 1 min 6s), less the 6's and plus two more lines:
<pre>
Twin prime pairs less than 10: 2
Twin prime pairs less than 100: 8
Twin prime pairs less than 1,000: 35
Twin prime pairs less than 10,000: 205
Twin prime pairs less than 100,000: 1,224
Twin prime pairs less than 1,000,000: 8,169
Twin prime pairs less than 10,000,000: 58,980
Twin prime pairs less than 100,000,000: 440,312
Twin prime pairs less than 1,000,000,000: 3,424,506
Twin prime pairs less than 10,000,000,000: 27,412,679
Twin prime pairs less than 100,000,000,000: 224,376,048
"10 minutes and 25s"
</pre>
 
=={{header|PureBasic}}==
Line 1,528 ⟶ 1,602:
my $p = Math::Primesieve.new;
 
printf "Twin prime pairs less than %14s17s: %s\n", comma(10**$_), comma $p.count(10**$_, :twins) for 1 .. 1012;</syntaxhighlight>
say (now - INIT now).round(.01) ~ ' seconds';</syntaxhighlight>
{{out}}
<pre>Twin prime pairs less than 10: 2
Twin prime pairs less than 100: 8
Twin prime pairs less than 1,000: 35
Twin prime pairs less than 10,000: 205
Twin prime pairs less than 100,000: 1,224
Twin prime pairs less than 1,000,000: 8,169
Twin prime pairs less than 10,000,000: 58,980
Twin prime pairs less than 100,000,000: 440,312
Twin prime pairs less than 1,000,000,000: 3,424,506
Twin prime pairs less than 10,000,000,000: 27,412,679</pre>
Twin prime pairs less than 100,000,000,000: 224,376,048
Twin prime pairs less than 1,000,000,000,000: 1,870,585,220
6.89 seconds</pre>
 
=={{header|REXX}}==
Line 1,877 ⟶ 1,955:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
var c = Int.primeSieve(1e8-1, false)
9,479

edits