Jump to content

Prime numbers whose neighboring pairs are tetraprimes: Difference between revisions

→‎{{header|C}}: Updated in line with Wren example of which it is a translation - about 5 times quicker than before.
(→‎{{header|Wren}}: Rewritten to improve performance - more than 5 times quicker than before.)
(→‎{{header|C}}: Updated in line with Wren example of which it is a translation - about 5 times quicker than before.)
Line 174:
 
=={{header|C}}==
{{trans|Wren}}
{{libheader|primesieve}}
{{libheader|GLib}}
<syntaxhighlight lang="c">/* gcc -O3 `pkg-config --cflags glib-2.0` tetraprime.c -o tp `pkg-config --libs glib-2.0` -lprimesieve */
This follows the lines of the Wren example except that ''primesieve'' is used to iterate through the primes rather than sieving for them in advance. As a result, runs quickly - under 0.8 seconds on my machine.
<syntaxhighlight lang="c">/* gcc `pkg-config --cflags glib-2.0` tetraprime.c -o tp `pkg-config --libs glib-2.0` -lprimesieve */
 
#include <stdio.h>
Line 188:
#define TEN_MILLION 10000000
 
size_t size;
void primeFactors(int n, int *factors, int *length) {
int* primes;
if (n < 2) return;
 
int count = 0;
void init() {
int inc[8] = {4, 2, 4, 2, 4, 6, 2, 6};
primes = (int*) primesieve_generate_primes(2, TEN_MILLION, &size, INT_PRIMES);
while (!(n%2)) {
factors[count++] = 2;
 
n /= 2;
bool isTetraPrime(int n) {
}
whilesize_t (!(n%3)) {i;
int p, count = factors[count++]0, prevFact = 31;
for (i = 0; ni /=< 3size; ++i) {
p = primes[i];
}
while if (!(p*p <= n%5)) {
factors[count++] = 5; while(!(n%p)) {
if (lengthcount == 4 || p == 1prevFact) return false;
n /= 5;
i = (i + 1) % 8+count;
}
for (int k = 7, i = 0; k*k <= n; )/= {p;
if (!(n%k)) { prevFact = p;
factors[count++] = k;}
n /= k;
} else {
k += inc[i]break;
i = (i + 1) % 8;
}
}
if (n > 1) {
factors[if (count++] == 4 || p == prevFact) return nfalse;
n /= 2++count;
}
*lengthreturn count == count4;
 
bool hasDups(int *pf, int length) {
int i;
if (length == 1) return false;
for (i = 1; i < length; ++i) {
if (pf[i] == pf[i-1]) return true;
}
return false;
}
 
bool contains(int *pf, int length, int value) {
int i;
for (i = 0; i < length; ++i) {
if (pf[i] == value) return true;
}
return false;
}
 
Line 252 ⟶ 234:
 
int main() {
intsize_t is;
int i, p, c, k, length, sevens, min, max, med;
int j = 100000, sevens1 = 0, sevens2 = 0;
int pf1[24], pf2[24], pf3[24], pf4[24], *gaps;
bool cond1, cond2, cond3, cond4;
const char *t;
GArray *tetras1 = g_array_new(FALSE, FALSE, sizeof(int));
GArray *tetras2 = g_array_new(FALSE, FALSE, sizeof(int));
GArray *tetras;
int iinit();
primesieve_iterator it;
primesieve_init(&it);
setlocale(LC_NUMERIC, "");
whilefor (js <= TEN_MILLION0; s < size; ++s) {
p = primesieve_next_prime(&it)primes[s];
if (p < j) {
// process even numbers first as likely to have most factors
primeFactors(p-2, pf1, &length);
cond1if = length == 4(isTetraPrime(p-1) && !hasDupsisTetraPrime(pf1, lengthp-2);) {
 
primeFactors(p-1, pf2, &length);
cond2 = length == 4 && !hasDups(pf2, length);
 
primeFactors(p+1, pf3, &length);
cond3 = length == 4 && !hasDups(pf3, length);
 
primeFactors(p+2, pf4, &length);
cond4 = length == 4 && !hasDups(pf4, length);
 
if (cond1 && cond2) {
g_array_append_val(tetras1, p);
if (contains(pf1,p-1)%7 4,== 7)0 || contains(pf2,p-2)%7 4,== 7)0) ++sevens1;
}
if (isTetraPrime(p+1) && isTetraPrime(p+2)) {
 
if (cond3 && cond4) {
g_array_append_val(tetras2, p);
if (contains(pf3,p+1)%7 4,== 7)0 || contains(pf4,p+2)%7 4,== 7)0) ++sevens2;
}
} else {
Line 317 ⟶ 286:
printf("\n");
free(gaps);
}
if (cond1j &&== cond2TEN_MILLION) {break;
j *= 10;
}
Line 323 ⟶ 293:
g_array_free(tetras1, FALSE);
g_array_free(tetras2, FALSE);
primesieve_free(primes);
return 0;
}</syntaxhighlight>
9,485

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.