예제 #1
0
static PyObject* primes(PyObject* self, PyObject* args){
    Py_ssize_t start = 0, n = 0; size_t pi;
    if (!PyArg_ParseTuple(args, "n|n:primes", &start, &n)) return NULL;
    if (PyTuple_Size(args) == 1){
        n = start;
        start = 0;
    }
    if (start > n) return PyList_New(0);
    if (start < 2) start = 2;
    if (n < 3) return PyErr_Occurred() ? NULL : PyList_New(0);
    else if (n == 3){
        PyObject* just_two = PyList_New(1);
        PyList_SET_ITEM(just_two, 0, PyInt_FromLong(2));
        return just_two;
    }
    else if (n < 6)  pi = 2;
    else if (n < 8)  pi = 3;
    else if (n < 12) pi = 4;
    else if (n < 14) pi = 5;
    else if (n < 18) pi = 6;
    else             pi = n/(log(n)-1);
    size_t i = 0;
    PrimePyList primes(pi, &i);
    PrimeSieve ps;
    ps.generatePrimes(start, n-1, &primes);
    i--;
    while (i < --pi) PyObject_CallMethod(primes.list, (char*)"__delitem__", (char*)"(n)", pi);
    return primes.list;
}
예제 #2
0
파일: RSA.cpp 프로젝트: santoshF/RSA
int main(){
	
	//Initialize Seed
	srand(time (NULL) );	
	//Create Random Start from range 0 to 99
	int start = rand () % 99 + 1 ;
	//Create Random Stop from range 999 to 99999 (Nighty - Nine Thousand )	
	int stop =  rand () % 99999  + 999 ;
	
	cout<<"Random start "<<start <<endl;
	cout<<"Random end " <<stop <<endl;

	PrimeSieve ps;
	try
	{
		ps.generatePrimes(start ,stop ,callback);
	 //	ps.printPrimes(start ,stop );

	}catch (stop_primesieve & e  ){
	//cerr << " Error " << e.what() <<endl; 
	}
	//Random Number
	int  num1  =  rand() % 99 + 1 ; 	
	int  num2  =  rand() % 500 +  100;

	 //P and Q are 2 large random number
	long long unsigned  int  p = primes[num1];
	long long unsigned int  q = primes[num2];

	 //Calculate n
	long long unsigned int n = p * q;
	
	//Calculate phi
	long long unsigned int phi = (p -1 ) * (q -1);

	//Calculate e s.t. 
	
	cout << " num1 =	" << num1 <<endl;	
	cout << " num2 =	" << num2 <<endl;	
	cout << " p =	" << p <<endl;	
	cout << " q =	" << q <<endl;	
	cout << " n =	" << n <<endl;	
	cout << " phi =  "<< phi <<endl;	

	euclid(a,b);	

return 0;
}
예제 #3
0
static PyObject* primes_nth(PyObject* self, PyObject* args){
    Py_ssize_t n = 0;
    if (!PyArg_ParseTuple(args, "n:primes_nth", &n)) return NULL;
    if (n < 1){
        PyErr_SetString(PyExc_ValueError, "a positive integer is required");
        return NULL;
    }
    switch (n){
        case 1: return PyInt_FromLong(2);
        case 2: return PyInt_FromLong(3);
        case 3: return PyInt_FromLong(5);
        case 4: return PyInt_FromLong(7);
        case 5: return PyInt_FromLong(11);
    }
    NthPrime nthprime(n);
    PrimeSieve ps;
    try {
        ps.generatePrimes(0, n*log(n*log(n)), &nthprime);
    }
    catch (StopPrimeGeneration&) {}
    return nthprime.prime;
}