Data update

This commit is contained in:
Ingy döt Net 2023-12-16 21:33:55 -08:00
parent 35bcdeebf8
commit 74c69a0df6
2427 changed files with 31826 additions and 3468 deletions

View file

@ -0,0 +1,42 @@
#include <bitset>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <vector>
const uint32_t limit = 1'000'000'000;
std::bitset<limit + 1> primes;
void sieve_primes(uint32_t limit) {
primes.set();
primes.reset(0); primes.reset(1);
for ( uint32_t p = 2; p * p <= limit; ++p ) {
if ( primes.test(p) ) {
for ( uint32_t i = p * p; i <= limit; i += p ) {
primes.reset(i);
}
}
}
}
int main() {
sieve_primes(limit);
uint32_t target = 10;
uint32_t count = 0;
bool last = false;
bool first = true;
for ( uint32_t index = 5; index <= limit; index += 2 ) {
last = first;
first = primes[index];
if ( last && first ) {
count += 1;
}
if ( index + 1 == target ) {
std::cout << std::setw(8) << count << " twin primes below " << index + 1 << std::endl;
target *= 10;
}
}
}

View file

@ -0,0 +1,31 @@
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
.

View file

@ -1,9 +1,6 @@
tp=: 3 : '+/ (*. _2&(|.!.0)) 1 p: i. y'
tp=: (_2 +/@:= 2 -/\ i.&.(p:inv))"0
NB. 3 : '' explicitly define a "monad" (a one-argument function)
NB. i. y list integers up to the provided argument
NB. 1 p: create list of 0s, 1s where those ints are prime
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)
NB. i.&.(p:inv) generate a list of primes below the given limit
NB. 2 -/\ pairwise subtract adjacent primes (create a list of differences)
NB. _2 +/@:= compare these differences to -2, and
NB. sum up the resulting boolean list (get the number of twin pairs)

View file

@ -0,0 +1,39 @@
import java.util.BitSet;
public final class TwinPrimes {
public static void main(String[] args) {
final int limit = 1_000_000_000;
sievePrimes(limit);
int target = 10;
int count = 0;
boolean last = false;
boolean first = true;
for ( int index = 5; index <= limit; index += 2 ) {
last = first;
first = primes.get(index);
if ( last && first ) {
count += 1;
}
if ( index + 1 == target ) {
System.out.println(String.format("%8d%s%d", count, " twin primes below ", index + 1));
target *= 10;
}
}
}
private static void sievePrimes(int aLimit) {
primes = new BitSet(aLimit + 1);
primes.set(2, aLimit + 1);
for ( int i = 2; i <= Math.sqrt(aLimit); i = primes.nextSetBit(i + 1) ) {
for ( int j = i * i; j <= aLimit; j += i ) {
primes.clear(j);
}
}
}
private static BitSet primes;
}

View file

@ -0,0 +1,25 @@
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)

View file

@ -0,0 +1,20 @@
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)

View file

@ -1,27 +0,0 @@
(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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>
<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>
<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>
<span style="color: #000000;">p</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- a prime, &lt;= maxp</span>
<span style="color: #000000;">prev_p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
<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>
<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>
<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>
<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>
<span style="color: #000000;">prev_p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span>
<span style="color: #000000;">pn</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<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: #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>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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>
<!--

View file

@ -4,4 +4,5 @@ use Math::Primesieve;
my $p = Math::Primesieve.new;
printf "Twin prime pairs less than %14s: %s\n", comma(10**$_), comma $p.count(10**$_, :twins) for 1 .. 10;
printf "Twin prime pairs less than %17s: %s\n", comma(10**$_), comma $p.count(10**$_, :twins) for 1 .. 12;
say (now - INIT now).round(.01) ~ ' seconds';