CalmoSoft primes: Difference between revisions
→{{header|C}}: Updated in line with Wren version, about 20% faster.
(→{{header|Go}}: Updated in line with Wren version, about 20% faster.) |
(→{{header|C}}: Updated in line with Wren version, about 20% faster.) |
||
Line 314:
=={{header|C}}==
{{libheader|GMP}}
Run time is
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
Line 321 ⟶ 322:
#include <stdint.h>
#include <locale.h>
#include <gmp.h>
#define MAX 50000000
typedef uint64_t u64;
if (n%2 == 0) return n == 2;▼
int *primeSieve(int limit, int *length) {
Line 373 ⟶ 361:
int calmoPrimes(int limit, int *primes, int len, int *sIndices, int *eIndices, u64 *sums, int *ilen) {
int i, j, temp, pc = len, longest = 0, ic = 0;
bool isEven;
u64 sum = 0, sum2;
mpz_t bsum;
mpz_init(bsum);
if (limit < MAX) {
for (i = 0; i < len; ++i) {
Line 386 ⟶ 377:
if (pc - i < longest) break;
if (i > 0) sum -= primes[i-1];
sum2 = sum;
for (j = pc - 1; j >= i; --j) {
Line 391 ⟶ 383:
if (temp < longest) break;
if (j < pc - 1) sum2 -= primes[j+1];
if (
mpz_set_ui(bsum, sum2);
if (mpz_probab_prime_p(bsum, 5) > 0) {
if (temp > longest) {
longest = temp;
|