示例#1
0
文件: main.cpp 项目: NFA/euler
int main(int argc, char *argv[]) {
  std::tuple<int, int, int> max{0,0,0};
  std::vector<int> primes1000{2};
  euler::prime_generator<int> primeg;
  while (*primeg < 1000) {
    primes1000.push_back(*primeg);
    ++primeg;
  }


  //#pragma omp parallel for
  for (int a = -999; a < 1000; ++a) {
    // optimization: b is a prime, 10x speed up
    //for (int b = -999; b < 1000; ++b) {
    for(auto& b: primes1000) {
      int num_primes = count_primes(a, b);

      if (num_primes > std::get<0>(max)) {
        max = {num_primes, a, b};
      }
    }
  }
  std::cout << "(a = " << std::get<1>(max) << ", b = " << std::get<2>(max)
    << ") generated " << std::get<0>(max) << " primes." << std::endl;
  std::cout << "a * b = " << std::get<1>(max) * std::get<2>(max) << std::endl;
  return 0;
}
示例#2
0
/**
 * Returns an array p^(floor(log_p B)) for all primes p <= B.
 * @return An array p^(floor(log_p B)) for all primes p <= B.
 *         Caller must free the returned array.
 * @param w is the number of prime powers.
 */
uint32_t* mpz_prime_powers(int* w, const uint32_t B) {
  mpz_t p; // prime
  mpz_t pp; // prime power
  int ppsi; // prime powers index
  unsigned long e; // exponent
  unsigned long exps[32]; // exponents
  int b;
  int i;
  double dB;
  uint32_t* prime_powers;

  mpz_init(p);
  mpz_init(pp);

  *w = count_primes(B);
  prime_powers = (uint32_t*)malloc(*w * sizeof(uint32_t));

  // check the base cases
  if (B == 0 || B == 1) {
    return prime_powers;
  }

  // size of B
  b  = msb_u32(B) + 1;

  // compute the i^th root of B, from 2 to log2(B)-1
  dB = (double)B;
  exps[0] = 0;
  exps[1] = 0;
  for (i = 2;  i < b;  i ++) {
    exps[i] = (unsigned long)floor(pow(dB, 1.0/((double)i)));
  }

  // start with p=2
  prime_powers[0] = 1U << (b-1);
  ppsi = 1;

  // let p=3 be the first odd prime
  mpz_set_ui(p, 3);
  e = b-1;
  while (ppsi < *w) {
    // reduce the exponent as appropriate
    while (e >= 2 && mpz_cmp_ui(p, exps[e]) > 0) {
      e --;
    }

    // compute the prime power
    mpz_pow_ui(pp, p, e);
    prime_powers[ppsi] = mpz_get_ui(pp);
    ppsi ++;

    // move to the next prime
    mpz_nextprime(p, p);
  }

  mpz_clear(pp);
  mpz_clear(p);

  return prime_powers;
}
示例#3
0
文件: 051.c 项目: ilyak/euler
static bool test_family(unsigned n)
{
	const unsigned target = 8;
	unsigned family[10];

	for (unsigned i1 = 0, t1 = n; t1 > 0; i1++, t1 /= 10) {
		unsigned d = t1 % 10;

		if (d <= 10 - target) {
			get_family_1(n, i1, family);

			if (count_primes(family) == target)
				return true;

			/* dirty hack: assume we have only one 3 digit combination per number */
			unsigned i3 = 0;

			for (unsigned i2 = i1 + 1, t2 = t1 / 10; t2 > 0;
					i2++, t2 /= 10) {
				if (t2 % 10 == d) {
					get_family_2(n, i1, i2, family);

					if (count_primes(family) == target)
						return true;

					if (i3) {
						get_family_3(n, i1, i2, i3, family);

						if (count_primes(family) == target)
							return true;
					}
					else {
						i3 = i2;
					}
				}
			}
		}
	}

	return false;
}
示例#4
0
int
main(int argc, char *argv[])
{
   uint64_t  max = argc > 1 ? atol(argv[1]) : 1000000000ull;
   char     *bitmap = calloc(max/8 + 1, 1);

   calc_primes(bitmap, max);

   printf("%ld\n", count_primes(bitmap, max));

   free(bitmap);
   return EXIT_SUCCESS;
}
int main (int argc, char ** argv) {
	if (argc != 2) {
		fprintf (stderr, "usage: %s <N>\n", argv[0]);
		return 1;
	}

	int N = atoi (argv[1]);

	bool * sieve = eratosthenes_sieve (PRIMES_PRODUCT_LIMIT);

	size_t primes_count = count_primes (sieve, N);

	if (primes_count == 0) {
		printf ("0\n");
		return 0;
	}

	int primes[primes_count];
	int powers[primes_count];

	fill_factor_arrays (primes, powers, sieve, primes_count);

	free (sieve);

	linked_list_t * pseudo_fortunates = linked_list_create ();

	int prime = 0;

	while ((prime = next_prime (primes, powers, primes_count, N)) > 0) {
		int val = find_pseudo_fortunate (prime);

		linked_list_add_sorted (pseudo_fortunates, copy_int (val), int_cmp, true);
	}

	int sum = linked_list_sum_int (pseudo_fortunates);

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

	linked_list_free (pseudo_fortunates);

	return 0;
}
示例#6
0
int
main(int argc, char *argv[])
{
   uint64_t  max = argc > 1 ? atol(argv[1]) : 1000000000ull;
   char     *bitmap = calloc(max/30 + sqrtl(max) + 16*8, 1);
   uint64_t  adj_max;
   int       a_i;

   /* adjust max to a multiple of 2,3,5 */
   for (adj_max = (max - 1) / 30 * 30 + 1, a_i = 0;
        adj_max + diff[a_i%8] <= max;
        adj_max += diff[a_i++%8])
      ;

   calc_primes(bitmap, adj_max);

   printf("%ld\n", count_primes(bitmap, adj_max / 30 * 8 + num2bit(adj_max)) + 3);

   free(bitmap);
   return EXIT_SUCCESS;
}