Пример #1
0
long long calc(long long limit)
{
    std::vector<bool> primes = getPrimes(limit);
    
    unsigned int maxSeq = 0;
    long long maxPrime = 0;
    for (unsigned int i = 1; i < limit; i++)
    {
        unsigned int sum = 0;
        unsigned int seq = 0;
        for (unsigned int j = i; j < limit; j++)
        {
            if (primes[j])
            {
                sum += j;
                seq += 1;
                if (sum > limit)
                    break;
            
                if (primes[sum])
                {
                    if (seq > maxSeq)
                    {
                        maxSeq = seq;
                        maxPrime = sum;
                    }
                }
            }
        }
    }
    return maxPrime;
}
int main(){

    const long N = 1e5 + 2;
    std::vector<long> v(N, 0);
    long n; scanf("%ld", &n);

    std::vector<long> primeList = getPrimes(N + 1);

    while(n--){
        long a; scanf("%ld", &a);

        for(int p = 0; p < primeList.size(); p++){
            if(a < p){break;}
            long cur = primeList[p];
            if(a % cur != 0){continue;}
            ++v[cur];
            while(a % cur == 0){a /= cur;}
        }
    }

    long max = 1;
    for(long p = 2; p < N; p++){max = (max > v[p]) ? max : v[p];}
    printf("%ld\n", max);

    return 0;
}
Пример #3
0
unsigned int calc(unsigned int limit)
{
    unsigned int count = 0;
    std::vector<bool> primesBool = getBoolPrimes(limit);
    std::vector<unsigned int> primes = getPrimes(primesBool, limit);
    
    for (unsigned int i = 0; i < primes.size(); i++)
    {
        std::string s = std::to_string(primes[i]);
        unsigned int l = s.size();
        bool circPrime = true;
        for (unsigned int i = 0; i < l; i++)
        {
            s = rotate(s);
            int n = std::stoi(s);
            if (!primesBool[n])
            {
                circPrime = false;
                break;
            }
        }
        if (circPrime)
            count++;
    }
    return count;
}
Пример #4
0
int main(){
  int i, x;

  getPrimes();
  printf("%d primes between 1 to %d\n", psize, MAXN);
  while(scanf("%d", &x) == 1){
    if(isPrime(x)) printf("%d is prime\n", x);
    else printf("%d is not prime\n", x);
  }
  return 0;
}
Пример #5
0
int main(int argc, char *argv[]) {
    int n;
    printf("Please input num to add primes up through: ");
    scanf("%d", &n);

    int *primes = getPrimes(n);
    long int sum = sumPrimes(primes, n);

    printf("Sum is: %ld", sum);
    free(primes);
    return 0;
}
Пример #6
0
int _main()
{
	call_I_Love_Threads();

	vector<int> primes1;
	getPrimes(58, 100, primes1);

	vector<int> primes3 = callGetPrimes(93, 289);

	callWritePrimesMultipleThreads(1, 100000, "primes2.txt", 2);

	system("pause");
	return 0;
}
Пример #7
0
int main(){
  Factors x;
  int i, n;

  getPrimes();
  while(scanf("%d", &n) == 1 && n > 1){
    x =  getPFactor(n);
    printf("%d = ", n);
    for(i = 0; i < x.size; i++){
      if(i) printf(" x ");
      printf("%d", x.f[i]);
    }
    printf("\n");
  }
  return 0;
}
Пример #8
0
main()
{
	//an array of integers
	long integers[100000];
	double starttime, endtime, fulltime;
	//loop counters
	long i,h;
	//buffer
	FILE* fd;
	
	gettimeofday(&secs,&zone);
	starttime = secs.tv_sec + secs.tv_usec/(double)1000000;
	
	//use the sieve method to get the primes up to 100000
	getPrimes(100000,integers);
	
	gettimeofday(&secs,&zone);
	endtime = secs.tv_sec + secs.tv_usec/(double)1000000;
	fulltime = endtime - starttime;
	printf("Getting primes... %f secs\n",fulltime);
	
	//open/create file for writing
	fd = fopen("primes","w");

	//write 2 to file
	fprintf(fd,"%d",2);

	h = 1;
	
	for(i=2; i<100000; i+=2)
	{
		if (integers[i] == UNSTRUCK)
		{
			//write prime to file
			fprintf(fd,"\n%ld",(i+1));
			h++;
		}
	}
		
	printf("Number of primes... %ld\n",h);

	//close the file
	fclose(fd);
}
Пример #9
0
int main()
{
    int t,i,j,l,h,factors,max,currentPrime,cuurentNumber,best,c,counter;
    getPrimes();
    scanf("%d", &t);
    for(i = 0 ; i < t ; i++)
    {
        max = 0;
        scanf("%d %d", &l,&h);
        for(j = l ; j <= h ; j++)
        {
            counter = 0;
            factors = 1;
            cuurentNumber = j;
            while(counter < 3401 && primes[counter] <= cuurentNumber)
            {
                c=0;
                currentPrime = primes[counter];
                while(cuurentNumber %  currentPrime ==0)
                {
                    c++;
                    cuurentNumber/=currentPrime;
                }
                factors *= (c+1);
                counter++;
            }
            if(cuurentNumber != 1)
            {
                factors *= 2;
            }
            if(max < factors)
            {
                max = factors;
                best = j;
            }
        }
        printf("Between %d and %d, %d has a maximum of %d divisors.\n", l,h,best,max);
    }

    return 0;
}
Пример #10
0
/*******************************************************************************************************************//**
 * \brief   Gets all prime factors of a number.
 *
 * Uses trial division algorithm.
 * See http://en.wikipedia.org/w/index.php?title=Trial_division&oldid=518625973.
 *
 * \param   n  The number to be factorized.
 *
 * \pre     n > 0
 *
 * \return  The prime factors in ascending order.
 **********************************************************************************************************************/
std::vector<uint_t> getPrimeFactors( const uint_t n )
{
   WALBERLA_ASSERT( n != 0 );

   auto primes = getPrimes(n);
   std::vector<uint_t> primeFactors;

   uint_t n_rest = n;
   for(auto primeIt = primes.begin(); primeIt != primes.end(); ++primeIt)
   {
      if( *primeIt * *primeIt > n )
         break;
      while( n_rest % *primeIt == 0)
      {
         n_rest /= *primeIt;
         primeFactors.push_back(*primeIt);
      }
   }

   if( n_rest != 1 )
      primeFactors.push_back(n_rest);

   return primeFactors;
}
Пример #11
0
int run()
{
	getPrimes();
	std::cout << sum;
	return 0;
}
Пример #12
0
//This is the main function.  It takes arguments from the command line to determine
//the wheel size to use and whether or not to print out the primes.
int main(int argc, char *argv[])
{
    int wheelSize;
    u_int64_t maxNum;

    //Checks the program was passed the proper number of arguments
    if (( argc < 3 ) || ( argc > 4))
    {
        printInstructions(argv[0]);
        return 1;
    }

    //Get command line arguments
    maxNum = strtol(argv[1], NULL, 0);;
    wheelSize = atoi(argv[2]);

    //Checks the passed arguments are all integers and within bounds
    if ((maxNum == 0) || (maxNum > MAX_NUMBER) || (wheelSize <= 0) || (wheelSize > 6))
    {
        printInstructions(argv[0]);
        return 1;
    }

    int i;
    int j;

    for (j = 0; j < PARRAY_SIZE; j++)
    {
        lastNum[j] = 0;
        lastCycle[j] = 1;
    }

    //This conditional makes sure we get all the primes desired
    if (maxNum < 210)
    {
        //If we're looking for a small number of primes, just sieve up to 210
        maxSlots = 21;
    }
    else
    {
        if (maxNum%10 == 0)
        {
            maxSlots = maxNum/10;
        }
        else
        {
            maxSlots = maxNum/10+1;
        }
    }

    //Checks that the wheel size doesn't exceed the number of slots we're using
    u_int64_t wheelCheck = getWheelSize(wheelSize);
    if (wheelCheck > maxSlots)
    {
        printf("Error: Wheel size specified is larger than the Max slots specified.\n");
        printf("Please try smaller wheel size\n");
        return 1;
    }

    //malloc memory for the table, which is a bit field of 8-bit integers, that keeps
    //track of all the primes
    if ((table = (u_int8_t *) malloc(maxSlots*sizeof(u_int8_t))) == NULL)
    {
        printf("Error: problem allocating memory for the table\n");
        return 1;
    }

    //The first four primes are hardcoded.
    primes[0] = 2;
    primes[1] = 3;
    primes[2] = 5;
    primes[3] = 7;

    //Set the values in the table for 3's cycle.  3 generates (Z/10,+) (The group of
    //integers mod 10 under addition) every 30 numbers.
    table[0] = 5;
    table[1] = 15;
    table[2] = 10;

    //Set tabslot to 21 since 3's cycle takes 30 numbers, 7's cycle takes 70 numbers.
    //Their cycles both start with no overlap at 210, which is 3*7*10.
    tabslot = 21;

    //Copy the values for 3's cycle until it completes through 210
    for (i = 3; i<tabslot; i+=3)
    {
        table[i] = table[0];
        table[i+1] = table[1];
        table[i+2] = table[2];
    }

    //Remove 3 as a potential prime for future cycles
    table[0] = 1;

    //
    switch (maxSlots%3)
    {
    case 0:
    {
        //We're already done
        break;
    }
    case 1:
    {
        table[i] = table[0];
        break;
    }
    case 2:
    {
        table[i] = table[0];
        table[i+1] = table[1];
        break;
    }
    default:
    {
        printf("Error: hit the default case while filling the table with 3's wheel\n");
        return 1;
    }
    }

    //Get primes up to 49
    getPrimes(3);

    //Mark off wheels up to wheelSize and then roll the wheel to maxSlots
    int nextPrime = rollWheel(wheelSize, 3);

    //Get primes up to the square of the next prime number
    getPrimes(nextPrime);
    nextPrime++;

    //Determine if single or multithreaded and mark off remaining composites
    //If BLOCK_SIZE>maxSlots, just ignore NUM_THREADS and use single thread
    if ((NUM_THREADS == 1) || (BLOCK_SIZE > maxSlots))
    {
        finishPrimes(nextPrime);
    }
    else if (NUM_THREADS > 1)
    {
        multiFinishPrimes(nextPrime);
    }
    else
    {
        printf("Error: NUM_THREADS must be greater than or equal to 1\n");
        printf("Please change the value of NUM_THREADS and recompile\n");
        exit(-1);
    }

    //If a fourth argument is supplied at runtime, print the list of primes.
    if (argv[3])
    {
        singlePrintPrimes(maxNum);
    }

    //Cleanup
    //free(table);

}
Пример #13
0
//Remove all potentially prime multiples of all primes until prime*prime is greater
//than the maximum number of slots in table.  This is the multi-threaded version.
void multiFinishPrimes(int currPrime)
{
    pthread_t tid[NUM_THREADS];
    int threadCount = 0;
    int i;
    int blockNum;
    int pindex = currPrime;
    startIndex = currPrime;
    u_int64_t stop =  sqrt(maxSlots*10); //stops when prime*prime>maxSlots
    u_int64_t minSize = BLOCK_SIZE;

    //Checks that we only find primes up to maxSlots
    //if (BLOCK_SIZE > maxSlots)
    //{
    //minSize = maxSlots;
    //}

    int blockCounter = BLOCK_SIZE;
    //Keep removing multiples of primes from the first block of the table.
    //It also keeps getting primes from finished entries of the table.
    //It does this until it finds a prime such that prime > (maxSlot)^(1/2).
    //If we need more primes than are in the first block, it continues sieving,
    //block by block, until the condition above is met.  This is still single threaded,
    //so there could be a slight performance gain by multi-threading this code, especially
    //for large maxSlots.
    while (primes[currPrime] <= stop)
    {
        //If we need primes larger than BLOCK_SIZE, sieve the next block and keep getting
        //primes.
        if (primes[currPrime]>blockCounter)
        {
            currPrime = startIndex;
            blockCounter += BLOCK_SIZE;
            minSize = blockCounter;
        }

        //Remove multiples of the current prime
        singleRemoveComposites(currPrime, minSize);
        currPrime++;

        //If we've sieved this block using all the primes we've gotten, get more primes
        //and continue sieving
        if (primeCount-currPrime == 0)
        {
            getPrimes(currPrime-1);
        }
    }

    //lastPrimeIndex is the index of the greatest prime such that prime*prime <= maxSlots
    lastPrimeIndex = currPrime-1;
    u_int64_t thisBlock;

    //Determine remaining blocks to be sieved
    int totalBlocks = maxSlots/BLOCK_SIZE-blockCounter/BLOCK_SIZE+1;

    //Increment blockCounter so we start sieving the next block.
    blockCounter += BLOCK_SIZE;

    if (totalBlocks > NUM_THREADS) //Use all threads
    {
        //Spawn all threads
        for (i=0; i < NUM_THREADS; i++)
        {
            pthread_create(&tid[i], NULL, primeThread, (void*) ((u_int64_t) blockCounter+(i*BLOCK_SIZE)));
        }

        //Wait for threads to complete
        for (i=0; i<NUM_THREADS; i++)
        {
            pthread_join(tid[i], NULL);
        }
    }
    else //We only need a few threads given the blocksize and limit
    {
        //Spawn necessary threads
        for (i=0; i < totalBlocks-1; i++)
        {
            pthread_create(&tid[i], NULL, primeThread, (void*)((u_int64_t) blockCounter+(i*BLOCK_SIZE)));
        }

        pthread_create(&tid[totalBlocks-1], NULL, primeThread, (void*) maxSlots);

        //Wait for threads to complete
        for (i=0; i < totalBlocks; i++)
        {
            pthread_join(tid[i], NULL);
        }
    }

    //At this point, we've removed all composite numbers from the table.
}
Пример #14
0
//Remove all potentially prime multiples of all primes until prime*prime is greater
//than the maximum number of slots in table.  This is the single threaded version.
void finishPrimes(int currPrime)
{
    int i;
    int blockNum;
    int pindex = currPrime;
    int startIndex = currPrime;
    u_int64_t stop =  sqrt(maxSlots*10); //stops when prime*prime>maxSlots
    u_int64_t minSize = BLOCK_SIZE;

    //Checks that we only find primes up to maxSlots
    if (BLOCK_SIZE > maxSlots)
    {
        minSize = maxSlots;
    }

    int blockCounter = BLOCK_SIZE;

    //Keep removing multiples of primes from the first block of the table.
    //It also keeps getting primes from finished entries of the table.
    //It does this until it finds a prime such that prime > (maxSlot)^(1/2).
    //If we need more primes than are in the first block, it continues sieving,
    //block by block, until the condition above is met.
    while (primes[currPrime] <= stop)
    {
        //If we need primes larger than BLOCK_SIZE, sieve the next block and keep getting
        //primes.
        if (primes[currPrime]>blockCounter)
        {
            currPrime = startIndex;
            blockCounter += BLOCK_SIZE;
            minSize = blockCounter;
        }

        //Remove multiples of the current prime
        singleRemoveComposites(currPrime, minSize);
        currPrime++;

        //If we've sieved this block using all the primes we've gotten, get more primes
        //and continue sieving
        if (primeCount-currPrime == 0)
        {
            getPrimes(currPrime-1);
        }
    }

    //Increment blockCounter so we start sieving the next block.
    blockCounter += BLOCK_SIZE;

    //lastPrimeIndex is the index of the greatest prime such that prime*prime <= maxSlots
    lastPrimeIndex = currPrime-1;
    u_int64_t thisBlock;

    //Continue sieving, one block at a time.  thisBlock keeps track of what block we're
    //currently sieving
    for (blockNum = blockCounter/BLOCK_SIZE; (thisBlock = blockNum*BLOCK_SIZE) < maxSlots; blockNum++)
    {
        //This loop sieves a block.
        for (i=startIndex; i<= lastPrimeIndex; i++)
        {
            singleRemoveComposites(i, thisBlock);
        }
    }

    //Sieve the last block if it hasn't already been done
    if (minSize == BLOCK_SIZE)
    {
        for (i=startIndex; i<= lastPrimeIndex; i++)
        {
            singleRemoveComposites(i, maxSlots);
        }
    }
    //At this point, we're removed all composite numbers from the table.
}
Пример #15
0
int main()
{
	std::vector<unsigned long> primeNumbers;
	unsigned int primeRange = 1000;

	std::cout << "Calculating primes... " << std::endl;
	getPrimes(primeRange, primeNumbers);
	std::cout << "Done calculating primes... " << std::endl;

	std::cout << "Started the search at " << getTime() << std::endl;

	unsigned long numberOne = 500;
	while(true)
	{
		std::vector<unsigned long> divisorsOfNumberOne;
		
		findPrimeDivisors(primeNumbers, numberOne, divisorsOfNumberOne);
		if (divisorsOfNumberOne.size() < 4)
		{
			numberOne += 1;
			continue;
		}

		unsigned long numberTwo = numberOne + 1;
		std::vector<unsigned long> divisorsOfNumberTwo;

		findPrimeDivisors(primeNumbers, numberTwo, divisorsOfNumberTwo);
		if (divisorsOfNumberTwo.size() < 4)
		{
			numberOne += 1;
			continue;
		}

		unsigned long numberThree = numberTwo + 1;
		std::vector<unsigned long> divisorsOfNumberThree;

		findPrimeDivisors(primeNumbers, numberThree, divisorsOfNumberThree);
		if (divisorsOfNumberThree.size() < 4)
		{
			numberOne += 1;
			continue;
		}

		unsigned long numberFour = numberThree + 1;
		std::vector<unsigned long> divisorsOfNumberFour;

		findPrimeDivisors(primeNumbers, numberFour, divisorsOfNumberFour);
		if (divisorsOfNumberFour.size() < 4)
		{
			numberOne += 1;
			continue;
		}

		/*bool intersectionFound = doVectorsIntersect(divisorsOfNumberOne, divisorsOfNumberTwo, divisorsOfNumberThree, divisorsOfNumberFour);
		if (intersectionFound)
		{
			numberOne += 1;
			continue;
		}

		intersectionFound = doVectorsIntersect(divisorsOfNumberTwo, divisorsOfNumberOne, divisorsOfNumberThree, divisorsOfNumberFour);
		if (intersectionFound)
		{
			numberOne += 1;
			continue;
		}

		intersectionFound = doVectorsIntersect(divisorsOfNumberThree, divisorsOfNumberOne, divisorsOfNumberTwo, divisorsOfNumberFour);
		if (intersectionFound)
		{
			numberOne += 1;
			continue;
		}

		intersectionFound = doVectorsIntersect(divisorsOfNumberFour, divisorsOfNumberOne, divisorsOfNumberTwo, divisorsOfNumberThree);
		if (intersectionFound)
		{
			numberOne += 1;
			continue;
		}*/

		std::cout << "Finished the search at " << getTime() << std::endl;
		std::cout << "Number one: " << numberOne << std::endl;
		std::cout << "Number two: " << numberTwo << std::endl;
		std::cout << "Number three: " << numberThree << std::endl;
		std::cout << "Number four: " << numberFour << std::endl;
		break;
	}

	return EXIT_SUCCESS;
}