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

	int N = atoi (argv[1]);

	if (N < 3)
		return 1;

	bool * primes = eratosthenes_sieve (N);

	int max_sum = 0;
	int max_count = 0;

	for (int i = 2; i < N; i++) {
		if (!primes[i])
			continue;

		int count = 0;
		int sum = sum_primes (primes, &count, i, N);

		if (count > max_count) {
			max_sum = sum;
			max_count = count;
		}
	}

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

	free (primes);

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

	int N = atoi (argv[1]);

	if (N < 1 || N >= DIGITS_COUNT)
		return 1;

	int primes_limit = sqrt (power (10, N)) + 1;
	bool * primes = eratosthenes_sieve (primes_limit);

	char digits[DIGITS_COUNT];
	int digit = 0;

	for (int i = N; i > 0; i--)
		digits[digit++] = i + '0';

	digits[digit] = '\0';

	while (prev_permutation (digits)) {
		if (is_prime (atoi (digits), primes, primes_limit)) {
			printf ("%s\n", digits);
			break;
		}
	}

	free (primes);

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

	int N = atoi (argv[1]);

	if (N < 1)
		return 1;

	int lower_limit = power (10, N - 1);
	int upper_limit = lower_limit * 10 - 1;
	long multiplier = lower_limit * 10;

	bool * primes = eratosthenes_sieve (upper_limit + 1);
	char next_perm[N + 1];

	long max = 0;

	for (int start = lower_limit; start <= upper_limit; start++) {
		if (!primes[start])
			continue;

		sprintf (next_perm, "%d", start);

		while (next_permutation (next_perm)) {
			int next_val = atoi (next_perm);

			if (!primes[next_val])
				continue;

			int final_val = next_val + next_val - start;

			if (final_val > upper_limit)
				continue;

			if (!primes[final_val])
				continue;

			if (!is_permutation (next_val, final_val))
				continue;

			long seq_val = (((start * multiplier) + next_val) * multiplier) + final_val;

			if (seq_val > max)
				max = seq_val;
		}

	}

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

	free (primes);

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

	int N = atoi (argv[1]);

	if (N < 2)
		return 1;

	bignum_t * powers[(N - 1) * (N - 1)];
	size_t powers_count = 0;

	bool * primes = eratosthenes_sieve (N + 1);

	for (int a = 2; a <= N; a++) {
		bignum_t * power = NULL;

		for (int b = 2; b <= N; b++) {
			bignum_t * new_power = NULL;

			if (!power)
				new_power = bignum_get (a * a);
			else
				new_power = bignum_mult (power, a);

			if (b == 2)
				bignum_delete (power);

			int duplicate_at = -1;

			// Powers of a prime base cannot be duplicates
			if (!primes[a])
				for (size_t i = 0; i < powers_count; i++)
					if (bignum_cmp (powers[i], new_power) == 0) {
						duplicate_at = i;
						break;
					}

			if (duplicate_at == -1) {
				powers[powers_count++] = new_power;
				power = new_power;
			} else {
				bignum_delete (new_power);
				power = powers[duplicate_at];
			}
		}
	}

	printf ("%d\n", (int) powers_count);

	bignum_free_array (powers, powers_count);
	free (primes);

	return 0;
}
예제 #5
0
int main() {
    unsigned count;

    #ifdef TEST_ATKIN
        count = atkin_sieve();
    #elif TEST_SUNDARAM
        count = sundaram_sieve();
    #elif TEST_ERATOSTHENES
        count = eratosthenes_sieve();
    #elif TEST_FAST_ERATOSTHENES
        count = fast_eratosthenes_sieve();
    #endif

    printf("Found %d primes under %d\n", count, SIZE);

    return 0;
}
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;
}
예제 #7
0
파일: run.c 프로젝트: HSchroeder/ccan
int main(void)
{
	struct eratosthenes s;
	unsigned long n;

	/* This is how many tests you plan to run */
	plan_tests(2 * LIMIT);

	eratosthenes_init(&s);

	eratosthenes_sieve(&s, LIMIT);

	for (n = 0; n < LIMIT; n++) {
		ok_eq(eratosthenes_isprime(&s, n), test_isprime(n));
		ok_eq(eratosthenes_nextprime(&s, n), test_nextprime(&s, n));
	}

	eratosthenes_reset(&s);

	/* This exits depending on whether all tests passed */
	return exit_status();
}
예제 #8
0
unsigned long quadratic_sieve(mpz_t N, 
			      unsigned int n, 
			      unsigned interval,
			      unsigned int max_fact,
			      unsigned int block_size,
			      mpz_t m,
			      unsigned int print_fact) {
  double t1, t2;
  
  int rank;
  int comm_size;
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, & comm_size);

  /* Controllo con test di pseudoprimalità di rabin */
  if(mpz_probab_prime_p(N, 25)) {
    return NUM_PRIMO;
  }

  /* Radice intera di N */
  mpz_t s;
  mpz_init(s);
  mpz_sqrt(s, N); 

  t1 = MPI_Wtime();
  /* Individuazione primi in [2, n] */
  unsigned int * primes = malloc(sizeof(unsigned int) * n);  
  eratosthenes_sieve(primes, n);
  /* Compattiamo i numeri primi in primes */
  unsigned j = 0;
  for(int i = 2; i < n; ++i)
    if(primes[i] == 1) {
      primes[j++] = i;
    }

  unsigned int n_all_primes = j;
    
  /* Fattorizzazione eseguita da tutti, gli slave ritornano
     IM_A_SLAVE mentre il main il fattore */
  unsigned int simple_factor = trivial_fact(N, primes, n_all_primes);
  if(simple_factor != 0) {
    mpz_set_ui(m, simple_factor);
    return rank == 0 ? OK : IM_A_SLAVE;
  }

  /* Calcolo base di fattori e soluzioni dell'eq x^2 = N mod p */
  pair * solutions = malloc(sizeof(pair) * n_all_primes);
  unsigned int * factor_base = primes;

  unsigned n_primes = base_fattori(N, s, factor_base, solutions,
				  primes, n_all_primes);
  t2 = MPI_Wtime();
  double t_base = t2 - t1;
  if(rank == 0)
    printf("#Dimensione base di fattori: %d\n", n_primes);

  /* Vettore degli esponenti in Z */
  unsigned int ** exponents;
  /* Vettore degli (Ai + s) */
  mpz_t * As;
  /* Parte di crivello: troviamo le k+n fattorizzazioni complete */
  unsigned int n_fatt;

  t1 = MPI_Wtime();
  if(rank == 0){
    /* Inizializzazioni vettori */
    init_matrix(& exponents, n_primes + max_fact, n_primes);
    init_vector_mpz(& As, n_primes + max_fact);

    /* Procedura master che riceve le fatt. complete */
    n_fatt = master(n_primes, max_fact, exponents, As, comm_size, print_fact);
  } else {
    mpz_t begin;
    mpz_init(begin);
    mpz_t counter;
    mpz_init(counter);

    mpz_set_ui(begin, interval * (rank - 1));
  
    //gmp_printf("%d) begin=%Zd interval=%d\n", rank, begin, interval);

    int stop_flag = 0;
    do {
      //gmp_printf("\t%d) [%Zd, %Zd+%d] - (flag=%d)\n", rank, begin, begin, interval, flag);
      stop_flag = smart_sieve(N, factor_base, n_primes, solutions,
		  begin, interval,
		  block_size, max_fact);
      mpz_add_ui(begin, begin, interval * (comm_size-1));
    } while(!stop_flag);

    //printf("#%d) Termina\n", rank);

    return IM_A_SLAVE;
  }
  t2 = MPI_Wtime();
  double t_sieve = t2 - t1;
  printf("#Numero fattorizzazioni complete trovate: %d\n", n_fatt);
 
  t1 = MPI_Wtime();
  /* Matrice di esponenti in Z_2 organizzata a blocchi di bit */ 
  word ** M;
  /* Numero di blocchi di bit da utilizzare */
  unsigned long n_blocchi = n_primes / N_BITS + 1;
  /* Inizializzazione egli esponenti mod 2 */
  init_matrix_l(& M, n_fatt, n_blocchi);
  for(int i = 0; i < n_fatt; ++i)
    for(int j = 0; j < n_primes; ++j) {
      unsigned int a = get_matrix(exponents, i, j);
      set_k_i(M, i, j, a);
    }

  /* Vettore con le info (bit piu' a dx e num bit a 1) su M */
  struct row_stats * wt = malloc(sizeof(struct row_stats) * n_fatt);
  for(int i = 0; i < n_fatt; ++i)
    get_wt_k(M, i, n_primes, & wt[i]);

  /* In gauss gli esponenti sommati possono andare in overflow,
     li converto dunque in mpz */
  mpz_t ** exponents_mpz;
  mpz_t temp;
  mpz_init_set_ui(temp, 2);

  unsigned int a; 
  init_matrix_mpz(& exponents_mpz, n_fatt, n_primes);
  for(unsigned i = 0; i < n_fatt; ++i)
    for(unsigned j = 0; j < n_primes; ++j) {
      a = get_matrix(exponents, i, j);
      mpz_set_ui(temp, a);
      set_matrix_mpz(exponents_mpz, i, j, temp);
    }
  
  /* Eliminazione gaussiana */
  gaussian_elimination(exponents_mpz, M, As, N, 
		       n_fatt, n_primes, n_blocchi, wt);
  t2 = MPI_Wtime();
  double t_gauss = t2 - t1;

  /* In m ritorno un fattore non banale di N */
  unsigned int n_fact_non_banali = factorization(N, factor_base, 
						 M, exponents_mpz, 
						 As, wt, n_fatt, 
						 n_primes, m);
  
  printf("#time_base time_sieve time_gauss time_totale\n");
  printf("%.6f ", t_base);
  printf("%.6f ", t_sieve);
  printf("%.6f ", t_gauss);
  printf("%.6f\n", t_base + t_gauss + t_sieve);
  
  if(n_fact_non_banali > 0) {
    return OK;
  }
  else {
    return SOLO_FATTORIZZAZIONI_BANALI;
  }
}