示例#1
0
int main() {

    int i;
    int prime;

    primegen pg;
    primegen_init(&pg);

    for(i = 0; i < 10001; i++)
        prime = primegen_next(&pg);

    printf("%d\n", prime);

    return 0;
}
示例#2
0
/****************************************************************************
 * Break down the number into prime factors using DJB's sieve code, which
 * is about 5 to 10 times faster than the Seive of Eratosthenes.
 *
 * @param number
 *		The integer that we are factoring. It can be any value up to 64 bits
 *		in size.
 * @param factors
 *		The list of all the prime factors, zero terminated.
 * @param non_factors
 *		A list of smallest numbers that aren't prime factors. We return
 *		this because we are going to use prime non-factors for finding
 *		interesting numbers.
 ****************************************************************************/
unsigned
sieve_prime_factors(uint64_t number, PRIMEFACTORS factors, PRIMEFACTORS non_factors, double *elapsed)
{
	primegen pg;
	clock_t start;
	clock_t stop;
	uint64_t prime;
	uint64_t max;
	unsigned factor_count = 0;
	unsigned non_factor_count = 0;

	/*
	 * We only need to seive up to the square-root of the target number. Only
	 * one prime factor can be bigger than the square root, so once we find
	 * all the other primes, the square root is the only one left.
	 * Note: you have to link to the 'm' math library for some gcc platforms.
	 */
	max = (uint64_t)sqrt(number + 1.0);

	/*
	 * Init the DJB primegen library.
	 */
	primegen_init(&pg);
  
	/*
	 * Enumerate all the primes starting with 2
	 */
	start = clock();
	for (;;) {

		/* Seive the next prime */
		prime = primegen_next(&pg);

		/* If we've reached the square root, then that's as far as we need
		 * to go */
		if (prime > max)
			break;

		/* If this prime is not a factor (evenly divisible with no remainder)
		 * then loop back and get the next prime */
		if ((number % prime) != 0) {
			if (non_factor_count < 12)
				non_factors[non_factor_count++] = prime;
			continue;
		}
		
		/* Else we've found a prime factor, so add this to the list of primes */
		factors[factor_count++] = prime;
		
		/* At the end, we may have one prime factor left that's bigger than the
		 * sqrt. Therefore, as we go along, divide the original number
		 * (possibly several times) by the prime factor so that this large
		 * remaining factor will be the only one left */
		while ((number % prime) == 0)
			number /= prime;

		/* exit early if we've found all prime factors. comment out this
		 * code if you want to benchmark it */
		if (number == 1 && non_factor_count > 10)
			break;
	}

	/*
	 * See if there is one last prime that's bigger than the square root.
	 * Note: This is the only number that can be larger than 32-bits in the
	 * way this code is written.
	 */
	if (number != 1)
		factors[factor_count++] = number;
	
	/*
	 * Zero terminate the results.
	 */
	factors[factor_count] = 0;
	non_factors[non_factor_count] = 0;

	/*
	 * Since prime factorization takes a long time, especially on slow
	 * CPUs, we benchmark it to keep track of performance.
	 */
	stop = clock();
	if (elapsed)
		*elapsed = ((double)stop - (double)start)/(double)CLOCKS_PER_SEC;

	/* should always be at least 1, because if the number itself is prime,
	 * then that's it's only prime factor */
	return factor_count;
}