Exemplo n.º 1
0
int main(int argc, char* argv[]){
  int start;
  int max = 0;
  int max_start;
  int max_count = 0;
  int counter;
  int sum;
  int i;
  // We don't cache the results
  // But set the limit start * max_count < 1000000
  // The program will finish quickly enough
  for (start = 0; start * max_count < 1000000; start = nextPrime(start)){
    printf("%d %d\n", start, max_count);
    sum = 0;
    counter = 0;
    for (i = nextPrime(start); sum < 1000000; i = nextPrime(i)){
      sum += i;
      counter += 1;
      if (isPrime(sum) && counter > max_count){
        max = sum;
        max_count = counter;
        max_start = start;
      }
    }
  }
  printf("%d\n", max);
  return 0;
}
Exemplo n.º 2
0
Arquivo: util.cpp Projeto: IlyaM/mongo
        void run() {
            assert( WrappingInt(0) <= WrappingInt(0) );
            assert( WrappingInt(0) <= WrappingInt(1) );
            assert( !(WrappingInt(1) <= WrappingInt(0)) );
            assert( (WrappingInt(0xf0000000) <= WrappingInt(0)) );
            assert( (WrappingInt(0xf0000000) <= WrappingInt(9000)) );
            assert( !(WrappingInt(300) <= WrappingInt(0xe0000000)) );

            assert( tdiff(3, 4) == 1 );
            assert( tdiff(4, 3) == -1 );
            assert( tdiff(0xffffffff, 0) == 1 );

            assert( isPrime(3) );
            assert( isPrime(2) );
            assert( isPrime(13) );
            assert( isPrime(17) );
            assert( !isPrime(9) );
            assert( !isPrime(6) );
            assert( nextPrime(4) == 5 );
            assert( nextPrime(8) == 11 );

            assert( endsWith("abcde", "de") );
            assert( !endsWith("abcde", "dasdfasdfashkfde") );

            assert( swapEndian(0x01020304) == 0x04030201 );

        }
Exemplo n.º 3
0
PerlinNoise::PerlinNoise(int oct, float per, float zoom, int start)
: m_iOctaves(oct), m_fPersistance(per), m_fZoom(zoom)
{
    m_Primes.reserve(oct+1);
    m_Primes.push_back( nextPrime(start) );

    for(int i=1; i<oct+1; i++)
        m_Primes.push_back( nextPrime(m_Primes[i-1]) );

    std::cout << oct << std::endl << std::setprecision(5) << per << std::endl << zoom << std::endl
    << start << std::endl << std::endl;
}
Exemplo n.º 4
0
int Primes::nextPrime(int start)
{
    if (thePrimes == NULL) {
	init();
    }
    if (start < 2) { return 2; }
    int cand = start+1;
    if (cand % 2 == 0) cand++;
    for (;;) {
	bool isPrime = true;
	for (int i = 0; thePrimes[i]*thePrimes[i] <= cand; i++) {
	    if (i+1 == thePrimesSize) {
		nextPrime();
	    }
	    if (cand % thePrimes[i] == 0) {
		isPrime = false;
		break;
	    }
	}
	if (isPrime) {
	    return cand;
	}
	cand += 2;
    }
}
Exemplo n.º 5
0
int main(int argc, char *argv[]) {
	/*
	printf("Argument count: %d\n", argc);
	for (int i = 0; i < argc; i++) {
		printf("Argument vector values:%s at %p memory\n", argv[i], argv[i]);
		for (char *j=argv[i]; *j!='\0'; j++) {
			printf("Another way to print argument vector values: "
                               "%c at %p memory\n", *j, j);
		}
	}
	*/

	bool flag[N];
	memset(flag,1,N*sizeof(bool));

	flag[0] = 0;
	flag[1] = 0;
	int prime = 2;

	while (prime < N) {
		crossOff(flag,prime);
		prime = nextPrime(flag,prime);
	}

	printPrimes(flag);

	return 0;
}
Exemplo n.º 6
0
int main(int argc, char const *argv[])
{
	struct node* head = NULL;
	struct node* current;
	int primes = 0;
	int i = 0;
	int next;

	while (primes <= 10001)
	{
		if (head == NULL)
		{
			i = 2;
			push(&head, i);
			primes++;
		}
		else
		{
			i = head->data + 1;
			next = nextPrime(head, i);
			push(&head, next);
		}
		printf("%d : %d\n", primes, head->data);
		primes++;

	}

	printf("%d\n", head->data);
	return 0;
}
Exemplo n.º 7
0
int main() {
    long int i, j, a, b, maxNum, tmpNum, coefficientA, coefficientB;
    maxNum = tmpNum = 0;
    for (i=3; i<1000; i+=2) {
        if (isPrime(i)) {
            b = i;
            for (a=-999; a<1000; a+=2) {
                if (a > -1-b) {
                    j = 1;
                    tmpNum = 1;
                    while (isPrime(j*j + a*j +b)) {
                        j++;
                        tmpNum++;
                    }
                    if (tmpNum > maxNum) {
                        maxNum = tmpNum;
                        coefficientA = a;
                        coefficientB = b;
                    }
                }
            }
        }
    }

    printf("a is %d, b is %d, product is %ld, maxNum is %d\n", coefficientA, coefficientB, coefficientA*coefficientB, maxNum);
    printf("%d\n", nextPrime(53));
    return 0;
}
Exemplo n.º 8
0
int main(void){
   int prime = 2;
   for (int i = 1; i < 10001; i++){
      prime = nextPrime(prime);
   }
   printf("%d\n",prime);
}
Exemplo n.º 9
0
        void run() {
            verify( isPrime(3) );
            verify( isPrime(2) );
            verify( isPrime(13) );
            verify( isPrime(17) );
            verify( !isPrime(9) );
            verify( !isPrime(6) );
            verify( nextPrime(4) == 5 );
            verify( nextPrime(8) == 11 );

            verify( endsWith("abcde", "de") );
            verify( !endsWith("abcde", "dasdfasdfashkfde") );

            verify( swapEndian(0x01020304) == 0x04030201 );

        }
Exemplo n.º 10
0
int numberOfDivisors(double p){
	double i;
	double temp;
	int n_exp = 0;
	int cont = 1;
	for(i = nextPrime(true) ; p > 1 ; ){
		if(modf(p / i, &temp) == 0){
			p = temp;
			n_exp++;
		} else {
			i = nextPrime(false);
			cont *=(n_exp + 1);
			n_exp = 0;
		}
	}
	cont *=(n_exp + 1);
	return cont;
}
Exemplo n.º 11
0
//----------------
// add the next occuring prime to storage
void PrimeNumbers::addPrimeNumber() {
  if (!primeNumbers.size()) {
    primeNumbers.push_back(1L);
  } else {
    uint64_t endp = primeNumbers[ primeNumbers.size() - 1 ];
    uint64_t newp = nextPrime(endp);
    primeNumbers.push_back(newp);
  }
}
Exemplo n.º 12
0
// Constructor
// IMPLEMENT
HashTable::HashTable(int table_size, HASH f)
    : size(nextPrime(table_size)), h(f), nItems(0)
{
    hTable = new Item* [size];

    for(int i = 0; i < size; i++)
    {
        hTable[i] = nullptr;
    }
}
Exemplo n.º 13
0
int main(int argv, char * argc [])
{/*
  int largestVal = atoi(argc[1]);
  int base = atoi(argc[2]);
  char * boolVal;
  size_t size;
  
  boolVal = baseexpansion(largestVal, base, &size);
   
  printf("Result of %i expanded to base %i is %s.\n", largestVal, base, boolVal);*/
  long first = nextPrime();
  long second = nextPrime();
  
  srand(time(NULL));

  printf("%l, %l\n", first, second);
  
  return 0;
}
Exemplo n.º 14
0
int
main(int argc, char** argv)
{
    primes.insert(1);
    primes.insert(2);
    primes.insert(3);

    long int i = 3;
    for (;; i += 2)
    {
        if (isPrime(i))
            continue;

        long int currSquare = 1;
        long int primePart = 1;
        long int currSquarePart = 2 * currSquare * currSquare;

        for (; currSquarePart + 1 < i; currSquare++, currSquarePart = 2 * currSquare * currSquare)
        {
            auto p = primes.begin();
            bool exceeded = false;
            primePart = *p;
            while (currSquarePart + primePart < i)
            {
                if (exceeded || p == primes.end())
                {
                    exceeded = true;
                    primePart = nextPrime(primePart);
                }
                else
                {
                    p++;
                    primePart = *p;
                }
            }

            if (currSquarePart + primePart == i)
            {
                long int sum = currSquarePart + primePart;
                printf ("%li: %li + 2 * (%li)^2 = %li\n", i, primePart, currSquare, i);
                break;
            }
        }

        // Print solution and break
        if (currSquarePart + primePart > i)
        {
            printf ("%li: ? + 2 * (?)^2 = ???\n", i);
            break;
        }
    }

    return 0;
}
WordHashTable *WordHashTableInit(int capacity)
{
   WordHashTable *wht = malloc(sizeof(wht));
   
   wht->capacity = capacity;
   wht->size = 0;
   wht->allocated = nextPrime(capacity * 2);
   wht->entries = calloc(sizeof(void *), wht->allocated);
   
   return wht;
}
Exemplo n.º 16
0
hashTable::hashTable(int tableSize)
{
    this->tableSize=nextPrime(tableSize);
    table = new node [this->tableSize];
    size=0 ;
    for (int i=0 ; i<this->tableSize ; i++)
    {
        table[i].collision = NULL ;
        table[i].token.clear() ;
        table[i].value = -2;
    }
}
Exemplo n.º 17
0
	void rehash()
	{
		vector<list<int> > oldLists = theLists;
		makeEmpty();
		theLists.resize(nextPrime(2*oldLists.size()));

		for(vecotr<list<int> >::iterator itr = oldLists.begin(); itr != oldLists.end(); itr++)
		{
			for(list<int>::iterator itrIn = itr->begin(); itrIn != itr->end(); itrIn()++)
				insert(*itrIn);
		}
	}
Exemplo n.º 18
0
void T_Hashtable<Item>::rehash() {

    T_Vector<HashEntry> *oldContent=_content ;
	_content=new T_Vector<HashEntry>(nextPrime(2*_content->Size())) ;
	Empty() ;
	for (int i=0;i<oldContent->Size();i++) {
		HashEntry &ae=oldContent->Get(i) ;
		if (ae.GetInfo()==ACTIVE) {
			Put(*ae.GetKey(),*ae.GetItem()) ;
		}
	}
}
Exemplo n.º 19
0
void Primer::calculateNextPrime(unsigned long num)
{
	unsigned long prime = nextPrime();
	const static unsigned int PRINT_PERIOD = 100000;
	if (num % PRINT_PERIOD == PRINT_PERIOD - 1)
	{
		std::chrono::high_resolution_clock::time_point stopTime = std::chrono::high_resolution_clock::now();
		double rate = std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(stopTime - startTime).count() / (double)PRINT_PERIOD;
		startTime = stopTime;

		printPrime(num, prime, rate);
	}
}
Exemplo n.º 20
0
xSymbolTable* xSymbolTable_create(int capacity) 
{
    capacity = (capacity <= 0 ? xSymbolTable_DEFAULT_CAPACITY : nextPrime(capacity));
    
    xSymbolTable* table = xalloc(xSymbolTable);
    table->capacity = capacity;
    table->load = 0;
    table->slot = xnalloc(xSTPair, capacity);
    for (int i = 0; i < table->capacity; i++) {
        table->slot[i].symbol = NULL;
    }
    
    return table;
}
int * GeneratePrimeNumbers (int start, int end, int * returnsize) {
	int size = 0;

	int * temp = (int *) malloc (TEMP_MAX_SIZE * sizeof(int));

	if (start > end) {
		return 0;
	}

	while (start <= end) {
		temp[size] = start;
		start = nextPrime(start);
		size++;
	}

	temp[size] = nextPrime(temp[size-1]);
	size++;

	* returnsize = size;
	int * returndata = realloc(temp, size * sizeof(int));

	return returndata;
}
Exemplo n.º 22
0
void HashTable<HashObj>::rehash() {
        std::cout<< "Entering rehash function\n"
                 << "TEST-ME: "<< std::endl;
        std::vector<HashEntry> oldArray = array;
        array.resize( nextPrime( 2* oldArray.size()));
        for( unsigned j = 0; j < array.size(); j++)
                array[j].info = EMPTY;
        currentSize=0;
        for ( unsigned i = 0; i < oldArray.size(); i++){
                if( oldArray[i].info == ACTIVE )
                        insert( oldArray[i].element);
        }
        

}
Exemplo n.º 23
0
 /**
  * Rehashing for quadratic probing hash table.
  */
 void rehash( )
 {
     vector<HashEntry> oldArray = array;
 
         // Create new double-sized, empty table
     array.resize( nextPrime( 2 * oldArray.size( ) ) );
     for( int j = 0; j < array.size( ); j++ )
         array[ j ].info = EMPTY;
 
         // Copy table over
     currentSize = 0;
     for( int i = 0; i < oldArray.size( ); i++ )
         if( oldArray[ i ].info == ACTIVE )
             insert( oldArray[ i ].element );
 }
Exemplo n.º 24
0
        void QuadraticPtrHashTable<HashedObj>::rehash( )
        {
            vector<const HashedObj*> oldArray = array;

                // Create new double-sized, empty table
            array.resize( nextPrime( 2 * oldArray.size( ) ) );
            for( int j = 0; j < array.size( ); j++ )
                array[ j ] = NULL;

                // Copy table over
            currentSize = 0;
            for( int i = 0; i < oldArray.size( ); i++ )
                if( oldArray[ i ] )
                    insert( *oldArray[ i ]);
        }
void rehash()
{
  vector<list<HashedObj>> oldLists = theLists;
  
  // Create new double-sized, empty table
  theLists.resize(nextPrime(2 * theLists.size()));
  for(auto & thisList: theLists)
    thisList.clear();
    
  // Copy table over
  currentSize = 0;
  for(auto & thisList: oldLists)
    for(auto & x: thisList)
      insert(std::move(x));
}
int main(int argc, char**argv) {
	int n = atoi(argv[1]);
	char *sieve = (char*) malloc(sizeof(char));
	int i;
	for(i = 0; i < n; i++) {
		sieve[i] = 1;
	}

	for(i = 2; i < n; i = nextPrime(i, sieve, n)) {
		printf("%d ", i);
		setNotPrime(i, sieve, n);
	}

	return 0;
}
Exemplo n.º 27
0
int prNextPrime(VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a;
	int n, p, i;

	a = g->sp;
	n = slotRawInt(a);
	i = nextPrime(n);
	p = nthPrime(i);
	if (p == 0) {
		SetNil(a);
	} else {
		SetInt(a, p);
	}
	return errNone;
}
Exemplo n.º 28
0
void HashTable<E>::rehash()
{
    cout<<endl<<"Rehashing: ="<<currentSize<<", array size"<<
    array.size()<<endl;
    printTable();
    
    vector<HashEntry> oldArray = array;
    
    array.resize( nextPrime( 2 * oldArray.size( ) ) );
    for( int j = 0; j < array.size( ); j++ )
       array[ j ].info = EMPTY;
        
    currentSize = 0;
    for( int i = 0; i < oldArray.size( ); i++ )
    if( oldArray[ i ].info == ACTIVE )
        insert( oldArray[ i ].element );
}
Exemplo n.º 29
0
size_t HashTable_ImpDetails::growBucketsForLoadFactor(size_t *capacity,
                                                      size_t  minElements,
                                                      size_t  requestedBuckets,
                                                      double  maxLoadFactor)
{
    BSLS_ASSERT_SAFE(  0 != capacity);
    BSLS_ASSERT_SAFE(  0  < minElements);
    BSLS_ASSERT_SAFE(  0  < requestedBuckets);
    BSLS_ASSERT_SAFE(0.0  < maxLoadFactor);

    const size_t MAX_SIZE_T = native_std::numeric_limits<size_t>::max();
    const double MAX_AS_DBL = static_cast<double>(MAX_SIZE_T);

    // This check is why 'minElements' must be at least one - so that we do not
    // allocate a number of buckets that cannot hold at least one element, and
    // then throw the unexpected 'logic_error' on the first 'insert'.  We make
    // it a pre-condition of the function, as some callers have contextual
    // knowledge that the argument must be non-zero, and so avoid a redundant
    // 'min' call.  The 'static_cast<double>' addresses warnings on 64-bit
    // systems.  The truncation that occurs in such cases does not impact the
    // final result, so we do not need any deeper analysis in such cases.

    double d = static_cast<double>(minElements) / maxLoadFactor;
    if (d > MAX_AS_DBL) {
        // Throw a 'std::length_error' exception if 'd' is larger than the
        // highest unsigned value representable by 'size_t'.
        StdExceptUtil::throwLengthError("The number of buckets overflows.");
    }
    for (size_t result =
             native_std::max(requestedBuckets, static_cast<size_t>(d));
         ;
         result *= 2) {
        result = nextPrime(result);  // throws if too large
        double newCapacity = static_cast<double>(result) * maxLoadFactor;
        if (static_cast<double>(minElements) <= newCapacity) {
            // Set '*capacity' to the integer value corresponding to
            // 'newCapacity', or the highest unsigned value representable by
            // 'size_t' if 'newCapacity' is larger.
            *capacity = newCapacity < MAX_AS_DBL
                            ? static_cast<size_t>(newCapacity)
                            : MAX_SIZE_T;
            return result;                                            // RETURN
        }
    }
}
Exemplo n.º 30
0
	void rehash()//再散列函数
	{
		std::vector<std::list<T>> oldLists = theLists;

		// Create new double-sized, empty table
		theLists.resize(nextPrime(2 * theLists.size()));//散列表的长度扩大比为原来长度的2倍小的质数
		for (int j = 0; j < theLists.size(); j++)//清楚散列表中的元素
			theLists[j].clear();

		// Copy table over
		currentSize = 0;
		for (int i = 0; i < oldLists.size(); i++)//把oldLists里面的元素重复插入到新的散列表中
		{
			std::list<T>::iterator itr = oldLists[i].begin();
			while (itr != oldLists[i].end())
				insert(*itr++);
		}
	}