void HashTable<Type> :: updateTableCapacity() { int updatedCapacity = getNextPrime(); CTECList<HashNode<Type>> * updateTable = new CTECList<HashNode<Type>> [updatedCapacity]; int oldTableCapacity = tableCapacity; tableCapacity = updatedCapacity; for(int index = 0; index < oldTableCapacity; index++) { if (tableStorage[index] != nullptr) { CTECList<HashNode<Type>> temp = tableStorage[index]; for(int innerIndex = 0; innerIndex < temp.getSize(); innerIndex++) { int updatedTablePosition = findPosition(temp.get(index)); if(updateTable[updatedTablePosition] == nullptr) { CTECList<HashTable<Type>> updatedList; updatedList.addEnd(temp.get(index)); } else { updateTable[updatedTablePosition].addEnd(temp.get(index)); } } } } }
int main(int argc, char* argv[]) { int* primes; int i, j, k, NPRIMES; NPRIMES = atoi(argv[1]); primes = (int*)malloc(NPRIMES * sizeof(int)); /* we start at 2 because it is the smallest prime */ for(i=2; i < NPRIMES; i++) { primes[i] = i; printf("%d\n", i); } for(i=2; i < NPRIMES; i = getNextPrime(i+1, primes)) { for(j = (i*2); j < NPRIMES; j += i) { printf("i: %d, j: %d\n", i, j); primes[j] = 0; } } for(i=2; i < NPRIMES; i++) { if ( primes[i] != 0 ) { printf("Prime number: %d\n", primes[i]); } } return 0; }
char* primeFactorization(long int num, int* success) { // Make sure success poniter is valid if (success == NULL) { return NULL; } else { *success = '\0'; } // Make sure we have a number greater than 1 if (num <= 1) { *success = -1; return NULL; } // Allocate space for the list and initialize it char* list = (char*)malloc(sizeof(char)); list[0] = '\0'; // Check if input is already prime. // If so, return the input num. if (isPrime(num) == 1) { *success = 1; list = addToList(list, num, 1); return list; } // Set starting base and power variables long int base = 2; long int power = 0; while (num != 1) { while (num % base == 0) { num /= base; power++; } if (power != 0) { list = addToList(list, base, power); // Check if the new num is prime. // If so, add it to the list and break. if (num != 1 && isPrime(num) == 1) { list = addToList(list, num, 1); break; } power = 0; } base = getNextPrime(base); } *success = 1; return list; }
/* resizes hash table if it is full above FILL_RATIO */ void resizeHashTable() { if ((int)(table_size*FILL_RATIO) <= hash_size) { int new_size = getNextPrime(2 * table_size); printf("The table is being resized from [%d] to [%d]\n", table_size, new_size); hash_table = (char**) realloc(hash_table, new_size * sizeof(char*)); for (int i=table_size; i<new_size; i++) { *(hash_table+i) = 0; } table_size = new_size; } }
void CTECHashTable<Type>::updateCapacity(void){ int updatedCapacity = getNextPrime(); int oldCapacity = capacity; capacity = updatedCapacity; HashNode<Type> * largerStorage = new HashNode<Type>[capacity]; for(int index = 0; index < oldCapacity; index ++){ if(internalStorage[index] != nullptr){ int updateIndex = findPosition(internalStorage[index]); largerStorage[updatedCapacity] = internalStorage[index]; } } internalStorage = largerStorage; }
void CTECHashTable<Type>::updateSize() { int updatedCapacity = getNextPrime(); HashNode<Type>** updatedStorage = new HashNode<Type>*[updatedCapacity]; int oldCapacity = capacity; capacity = updatedCapacity; for (int index = 0; index < capacity; index++) { if (internalStorage[index] != nullptr) { int updatedPosition = findPosition(*internalStorage[index]); updatedStorage[updatedPosition] = internalStorage[index]; } } internalStorage = updatedStorage; }
void std::vector<bool> sieveOfEratosthenes(int max) { std::vector<bool> flags(max + 1); init(flags); int prime = 2; while (prime <= sqrt(max)) { crossOff(flags, prime); prime = getNextPrime(flags, prime); } return flags; }
int main(int argc, char *argv) { char primeList[BOUND]; int i, j, k, l, m; int next, tmp; getPrimeList(primeList); for (i = 3; i < BOUND; getNextPrime(primeList,&i)) { for (j = i + 1, getNextPrime(primeList, &j); j < BOUND; getNextPrime(primeList,&j)) { if (connect(i, j) == 1) break; if (check(i, j, primeList)) { for (k = j + 1, getNextPrime(primeList, &k); k < BOUND; getNextPrime(primeList, &k)) { if (connect(j, k) == 1) break; if (check(j, k, primeList) && check(i, k, primeList)) { for (l = k + 1, getNextPrime(primeList, &l); l < BOUND; getNextPrime(primeList, &l)) { if (connect(k, l) == 1)break; if (check(k, l, primeList) && check(j, l, primeList) && check(i, l,primeList)) { for (m = l + 1, getNextPrime(primeList, &m); m < BOUND; getNextPrime(primeList, &m)){ if (connect(l, m) == 1)break; if (check(l, m, primeList) && check(k, m, primeList) && check(j, m, primeList) && check(i, m, primeList)) { printf("%d %d %d %d\n",i,j,k,l); return 0; } } } } } } } } } return 0; }
void CTECHashTable<Type>:: updateChainedCapacity(){ int updatedChainedCapacity = getNextPrime(); int oldChainedCapacity = chainedCapacity; chainedCapacity = updatedChainedCapacity; std::list<HashNode<Type>> * largerChainedStorage = new std::list<HashNode<Type>>[updatedChainedCapacity]; for(int index = 0; index < oldChainedCapacity; index++){ if(chainedStorage[index] != nullptr){ std::list<HashNode<Type>> temp = chainedStorage[index]; for(int innerIndex = 0; innerIndex < temp.getSize(); innerIndex ++){ int updatedChainedPosition = findPosition(temp.getFromIndex(innerIndex)); if(largerChainedStorage[updatedChainedPosition] == nullptr){ std::list<HashNode<Type>> insertList; insertList.addEnd(temp.getFromIndex(innerIndex)); largerChainedStorage[updatedChainedPosition] = insertList; }else{ largerChainedStorage[updatedChainedPosition].addEnd(temp.getFromIndex(innerIndex)); } } } } }
bool hashTable::init(const list<string>& listToBeInserted, double factor) { if (listToBeInserted.empty() || factor <= 0.f) { return false; } int nrElems = listToBeInserted.size(); int len = getNextPrime(nrElems/factor); table_.clear(); table_.resize(len); for (list<string>::const_iterator it = listToBeInserted.begin(); it != listToBeInserted.end(); ++it) { string q = (*it); unsigned int ind = hashFunc(q); ind = ind % table_.size(); table_[ind].push_back(q); } return true; }