Exemplo n.º 1
0
int PrimeFactor(long x)
{
  /* Start with the first prime */
  int prime = 2;

  if (IsPrime(x))
  {
    printf("Found prime factor %ld\n", x);
    return x;
  }

  /* Find the next prime that divides cleanly */
  while ( x % prime != 0 )
    prime = NextPrime(prime);

  printf("Factoring %ld, found prime %d\n", x, prime);

  x /= prime; /* Divide the value by the prime factor */

  /* Shall we continue? */
  if (!IsPrime(x))
    PrimeFactor(x);
  else
    printf("Found prime factor %ld\n", x);

  return x;
}
Exemplo n.º 2
0
HashTable InitializeTable(int TableSize)
{
	HashTable H;
	int i;

	if(TableSize < MinTableSize)
	{
		Error("Table Size too Small");
		return NULL;
	}

	H = malloc(sizeof(struct HashTable));
	if(H == NULL)
		FatalError("Out of space!");

	H->TableSize = NextPrime(TableSize);

	H->TheLists = malloc(sizeof(List) * H->TableSize);
	if(H->TheLists == NULL)
		FatalError("Out of space!");

	for(i = 0; i < H->TableSize; i++)
	{
		H->TheLists[i] = malloc(sizeof(struct ListNode));
		if(H->TheLists[i] == NULL)
			FatalError("Out of space!");
		else
			H->TheLists[i]->Next = NULL;
	}
}
Exemplo n.º 3
0
HashTable InitializeTable(int TableSize) {
	HashTable H;
	int i;

	if (TableSize < MinTableSize) {
		Error("Table size too small");
		return NULL;
	}

	H = malloc(sizeof(struct HashTbl));
	if (H == NULL) {
		FatalError("Out of space!!!");
	}

	H->TableSize = NextPrime(TableSize);
	H->TheCells = malloc(sizeof(Cell) * H->TableSize);
	if (H->TheCells == NULL) {
		FatalError("Out of space!!!");
	}
	for (i = 0; i < H->TableSize; i++) {
		H->TheCells[i].Info = Empty;
	}

	return H;
}
Exemplo n.º 4
0
/*InitializeTable分离链接散列表的初始化*/
HashTable InitializeTable(int TableSize){
    HashTable H;
    int i;

    if(TableSize < MinTableSize){
        printf("Table is too small\n");
        return NULL;
    }

    H = malloc(sizeof(HashTbl));
    if(H != NULL){
        H->TableSize = NextPrime(TableSize);

        H->TheList = malloc(sizeof(List) * H->TableSize);
        if(H->TheList == NULL){
            printf("Out of spzce!!!\n");
        }

        for(i = 0;i < H->TableSize;i++){
            H->TheList[i] = malloc(sizeof(LinkNode));

            if(H->TheList[i] == NULL){
                printf("Out of space!!!\n");
            }else{
                H->TheList[i]->nextPtr = NULL;
            }
        } 
    }else{
        printf("Out of space!!!\n");
    }
    return H;
}
Exemplo n.º 5
0
// p is prime and p = 1 mod 4
vector<ZZ> CommonFunctions::decomposePrime(const ZZ &p, int stat) {
	ZZ b;
	if((p % 8) == to_ZZ(5)) {
		b = to_ZZ(2);
	}
	else {
		b = to_ZZ(3);
		while(PowerMod(b, (p-1)/2, p) == 1) {
			// next prime(b) returns the smallest prime larger than b
			b = NextPrime(b, stat);
		}
	}

	b = PowerMod(b, (p-1)/4, p);
	// b is now an imaginary unit, i.e. b^2 = -1 mod p

	ZZ a;
	a = p;
	while(power(b, 2) > p) {
		ZZ temp = a;
		a = b;
		b = temp % b;
	}
	// cout << "a : " << a << "   b : " << b << endl;
	vector<ZZ> twoSquares;
	twoSquares.push_back(b);
	twoSquares.push_back(a % b);
	return twoSquares;
}
Exemplo n.º 6
0
/* START: fig5_15.txt */
        HashTable
        InitializeTable( int TableSize )
        {
            HashTable H;
            int i;

/* 1*/      if( TableSize < MinTableSize )
            {
/* 2*/          printf( "Table size too small" );
/* 3*/          return NULL;
            }

            /* Allocate table */
/* 4*/      H = malloc( sizeof( struct HashTbl ) );
/* 5*/      if( H == NULL )
/* 6*/          printf( "Out of space!!!" );

/* 7*/      H->TableSize = NextPrime( TableSize );

            /* Allocate array of Cells */
/* 8*/      H->TheCells = malloc( sizeof( Cell ) * H->TableSize );
/* 9*/      if( H->TheCells == NULL )
/*10*/          printf( "Out of space!!!" );

/*11*/      for( i = 0; i < H->TableSize; i++ )
/*12*/          H->TheCells[ i ].Info = Empty;

/*13*/      return H;
        }
Exemplo n.º 7
0
//初始化
HashTable InitHashTable(int TableSize)
{
	HashTable H;
	if (TableSize < MinTableSize)
	{
		printf("Error! Table size too small!\n");
		return NULL;
	}
	//分配空间给散列表
	H = HashTable(malloc(sizeof(struct HashTbl)));
	if (H == NULL)
		printf("Error! Out of space!\n");
	H->TableSize = NextPrime(TableSize);
	//分配空间给每一个链表
	H->TheLists = (List*)(malloc(sizeof(List)*H->TableSize));
	if (H->TheLists == NULL)
		printf("Error! Out of space!\n");
	for (int i = 0; i < H->TableSize; ++i)
	{
		H->TheLists[i] = List(malloc(sizeof(struct ListNode)));
		if (H->TheLists[i] == NULL)
			printf("Error! Out of space!\n");
		else
			H->TheLists[i]->next = NULL;
	}

	return H;
}
Exemplo n.º 8
0
HashTable
InitializeTable(int TableSize)
{
  HashTable H;
  int i;

  H = malloc(sizeof(struct HashTbl));

  if (H == NULL) {
    FatalError("No memory space!");
  }

  H->TableSize = NextPrime(TableSize);
  H->TheCells = malloc(sizeof(Cell) * H->TableSize);

  if (H->TheCells == NULL) {
    FatalError("No memory space!");
  }

  for (i = 0; i < H->TableSize; i++) {
    H->TheCells[i].Info = Empty;
    H->TheCells[i].Element = 0;
  }

  return H;
}
HashTable InitializTable( int TableSize ){
	HashTable H;
	int i;
	H = malloc( sizeof( struct HashTbl ) );
	if( H == NULL ){
		printf( "Out of space" );
		exit( -1 );
	}
	H->TableSize = NextPrime( TableSize );
	printf( "%d\n",H->TableSize );
	H->TheLists = malloc( sizeof( struct HashTbl ) * TableSize );
	if( H->TheLists == NULL ){
		printf( "Out of space" );
		exit( -1 );
	}
	for( i = 0;i < H->TableSize;i++ ){
		H->TheLists[i] = malloc( sizeof( struct ListNode ) );
		if( H->TheLists[i] == NULL ){
			printf( "Out of space" );
			exit( -1 );
		}else{
			H->TheLists[i] = NULL;
		}
	}
	return H;
}
Exemplo n.º 10
0
Int Primes::GenPrime(Int size, boost::mt19937 *randNumGen)
{
	Int beg = 1;

	boost::uniform_int<> degen_dist(1 << (size-1), 1 << size);
	boost::variate_generator<boost::mt19937&, boost::uniform_int<> > sampler(*randNumGen, degen_dist);

	beg = sampler();
	beg = NextPrime(beg);

	return beg;
}
Exemplo n.º 11
0
HashTable* InitHashTable(int n) {
    HashTable* h;
    int i;
    h = (HashTable *)malloc(sizeof(HashTable));
    h->size = NextPrime(n);                                         /* Better be prime */
    h->list = (ListNode **)malloc(sizeof(ListNode *) * h->size);
    for( i = 0; i < h->size; i++ ) {                                /* Allocate list headers */
        h->list[i] = (ListNode *)malloc(sizeof(ListNode));
        h->list[i]->next = NULL;
    }
    return h;
}
Exemplo n.º 12
0
NTL_CLIENT

// Программа для генерации задачи дискретного логарифмирования заданной размерности 
// Образующая и модуль - D1 гладкие

int main() {
ZZ a,b,p,r; //основание, степень, модуль, порядок циклической группы, образованной основанием
ZZ x; //Показатель
long len; //Длина модуля

//Спросим длину модуля
//Фактически, модуль будет иметь длину len+1 двоичных разрядов
cin >> len;

//Простое число Жермен - p - простое и 2*p+1 - тоже простое
//Генерируем число заданной размерности (в двоичных разрядах)
p=GenGermainPrime_ZZ(len);

//Порядок циклической группы будет p-1. Мы будем искать образующую :)
//Всё так, как учил нас Великий Шнайер в Красной Книге
r=2*p;
//Модуль
p=2*p+1;

a=2;
//Найдём образующую
while ((PowerMod(a,2,p)==1) || (PowerMod(a,(p-1)/2,p)==1)) {
	a=NextPrime(a+1);
	}

//Получим степень b
b=2;
if (b==a)
	b=NextPrime(b+1);

//Выдадим задачу на стандартный вывод
cout << a << endl << b << endl << p << endl << r << endl;
//Вот программе и конец, а кто - кодил - молодец!
return 0;
}
Exemplo n.º 13
0
state_probability_queue::state_probability_queue(unsigned long mas)
:  max_active_states((unsigned long) (mas * gPercentActiveStates))
{

  stateArray = new state_and_probability_pair[max_active_states];

  hashtable_size = NextPrime((unsigned long) mas);

  table = new state_and_probability_pair[hashtable_size];

  Full = new dynBitVec(hashtable_size);

#ifndef SPLITFILE
  if ((paging_file_top = tmpfile()) == NULL) {
    Error.Notrace("Internal: Error creating top paging file.");
  }

  if ((paging_file_bottom = tmpfile()) == NULL) {
    Error.Notrace("Internal: Error creating bottom paging file.");
  }
#else
  paging_file_top = new splitFile(SPLITFILE_LEN, false);
  paging_file_bottom = new splitFile(SPLITFILE_LEN, false);

  if (!
      (paging_file_top->open
       (make_unique_filename(SFQ_PAGING_FILE_TOP), "w+b"))) {
    Error.Notrace("Internal: Error creating top paging file for s_f_q.");
  }

  if (!
      (paging_file_bottom->open
       (make_unique_filename(SFQ_PAGING_FILE_BOTTOM), "w+b"))) {
    Error.Notrace("Internal: Error creating bottom paging file.");
  }
#endif

  num_elts_head = num_elts_tail = 0;

  head_begin = 0;
  tail_begin = max_active_states / 2;

  head_size = max_active_states / 2;
  tail_size = max_active_states - head_size;

  global_front = global_rear = front = rear = 0;
}
Exemplo n.º 14
0
void DynamicSieve<T,TSmall>::SetStorage( bool keepAll )
{
    if( keepAll && !keepAll_ )
    {
        // Ensure that we have all of the primes below lowerBound_ stored
        if( lowerBound_ > oddPrimes.back()+2 )
        {
            T lowerBoundSave = lowerBound_;
            lowerBound_ = oddPrimes.back()+2;
            MoveSegmentOffset( lowerBound_ );

            keepAll_ = true;
            while( lowerBound_ < lowerBoundSave )
                NextPrime(); 
        }
    }
    keepAll_ = keepAll;
}
Exemplo n.º 15
0
PHASHTABLE InitializeTable(unsigned int tableSize)
{
	PHASHTABLE pHashTable = NULL;
	PTWOWAY pNode = NULL;
	unsigned int i;

	// Allocate space for the hashtable
	pHashTable = ExAllocatePoolWithTag(NonPagedPool, sizeof(HASHTABLE), DRIVERTAG1);
	if (pHashTable == NULL)
	{
		DbgPrint("ExAllocatePoolWithTag returned NULL!\n");
		return NULL;
	}

	pHashTable->tableSize = NextPrime(tableSize);

	// Allocate array of pointers to linkedlists 
	pHashTable->pListHeads = ExAllocatePoolWithTag(NonPagedPool, sizeof(PLIST_ENTRY) * pHashTable->tableSize, DRIVERTAG1);
	if (pHashTable->pListHeads == NULL)
	{
		DbgPrint("ExAllocatePoolWithTag returned NULL!\n");
		return NULL;
	}

	// Allocate space for the lookaside list for the TWOWAY-structures.
	pLookasideList_TWOWAY = ExAllocatePoolWithTag(NonPagedPool, sizeof(NPAGED_LOOKASIDE_LIST), DRIVERTAG1);
	if (pLookasideList_TWOWAY == NULL)
	{
		DbgPrint("ExAllocatePoolWithTag returned NULL!\n");
		return NULL;
	}

	// Initialize the lookaside list.
	ExInitializeNPagedLookasideList(
		pLookasideList_TWOWAY,
		NULL,
		NULL,
		0,
		sizeof(TWOWAY),
		DRIVERTAG1,
		0);

	// Allocate empty nodes for the each linked list.
	for (i = 0; i < pHashTable->tableSize; i++)
	{
		pNode = ExAllocateFromNPagedLookasideList(pLookasideList_TWOWAY);
		if (pNode == NULL)
		{
			DbgPrint("ExAllocateFromNPagedLookasideList returned NULL!\n");
			return NULL;
		}
		else
		{
			pNode->key = 0x00000000;
			RtlZeroMemory(&pNode->data, sizeof(ELEMENT));
			InitializeListHead(&pNode->linkfield);
		}
		pHashTable->pListHeads[i] = &pNode->linkfield;
	}

	return pHashTable;
}
void main(){
    unsigned Mersenne[500], S[500], T[500], quot[500], res[500];
    int a[500];
    int p, j, k, t, d, r;

    p = 5;

    while(p <525){
        
        Initialize(Mersenne);
        Mersenne[0] = 1;
        LongLeftShift(Mersenne, p);
        Initialize(a);
        a[0] = 1;
        Sub(Mersenne, a);       // Mersenne = 2 の p 乗 - 1

        printf("p = %d  \n", p); 
        // Display(Mersenne);

        Initialize(S);
        S[0] = 4;

        for(j = 1; j<p-1; j++){

            Copy(S, a);
            LongMul(S, a);      //  S -> S^2

            Initialize(a);
            a[0] = 2;
            Sub(S, a);          //  S -> S^2 - 2


            LongDiv(S, Mersenne, quot, res);
            if(DivCheck(S, Mersenne, quot, res)==0) {
                    printf("DivCheck Failed\n");
                    return;
            }

 

            Copy(S,T);
            LongRightShift(T, p);

            t = p/16;
            r = p - 16*t;
            d = Degree(S);
            for(k = d; k>t; k--){
                S[k] = 0;
            }
            S[t] = (S[t]<<(32-r))>>(32-r);
            Add(S, T);
            k = Compare(S, Mersenne);
            if(k==1 || k==0){
                Sub(S, Mersenne);
            }


            k = Compare(res, S);
            if(k!=0) {
                printf("Something is wrong\n");
                return;
            }

 

        }
        if(ZeroCheck(S)==0) printf("mersenne,   p = %d\n", p);
    
        p = p+2;
        p = NextPrime(p);   
    }
}