static void multiply_primes(uint32 first, uint32 last, prime_sieve_t *sieve, mpz_t prod) { /* recursive routine to multiply all the elements of a list of consecutive primes. The current invocation multiplies elements first to last of the list, inclusive. The primes are created on demand */ mpz_t half_prod; uint32 mid = (last + first) / 2; /* base case; accumulate a few primes */ if (last < first + BREAKOVER_WORDS) { mpz_set_ui(prod, (unsigned long)get_next_prime(sieve)); while (++first <= last) { mpz_mul_ui(prod, prod, (unsigned long)get_next_prime(sieve)); } return; } /* recursively handle the left and right halves of the list */ mpz_init(half_prod); multiply_primes(first, mid, sieve, prod); multiply_primes(mid + 1, last, sieve, half_prod); /* multiply them together. We can take advantage of fast multiplication since in general the two half- products are about the same size*/ mpz_mul(prod, prod, half_prod); mpz_clear(half_prod); }
void init_assess(double b0, double b1, double area, unsigned int pb) { int i; double dp; assess_bound0=b0; assess_bound1=b1; assess_area=area; assess_prime_bound=pb; prime_table_init(); for (i=0; i<NSMALLPRIMES; i++) assess_primes[i]=get_next_prime(); if (assess_primes[NSMALLPRIMES-1]!=65521) Schlendrian("init_assess\n"); assess_alpha_max=0.; assess_rat=0.; for (i=0; i<NSMALLPRIMES; i++) { if (assess_primes[i]<100) continue; if (assess_primes[i]>assess_prime_bound) break; dp=(double)(assess_primes[i]); assess_alpha_max+=dp*log(dp)/(dp*dp-1); assess_rat+=log(dp)/(dp-1); } assess_mod_len=0; assess_mod=NULL; assess_root_len=0; assess_root=NULL; assess_coeffmod_len=0; assess_coeffmod=NULL; assess_optima=(double *)xmalloc(12*sizeof(double)); /* degree 5+1 !! */ }
static void rehash(smtlib2_hashtable *t, size_t min_buckets) { size_t cursz = smtlib2_vector_size(t->table_); if (cursz < min_buckets) { size_t i; uint32_t newsz = get_next_prime(min_buckets); smtlib2_vector *tmp = smtlib2_vector_new(); smtlib2_vector_resize(tmp, newsz); for (i = 0; i < cursz; ++i) { smtlib2_hashtable_bucket *b = (smtlib2_hashtable_bucket *)smtlib2_vector_at(t->table_, i); while (b) { size_t j = t->hf_(b->key_) % newsz; smtlib2_hashtable_bucket *n = b; b = b->next_; n->next_ = (smtlib2_hashtable_bucket *)smtlib2_vector_at(tmp, j); smtlib2_vector_at(tmp, j) = (intptr_t)n; } } smtlib2_vector_delete(t->table_); t->table_ = tmp; } }
/*------------------------------------------------------------------------*/ void sieve_fb_init(void *s_in, poly_coeff_t *c, uint32 factor_min, uint32 factor_max, uint32 fb_roots_min, uint32 fb_roots_max, uint32 fb_only) { uint32 p; sieve_fb_t *s = (sieve_fb_t *)s_in; aprog_list_t *aprog = &s->aprog_data; prime_sieve_t prime_sieve; s->degree = c->degree; s->fb_only = fb_only; factor_max = MIN(factor_max, 1000000); if (factor_max <= factor_min) return; aprog->num_aprogs = 0; init_prime_sieve(&prime_sieve, factor_min, factor_max); while (1) { p = get_next_prime(&prime_sieve); if (p > factor_max) break; sieve_add_aprog(s, c, p, fb_roots_min, fb_roots_max); } free_prime_sieve(&prime_sieve); }
int PianoFreqPrime::handle_event() { float number = 1; for(int i = 0; i < synth->config.oscillator_config.total; i++) { synth->config.oscillator_config.values[i]->freq_factor = number; number = get_next_prime(number); } synth->thread->window->update_gui(); synth->send_configure_change(); }
/*--------------------------------------------------------------------*/ static void find_fb_size(factor_base_t *fb, uint32 limit_r, uint32 limit_a, uint32 *entries_r_out, uint32 *entries_a_out) { prime_sieve_t prime_sieve; uint32 entries_r = 0; uint32 entries_a = 0; /* If the filtering bounds are extremely large, just estimate the target as the number of primes less than the filtering bounds */ if (limit_r > 20000000 && limit_a > 20000000) { *entries_r_out = 1.02 * limit_r / (log((double)limit_r) - 1); *entries_a_out = 1.02 * limit_a / (log((double)limit_a) - 1); return; } init_prime_sieve(&prime_sieve, 0, MAX(limit_r, limit_a) + 1000); while (1) { uint32 p = get_next_prime(&prime_sieve); uint32 num_roots; uint32 high_coeff; uint32 roots[MAX_POLY_DEGREE + 1]; if (p >= limit_r && p >= limit_a) break; if (p < limit_r) { num_roots = poly_get_zeros(roots, &fb->rfb.poly, p, &high_coeff, 1); if (high_coeff == 0) num_roots++; entries_r += num_roots; } if (p < limit_a) { num_roots = poly_get_zeros(roots, &fb->afb.poly, p, &high_coeff, 1); if (high_coeff == 0) num_roots++; entries_a += num_roots; } } free_prime_sieve(&prime_sieve); *entries_r_out = entries_r; *entries_a_out = entries_a; }
int main(void) { ULONG f, max = 0; ULONG num = 600851475143ULL; while (num > 1) { f = get_next_prime(); while (num % f == 0) { num /= f; if (f > max) max = f; } } printf("Max:%llu\n", max); return 0; }
int main(int argc, char *argv[]) { unsigned int current_prime = 2; char user_input = 'Y'; while (user_input != 'N' && user_input != 'n') { printf("Show the next prime? (Y/N):"); scanf(" %c", &user_input); if (user_input == 'Y' || user_input == 'y') { printf("%s%d\n\n", "The next prime number is ", current_prime); current_prime = get_next_prime(current_prime); } } printf("Bye :) \n"); return 0; }
/************************************************************************************************* ** Function: sync_threads ** Description: ... *************************************************************************************************/ void *sync_threads ( void *n ) { //get current location unsigned int curr = ( unsigned int ) ( intptr_t ) n; //set min for current thread unsigned int min = curr * (t_upper_bound / t_parallelism) + 1; //set max for current thread unsigned int max = (curr == t_parallelism - 1) ? t_upper_bound : min + (t_upper_bound / t_parallelism) - 1; unsigned int i; unsigned long j; i = 1; while ( ( i = get_next_prime ( i , t_upper_bound ) ) != 0 ) { for ( j = ( min / i < 3 ) ? 3 : ( min / i ) ; ( i * j ) <= max ; j++ ) { mark_non_prime ( i * j ); } } pthread_exit(EXIT_SUCCESS); }
/*********************************************** * get_prime_decomp 素因数分解結果の配列を取得する * * * in : num 素因数分解の対象となる数 * in/out : prime_listp 素因数分解結果 * out : 分解した素因数の数 ************************************************/ int get_prime_decomp(const int num, int *prime_listp) { int check_num; int prime; int size; check_num = num; prime = MIN_PRIME; size = 0; /* * check_num が素数であるか?をチェック */ while (1) { //printf(" check_num = %d,\tprime = %d\n", check_num, prime); if (check_num <= prime) { // check_num が素数で割り切れなければ終了 prime_listp[size] = check_num; size++; break; } if ((check_num % prime) == 0) { // 割り切れる素数が存在していたなら // 素因数として記憶 prime_listp[size] = prime; size++; // 割った値を次のループでチェックする数として更新 check_num = check_num / prime; // 素数の初期化 prime = MIN_PRIME; } else { // 割り切れなければ、次に大きい素数を探す prime = get_next_prime(prime); } } return size; }
/*------------------------------------------------------------------------*/ uint64 sieve_fb_next(sieve_fb_t *s, poly_search_t *poly, root_callback callback, void *extra) { uint32 i, j; uint64 p; uint32 num_roots; uint32 num_factors; uint32 factors[MAX_FACTORS]; uint32 found; while (1) { if (s->curr_algo == ALGO_ENUM || s->curr_algo == ALGO_SIEVE) { if (s->curr_algo == ALGO_ENUM) p = get_next_enum_composite(s); else p = get_next_composite(s); if (p == P_SEARCH_DONE) { uint32 last_p = s->aprog_data.aprogs[ s->aprog_data.num_aprogs-1].p; if (s->p_max >= P_PRIME_LIMIT || s->p_min >= P_PRIME_LIMIT || last_p >= s->p_max || s->num_roots_min > s->degree) { break; } s->curr_algo = ALGO_PRIME; init_prime_sieve(&s->p_prime, MAX(s->p_min, last_p + 1), (uint32)s->p_max); continue; } num_factors = get_composite_factors(s, p, factors); for (i = found = 0; i < poly->num_poly; i++) { num_roots = get_composite_roots(s, poly->batch + i, i, p, num_factors, factors, s->num_roots_min, s->num_roots_max); if (num_roots == 0) continue; if (num_roots == INVALID_NUM_ROOTS) break; found++; lift_roots(s, poly->batch + i, p, num_roots); callback(p, num_roots, i, s->roots, extra); } if (found > 0) break; } else { p = get_next_prime(&s->p_prime); if (p >= s->p_max) { p = P_SEARCH_DONE; break; } for (i = found = 0; i < poly->num_poly; i++) { uint32 roots[MAX_POLYSELECT_DEGREE]; num_roots = get_prime_roots(poly, i, (uint32)p, roots); if (num_roots == 0) continue; if (num_roots < s->num_roots_min || num_roots > s->num_roots_max) break; found++; for (j = 0; j < num_roots; j++) { mpz_set_ui(s->roots[j], (mp_limb_t)roots[j]); } lift_roots(s, poly->batch + i, p, num_roots); callback(p, num_roots, i, s->roots, extra); } if (found > 0) break; } } return p; }
/*------------------------------------------------------------------------*/ void sieve_fb_init(sieve_fb_t *s, poly_search_t *poly, uint32 factor_min, uint32 factor_max, uint32 fb_roots_min, uint32 fb_roots_max) { uint32 i, j; prime_sieve_t prime_sieve; aprog_list_t *aprog = &s->aprog_data; p_sieve_t *p_sieve = &s->p_sieve; p_enum_t *p_enum = &s->p_enum; if (factor_max <= factor_min) return; memset(s, 0, sizeof(sieve_fb_t)); s->degree = poly->degree; mpz_init(s->p); mpz_init(s->p2); mpz_init(s->m0); mpz_init(s->nmodp2); mpz_init(s->tmp1); mpz_init(s->tmp2); for (i = 0; i <= MAX_P_FACTORS; i++) mpz_init(s->accum[i]); for (i = 0; i < MAX_ROOTS; i++) mpz_init(s->roots[i]); i = 500; aprog->num_aprogs = 0; aprog->num_aprogs_alloc = i; aprog->aprogs = (aprog_t *)xmalloc(i * sizeof(aprog_t)); p_sieve->sieve_block = (uint8 *)xmalloc(SIEVE_SIZE * sizeof(uint8)); p_sieve->good_primes.num_primes = 0; p_sieve->good_primes.num_primes_alloc = i; p_sieve->good_primes.primes = (sieve_prime_t *)xmalloc( i * sizeof(sieve_prime_t)); p_sieve->bad_primes.num_primes = 0; p_sieve->bad_primes.num_primes_alloc = i; p_sieve->bad_primes.primes = (sieve_prime_t *)xmalloc( i * sizeof(sieve_prime_t)); p_enum->curr_list.num_entries_alloc = i; p_enum->curr_list.list = (ss_t *)xmalloc(i * sizeof(ss_t)); p_enum->new_list.num_entries_alloc = i; p_enum->new_list.list = (ss_t *)xmalloc(i * sizeof(ss_t)); init_prime_sieve(&prime_sieve, 2, factor_max); while (1) { uint32 p = get_next_prime(&prime_sieve); if (p >= factor_max) break; else if (p <= factor_min) sieve_add_prime(&p_sieve->bad_primes, p); else if (!sieve_add_aprog(s, poly, p, fb_roots_min, fb_roots_max)) sieve_add_prime(&p_sieve->bad_primes, p); else sieve_add_prime(&p_sieve->good_primes, p); } free_prime_sieve(&prime_sieve); j = p_sieve->good_primes.num_primes; for (i = 0; i < j; i++) { uint32 p = p_sieve->good_primes.primes[i].p; uint32 power_limit = factor_max / p; uint32 power = p; if (power_limit < p) break; while (power < power_limit) { power *= p; sieve_add_prime(&p_sieve->good_primes, power); } } }
/*------------------------------------------------------------------*/ void relation_batch_init(msieve_obj *obj, relation_batch_t *rb, uint32 min_prime, uint32 max_prime, uint32 lp_cutoff_r, uint32 lp_cutoff_a, savefile_t *savefile, print_relation_t print_relation) { prime_sieve_t sieve; uint32 num_primes, p; /* count the number of primes to multiply. Knowing this in advance makes the recursion a lot easier, at the cost of a small penalty in runtime */ init_prime_sieve(&sieve, min_prime + 1, max_prime); p = min_prime; num_primes = 0; while (p < max_prime) { p = get_next_prime(&sieve); num_primes++; } free_prime_sieve(&sieve); /* compute the product of primes */ logprintf(obj, "multiplying %u primes from %u to %u\n", num_primes, min_prime, max_prime); init_prime_sieve(&sieve, min_prime, max_prime); mpz_init(rb->prime_product); multiply_primes(0, num_primes - 2, &sieve, rb->prime_product); logprintf(obj, "multiply complete, product has %u bits\n", (uint32)mpz_sizeinbase(rb->prime_product, 2)); rb->savefile = savefile; rb->print_relation = print_relation; /* compute the cutoffs used by the recursion base-case. Large primes have a maximum size specified as input arguments, but numbers that can be passed to the SQUFOF routine are limited to size 2^62 */ rb->lp_cutoff_r = lp_cutoff_r; lp_cutoff_r = MIN(lp_cutoff_r, 0x7fffffff); mp_clear(&rb->lp_cutoff_r2); rb->lp_cutoff_r2.nwords = 1; rb->lp_cutoff_r2.val[0] = lp_cutoff_r; mp_mul_1(&rb->lp_cutoff_r2, lp_cutoff_r, &rb->lp_cutoff_r2); rb->lp_cutoff_a = lp_cutoff_a; lp_cutoff_a = MIN(lp_cutoff_a, 0x7fffffff); mp_clear(&rb->lp_cutoff_a2); rb->lp_cutoff_a2.nwords = 1; rb->lp_cutoff_a2.val[0] = lp_cutoff_a; mp_mul_1(&rb->lp_cutoff_a2, lp_cutoff_a, &rb->lp_cutoff_a2); mp_clear(&rb->max_prime2); rb->max_prime2.nwords = 1; rb->max_prime2.val[0] = max_prime; mp_mul_1(&rb->max_prime2, max_prime, &rb->max_prime2); /* allocate lists for relations and their factors */ rb->target_relations = 500000; rb->num_relations = 0; rb->num_relations_alloc = 1000; rb->relations = (cofactor_t *)xmalloc(rb->num_relations_alloc * sizeof(cofactor_t)); rb->num_factors = 0; rb->num_factors_alloc = 10000; rb->factors = (uint32 *)xmalloc(rb->num_factors_alloc * sizeof(uint32)); }
const Primality::key_type Primality::get_random_prime() { prime = get_random_integer(); return get_next_prime(); }
/*------------------------------------------------------------------------*/ uint32 sieve_fb_next(void *s_in, poly_coeff_t *c, root_callback callback, void *extra) { /* main external interface */ uint32 i, p, num_roots; sieve_fb_t *s = (sieve_fb_t *)s_in; while (1) { if (s->avail_algos & ALGO_ENUM) { /* first attempt to find a p by combining smaller suitable aprogs p_i */ p = get_next_enum(s); if (p == P_SEARCH_DONE) { s->avail_algos &= ~ALGO_ENUM; continue; } num_roots = get_enum_roots(s); } else if (s->avail_algos & ALGO_PRIME) { /* then try to find a large prime p */ uint32 roots[MAX_POLYSELECT_DEGREE]; p = get_next_prime(&s->p_prime); if (p >= s->p_max || p >= P_PRIME_LIMIT) { s->avail_algos &= ~ALGO_PRIME; continue; } num_roots = get_prime_roots(c, p, roots, &s->tmp_poly); num_roots = MIN(num_roots, s->num_roots_max); if (num_roots == 0 || num_roots < s->num_roots_min) continue; for (i = 0; i < num_roots; i++) s->roots[i] = roots[i]; } else { return P_SEARCH_DONE; } /* p found; generate all the arithmetic progressions it allows and postprocess the whole batch */ lift_roots(s, c, p, num_roots); callback(p, num_roots, s->roots, extra); return p; } }