Exemple #1
0
// Helper method that finds all "small" primes -- primes in [0, 2^32).
static sieve_t* find_small_primes(void) {
    int64_t upper_bound = (int64_t)((double)1.42 * ((int64_t)1 << 31));
    sieve_t *sieve = create_sieve(upper_bound);
    if (NULL == sieve) {
        fprintf(stderr, "Failed to create SMALL_PRIMES sieve of length %"PRId64".\n"\
                "This failure can occur if there is insufficient physical memory on the system.\n"\
                "Aborting.\n", upper_bound);
        exit(1);
    }

    init_sieve(sieve);

    mark_composite(sieve, 0);
    mark_composite(sieve, 1);

    // Scan the entries of the sieve from 2 to UPPER_BOUND
    for (int64_t i = 2; i < upper_bound; ++i) {
        tbassert(trialdiv_prime_p(i) == prime_p(sieve, i),
                 "Incorrect primality recorded for %"PRId64" (%d vs %d)\n",
                 i, trialdiv_prime_p(i), prime_p(sieve, i));

        // Skip any I marked as composite
        if (!prime_p(sieve, i)) {
            continue;
        }

        // At this point, I is prime.
        // Mark all multiples of I as composite.
        for (int64_t j = 2; i * j < upper_bound; ++j) {
            mark_composite(sieve, i * j);
        }
    }

    return sieve;
}
Exemple #2
0
Fichier : 111.c Projet : pete/euler
int main(int argc, char **argv)
{
	char maxima[10];
	char dc[10];
	int64_t i, sum = 0;
	int j;
	char c;

	for(i = 0; i < 10; i++)
		maxima[i] = 0;

	init_sieve();
	for(i = next_prime((PMAX/10)-1); i < PMAX && i > 0; i = next_prime(i)) {
		dcount(i, dc);
		for(j = 0; j < 10; j++)
			if(dc[j] > maxima[j])
				maxima[j] = dc[j];
	}
	for(j = 0; j < 10; j++) {
		c = j == 9 ? '\n' : ',';
		printf("%d%c", maxima[j], c);
	}

	for(i = next_prime((PMAX/10)-1); i < PMAX && i > 0; i = next_prime(i)) {
		dcount(i, dc);
		for(j =  0; j < 10; j++)
			if(dc[j] == maxima[j])
				sum += i;
	}

	printf("%ld\n", sum);
	return 0;
}
Exemple #3
0
int main(int argc, char** argv) {
	if (argc != 2) {
		fprintf(stderr, "\nInsufficient number of command line arguments.  Exiting...\n");
		return 0;
	}

	int limit = atoi(argv[1]);
	sieve_t sieve;

	init_sieve(&sieve, limit);
	sundaram_find_primes(&sieve, limit);
	print_as_list(sieve, limit);
//	uninit_sieve(&sieve);
	return 0;
}
Exemple #4
0
// Helper function for COUNT_PRIMES_IN_INTERVAL() to count the number
// of primes in [START, START+LENGTH) where 0 < LENGTH <=
// MAX_SIEVE_LENGTH.  Returns the number of primes in [START,
// START+LENGTH).
//
//   START -- The low endpoint of the interval.
//
//   LENGTH -- The length of the interval.
//
//   SMALL_PRIMES -- Sieve recording all primes in [0,2^32).
//
static int64_t count_primes_in_interval_helper(int64_t start, int64_t length,
        const sieve_t* small_primes) {
    int64_t num_primes;

    // Create LARGE_PRIMES structures to record the primes in
    // [START,START+LENGTH), where index I in LARGE_PRIMES corresponds
    // to the integer LOW+I.
    sieve_t *large_primes = create_sieve(length);
    if (NULL == large_primes) {
        fprintf(stderr, "Failed to create LARGE_PRIMES sieve of length %"PRId64".\n"\
                "This can happen if there is insufficient physical memory on the system.\n"\
                "Aborting.\n", length);
        exit(1);
    }

    init_sieve(large_primes);

    // Scan all of the potentially prime entries in the SMALL_PRIMES
    // sieve.
    for (int64_t p = 2; p < small_primes->length; ++p) {
        tbassert(trialdiv_prime_p(p) == prime_p(small_primes, p),
                 "Incorrect primality recorded for %"PRId64" in small_primes\n",
                 p);

        // Skip any entry in SMALL_PRIMES marked as composite.
        if (!prime_p(small_primes, p)) {
            continue;
        }

        // At this point, P is a prime.
        /* DEBUG_EPRINTF("p = %ld\n", p); */

        // Find index KP_INDEX of smallest multiple of <p> in range
        // [START,START+LENGTH).
        int64_t kp_index = start % p;
        if (0 != kp_index) {
            kp_index = p - kp_index;
        }
        if (start + kp_index == p) {
            kp_index += p;
        }

        /* DEBUG_EPRINTF("kp_index = %"PRId64"\n", kp_index); */

        // Mark all multiples of P in [KP_INDEX, KP_INDEX+LENGTH) as
        // composite.
        for ( ; kp_index < length; kp_index += p) {
            mark_composite(large_primes, kp_index);
        }
    }

    // Count number of primes in LARGE_PRIMES.
    num_primes = 0;
    for (int64_t i = 0; i < length; ++i) {
        tbassert(trialdiv_prime_p(i + start) == prime_p(large_primes, i),
                 "Incorrect primality recorded for %"PRId64" in large_primes (index %"PRId64")\n",
                 i + start, i);

        if (prime_p(large_primes, i)) {
            ++num_primes;
        }
    }

    // Free LARGE_PRIMES structure
    destroy_sieve(large_primes);

    return num_primes;
}