Twin primes: Difference between revisions
m
→{{header|Wren}}: Minor tidy
(→J: more memory efficient and ~25 times faster) |
m (→{{header|Wren}}: Minor tidy) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 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.
<!--
with javascript_semantics
atom t0 = time()
function twin_primes(integer maxp, bool both=true)
integer n = 0, -- result
pn = 2, -- next prime index
p, -- a prime, <= maxp
prev_p = 2
while true do
p = get_prime(pn)
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
pn += 1
end while
return n
end function
integer mp = 6 -- prompt_number("Enter limit:")
printf(1,"Twin prime pairs less than %,d: %,d\n",{mp,twin_primes(mp)})
printf(1,"Twin prime pairs less than %,d: %,d\n",{mp,twin_primes(mp,false)})
for p=1 to 9 do
integer p10 = power(10,p)
printf(1,"Twin prime pairs less than %,d: %,d\n",{p10,twin_primes(p10)})
end for
?elapsed(time()-t0)
▲<!--</syntaxhighlight>-->
</syntaxhighlight>
{{out}}
<pre>
Line 1,414 ⟶ 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,561 ⟶ 1,602:
my $p = Math::Primesieve.new;
printf "Twin prime pairs less than %
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
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,910 ⟶ 1,955:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
import "./fmt" for Fmt
var c = Int.primeSieve(1e8-1, false)
|