/* Create the hashmap with an initial capacity */ hashmap* hashmap_create(const size_t init_cap) { hashmap *h = malloc(sizeof(hashmap)); h->bucket_count = next_prime(init_cap); h->buckets = calloc(h->bucket_count, sizeof(hnode*)); h->size = 0; return h; }
int main() { std::vector<uint64_t> primes; while (primes.size() < 10001) { primes.push_back(next_prime(primes)); } std::cout << primes.back() << std::endl; }
void cpSpaceHashResize(cpSpaceHash *hash, cpFloat celldim, int numcells) { // Clear the hash to release the old handle locks. clearHash(hash); hash->celldim = celldim; cpSpaceHashAllocTable(hash, next_prime(numcells)); }
int main(){ int cur_prime=2,max_found=0,i=1; while (i != 10001){ cur_prime = next_prime(cur_prime); i++; } printf ("\n%d\n",cur_prime); return 0; }
static void init_sieve() { int64_t i, j; for(i = 3; i && i < PMAX_SQRT; i = next_prime(i)) { for(j = i * 3; j < PMAX; j += i * 2) { sieve[p2i(j)] = 1; } } }
/* initialize empty set */ static set_t *set_init(int num) { set_t *s = (set_t *)malloc(sizeof(set_t)); s->nbuckets = next_prime(num); s->buckets = malloc(s->nbuckets*sizeof(llnode_t)); memset(s->buckets,0,s->nbuckets*sizeof(llnode_t)); s->count = 0; return s; }
long long maior_fator(long long number){ long long numero = number; int next = next_prime(1); while(1){ if ( numero % next == 0 ){ numero = numero/next; }else if ( numero == 1 ){ return next; }else{ next = next_prime(next); } } }
void shrink() { tsize = next_prime(std::size_t(elems / shload)); std::vector<T> temp(tsize); t.swap(temp); // temp is now the old vector, reinsert all the old elements rehash(temp); }
void enlarge() { tsize = next_prime(tsize + 1); std::vector<T> temp(tsize); t.swap(temp); // temp is now the old vector, reinsert all the old elements rehash(temp); }
/* initialize empty map */ static map_t *map_init(int num) { map_t *m = (map_t *)malloc(sizeof(map_t)); if (!m) return NULL; m->nbuckets = next_prime(num); m->buckets = malloc(m->nbuckets*sizeof(mapnode_t)); memset(m->buckets,0,m->nbuckets*sizeof(mapnode_t)); m->count = 0; return m; }
Map get_prime_factors(uint64_t n) { static std::map<uint64_t, Map> cache; auto it = cache.find(n); if (it != cache.end()) { return it->second; } Map & result = cache[n]; static std::vector<uint64_t> pre = []{ std::vector<uint64_t> result; for (int i = 0; i < 1000; ++i) { next_prime(result); } return result; }(); while (n > pre.size()) { next_prime(pre); } for (uint64_t i = 0; i < pre.size(); ++i) { auto p = pre[i]; while (n % p == 0) { result[p]++; n /= p; } if (p > n) { return result; } } return result; }
void cpSpaceHashResize(cpSpaceHash *hash, cpFloat celldim, int numcells) { if(hash->spatialIndex.klass != Klass()){ cpAssertWarn(cpFalse, "Ignoring cpSpaceHashResize() call to non-cpSpaceHash spatial index."); return; } clearTable(hash); hash->celldim = celldim; cpSpaceHashAllocTable(hash, next_prime(numcells)); }
void find_point() { bigmod h,x; bigint q,a4,a6,y; char* sa4=new char[MAX]; char* sa6=new char[MAX]; char* sq=new char[MAX]; banner(); cout<<"\nFIND POINT"; cout<<"\n----------"; do { cout<<"\nPlease enter prime field (p)."; cout<<"Its length should be 160 or 192-bit : \n"; cin>>sq; string_to_bigint(sq,q); } while((!is_prime(q)) || ((q.bit_length() != 160) && (q.bit_length()!=192))); bigmod::set_modulus(q); cout<<"\nPlease input numbers to the following : "<<endl; do { cout<<"Coefficient a = ";cin>>sa4; } while(!is_number(sa4)); do { cout<<"Coefficient b = ";cin>>sa6; } while(!is_number(sa6)); string_to_bigint(sa4,a4); string_to_bigint(sa6,a6); q=next_prime(q-1); do { x.randomize(); h = (x*x+a4)*x+a6; } while(jacobi(h.mantissa(),q) !=1); ressol_p(y,h.mantissa(),q); cout<<"Point P : ("<<x<<","<<y<<")"<<endl; delete[] sa4; delete[] sa6; }
Hash_Table * hash_table__new( unsigned int buffer_size, double load, double expansion, hash_f hash, Comparator compare ) { unsigned int i; Hash_Table *h; if ( ( h = new( Hash_Table ) ) ) { h->buffer_size = next_prime( buffer_size ); h->hash = hash; h->compare = compare; /* Sparsity must be at least 1, otherwise the buffer will not resize even when it is completely full. */ h->load = ( load > 0 && load < 1 ) ? load : DEFAULT_LOAD_FACTOR; /* Expansion factor must be greater than 1 for the buffer to actually gain in size. */ h->expansion = ( expansion > 1 ) ? expansion : DEFAULT_EXPANSION_FACTOR; /* Buffer is initially empty. */ h->size = 0; /* Capacity is re-calculated whenever the table resizes. Note: capacity may initially be 0, which just means that the hash table will expand as soon as it is added to. */ h->capacity = ( unsigned int ) ( h->buffer_size * h->load ); if ( !( h->buffer = buffer_new( h->buffer_size ) ) ) { free( h ); h = 0; } else for ( i = 0; i < h->buffer_size; i++ ) h->buffer[i] = 0; } if ( !h ) ERROR( "hash_table__new: allocation failure" ); return h; }
cpSpaceHash* cpSpaceHashInit(cpSpaceHash *hash, cpFloat celldim, int numcells, cpSpaceHashBBFunc bbfunc) { cpSpaceHashAllocTable(hash, next_prime(numcells)); hash->celldim = celldim; hash->bbfunc = bbfunc; hash->bins = NULL; hash->handleSet = cpHashSetNew(0, &handleSetEql, &handleSetTrans); hash->stamp = 1; return hash; }
int is_prime(xint x) { pint p; if (x > 5) { if (x < MAX_PRIME) return pbits[x/30] & bit_pos[x % 30]; for (p = 2; p && (xint)p * p <= x; p = next_prime(p)) if (x % p == 0) return 0; return 1; } return x == 2 || x == 3 || x == 5; }
std::vector<uint64_t> get_primes_below(uint64_t limit) { std::vector<uint64_t> result; result.reserve(limit / 2); // no reallocs for (;;) { uint64_t next = next_prime(result); if (next >= limit) { return result; } result.push_back(next); } }
int main(int argc, char **argv) { long int sum = 5; // 2 and 3 are primes long int next = 5; while(next < PRIMES_LIMIT) { sum += next; next = next_prime(next); } printf("Sum: %ld\n", sum); return 0; }
int main(){ int n = 7; int count = 0; int result = 0; while (count < 11){ n = next_prime(n); if(truncatable_prime(n)){ result += n; count++; } } std::cout << result << std::endl; }
// This algorithm is explained in section 2 of // "Landau's function for one million billions", // by Marc Deleglise, Jean-Louis Nicolas and Paul Zimmermann // in Journal de Theorie des Nombres de Bordeaux 20, 3 (2009) 625-671. // The paper goes on to give a much more efficient algorithm for // larger values of n, but that seems needless complex for our purposes. unsigned landau(unsigned const n) { vector<unsigned> g(n+1, 1); // Strictly speaking, this bound is only valid for n>=5. Adding 1 // empirically fixes it for n<5. unsigned pmax = (unsigned) floor( 1.328 * sqrt(n * log( (double)n ) ) ) + 1; for ( unsigned p = 2; p <= pmax; p = next_prime(p) ) for ( unsigned m = n; m >=1; --m ) for ( unsigned q = p; q <= m; q *= p ) { unsigned gm = q * g[m-q]; if ( gm > g[m] ) g[m] = gm; } return g[n]; }
cpHashSet * cpHashSetInit(cpHashSet *set, int size, cpHashSetEqlFunc eqlFunc, cpHashSetTransFunc trans) { set->size = next_prime(size); set->entries = 0; set->eql = eqlFunc; set->trans = trans; set->default_value = NULL; set->table = (cpHashSetBin **)cpcalloc(set->size, sizeof(cpHashSetBin *)); return set; }
std::vector< std::pair<int, int> > prime_decomp( size_t n ){ std::map< int, int > decomp; while( n % 2 == 0 ){ n = n / 2; decomp[ 2 ] = decomp[ 2 ] + 1; } while( n % 3 == 0 ){ n = n / 3; decomp[ 3 ] = decomp[ 3 ] + 1; } if( n > 1 ){ PrimeCandidateState state = { 0, 1 }; size_t p = next_prime( state ); do{ if( n % p == 0 ){ n = n / p; decomp[ p ] = decomp[ p ] + 1; } else { p = next_prime( state ); } }while( n > 1 ); } std::vector< std::pair<int, int> > result; result.reserve( decomp.size() ); for( const auto & v: decomp ){ result.emplace_back( v.first, v.second ); } return result; }
static size_t _GL_ATTRIBUTE_PURE compute_bucket_size (size_t candidate, const Hash_tuning *tuning) { if (!tuning->is_n_buckets) { float new_candidate = candidate / tuning->growth_threshold; if (SIZE_MAX <= new_candidate) return 0; candidate = new_candidate; } candidate = next_prime (candidate); if (xalloc_oversized (candidate, sizeof (struct hash_entry *))) return 0; return candidate; }
static void insert_entry_2 (hash_table *htab, const void *key, size_t keylen, unsigned long int hval, size_t idx, void *data) { hash_entry *table = (hash_entry *) htab->table; table[idx].used = hval; table[idx].key = key; table[idx].keylen = keylen; table[idx].data = data; /* List the new value in the list. */ if ((hash_entry *) htab->first == NULL) { table[idx].next = &table[idx]; htab->first = &table[idx]; } else { table[idx].next = ((hash_entry *) htab->first)->next; ((hash_entry *) htab->first)->next = &table[idx]; htab->first = &table[idx]; } ++htab->filled; if (100 * htab->filled > 75 * htab->size) { /* Table is filled more than 75%. Resize the table. Experiments have shown that for best performance, this threshold must lie between 40% and 85%. */ unsigned long int old_size = htab->size; htab->size = next_prime (htab->size * 2); htab->filled = 0; htab->first = NULL; htab->table = (void *) xcalloc (1 + htab->size, sizeof (hash_entry)); for (idx = 1; idx <= old_size; ++idx) if (table[idx].used) insert_entry_2 (htab, table[idx].key, table[idx].keylen, table[idx].used, lookup (htab, table[idx].key, table[idx].keylen, table[idx].used), table[idx].data); free (table); } }
std::map<unsigned, unsigned> get_prime_factors(unsigned n) { std::map<unsigned, unsigned> result; std::vector<uint64_t> pre = {2}; for (unsigned i = 2; i <= n; i = next_prime(pre)) { pre.push_back(i); auto copy = n; while (copy % i == 0) { result[i]++; copy /= i; } } return result; }
static void zbx_hashmap_init_slots(zbx_hashmap_t *hm, size_t init_size) { hm->num_data = 0; if (0 < init_size) { hm->num_slots = next_prime(init_size); hm->slots = hm->mem_malloc_func(NULL, hm->num_slots * sizeof(ZBX_HASHMAP_SLOT_T)); memset(hm->slots, 0, hm->num_slots * sizeof(ZBX_HASHMAP_SLOT_T)); } else { hm->num_slots = 0; hm->slots = NULL; } }
int main(int argc, char *argv[]) { unsigned long num = TARGET_NUM; unsigned long prime; prime = 1; while (num > 1) { prime = next_prime(prime); while (num % prime == 0) num /= prime; } printf("%ld\n", prime); return 0; }
std::map<value_type, count_type> get_prime_factors(unsigned n) { std::map<value_type, count_type> result; std::vector<uint64_t> pre = {2}; for (unsigned i = 2; i <= n; i = next_prime(pre)) { auto copy = n; while (copy % i == 0) { result[i]++; copy /= i; } } std::cout << "prime factors for " << n << ": " << result << std::endl; return result; }
int main() { long long number = 600851475143; long long factor = 3; while (number != 1) { if (number % factor) { next_prime(factor); } else { number /= factor; } } std::printf("%lld\n", factor); }
cpSpaceHash* cpSpaceHashInit(cpSpaceHash *hash, cpFloat celldim, int numcells, cpSpaceHashBBFunc bbfunc) { cpSpaceHashAllocTable(hash, next_prime(numcells)); hash->celldim = celldim; hash->bbfunc = bbfunc; hash->handleSet = cpHashSetNew(0, (cpHashSetEqlFunc)handleSetEql, (cpHashSetTransFunc)handleSetTrans); hash->pooledHandles = cpArrayNew(0); hash->pooledBins = NULL; hash->allocatedBuffers = cpArrayNew(0); hash->stamp = 1; return hash; }