コード例 #1
0
ファイル: count_primes.c プロジェクト: joshblum/project1
// 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;
}
コード例 #2
0
ファイル: univht.c プロジェクト: DhruvRelwani/GamesmanClassic
/* Finds random prime between number and 2*number */
unsigned long int find_next_random_prime(unsigned long int number) {

	unsigned long int candidate;

	do {

		candidate = number + rand() % number;

	} while (!prime_p(candidate));

	return candidate;

}
コード例 #3
0
ファイル: qcn.c プロジェクト: macssh/macssh
double
qcn_estimate (mpz_t d)
{
#define P_LIMIT  132000

  double  h;
  unsigned long  p;

  /* p=2 */
  h = sqrt (-mpz_get_d (d)) / M_PI
    * 2.0 / (2.0 - mpz_kronecker_ui (d, 2));

  if (mpz_cmp_si (d, -3) == 0)       h *= 3;
  else if (mpz_cmp_si (d, -4) == 0)  h *= 2;

  for (p = 3; p < P_LIMIT; p += 2)
    if (prime_p (p))
      h *= (double) p / (double) (p - mpz_kronecker_ui (d, p));

  return h;
}
コード例 #4
0
ファイル: count_primes.c プロジェクト: joshblum/project1
// 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;
}