简体   繁体   中英

Program to generate nth prime number

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
main()
{
clrscr();
int num,b,c,d,f,primeset[500],j=0;
cout<<"Enter Number (n): ";
cin>>num;
primeset[0]=2;
for(b=3;b<=1000;b++)
{

    for(c=2;c<b;c++)
    {
        d=b%c;
        if(d==0)
        {
            j=1;break;
        }
        else
        {
            j=0;
        }
    }

    if(j==0)
    {
        for(f=1;f<500;f++)
        {
            primeset[f]=b;
        }
    }
    else
    {
    //do nothing
    }
    }
    cout<<"n th Prime is "<<primeset[num-1];
    getch();
    return 0;
    }

This is my program to find the nth prime, but it does not work, please hep me. please tell me about my errors. please tell me, why my program is not working, its giving 2 for num=1 and 997 for all other values of num. tell me the errors and the code to fix them. PS:I use turbo c++

EDIT: You did not like the sieve of Eratosthenes, so here is another method using dynamic programming. This means that you use the array of primes already created to decide if the next candidate is prime.

ie if you had generated primes [2,3,5,7], to decide what 4th (0-based) prime is you calculate if the candidates 8,9,10,11 are divisible by the current list of primes. You keep going up from 8 until a candidate is not divisible by an already existing prime

Code:

void createNextPrime(int* primes, int nextPrimeIndex) {
  int primeCandidate=primes[nextPrimeIndex-1];
  do {
    primeCandidate++;
    bool found=true;
    for(int primeIndex=0;primeIndex<nextPrimeIndex;primeIndex++) {
      if (primeCandidate%primes[primeIndex]==0) {
        found=false;
        break;
      }
    }
    if (found) {
      primes[nextPrimeIndex]=primeCandidate;
      return;
    }
  }
  while(true);
}

void createNPrimes(int primeCountToCreate) {
  int primes[primeCountToCreate];
  primes[0]=2;

  for(int primeIndexToCreate=1;primeIndexToCreate<primeCountToCreate;primeIndexToCreate++) {
    createNextPrime(primes,primeIndexToCreate);
  }

  for(int i=0;i<primeCountToCreate;i++) {
    cout<< primes[i]<< " ";
  }
  cout<<endl;
}

EDIT:

This is your code, fixed to work:

int main()
{
clrscr();
int num,c,d,f,primeset[500],composite=0;
cout<<"Enter Number (n): ";
cin>>num;
primeset[0]=2;
f=1;
for(int candidate=3;candidate<=1000;candidate++)
{
    composite=0;
    for(c=2;c*c<=candidate;c++)
    {
        d=candidate%c;
        if(d==0)
        {
            composite=1;
            break;
        }
    }

    if(composite==0)
    {
      primeset[f++]=candidate;
      // Not sure what this did
        /*for(f=1;f<500;f++)
        {
            primeset[f]=b;
        } */
    }
  }
  cout<<"n th Prime is "<<primeset[num-1]<<endl;
    getch();
  return 0;
}

here is logic

number:-Enter Number prime numbers are (2,3,5,7,11)

number=100;
nth= ? ;//enter the nth number prime u want
int a=2;
int count=0;
boolean status=true;
boolean success=false;
for(int i=a;i<=number;i++)
{
  for(int j=2;j<i;j++)
  {
     if(i/j == 0)
          status=false;
  }
  if(!status)
  {
   count++;
    if(count == nth){
        //print out nth prime number
          success=true;
         break;
    } 
  }
  if(!success){
   //nth prime is beyond the limit
  }
}

The problem you describe is called the prime distribution problem and there is no formula so we are left with brute force.

This means the only way to find out which prime is to iterate through all values, although we can skip all even values since 2 is the only even prime and we have a special case for this.

#include <iostream>

bool isPrime(unsigned int number) 
{
    for (unsigned int i = 3; (i*i) <= number; i += 2) 
    {
        if (number % i == 0) 
            return false;
    }
    return true;
}

unsigned int nthPrime(unsigned int n)
{
    // Special case for checking for 1st prime which is 2.
    if (n == 1)
        return 2;

    unsigned int counter = 1;
    unsigned int number = 1;

    while (counter < n)
    {
        number += 2;
        counter += isPrime(number) ? 1 : 0;
    }
    return number;
}

int main(int argc, char** argv)
{
    unsigned int num;
    std::cout << "Nth Prime to calculate : ";
    std::cin >> num;
    std::cout << "Prime is " << nthPrime(num) << '\n';
    return 0;
}

Depending on the size of the primes you wish to be working with, you may run out of range with a 32-bit unsigned int. Substituting unsigned int with long long will go further, however, primes can get very big very quickly, so even this might not be enough. Ultimately you need to use an n-bit interger which has no fixed/finite length. This is the case in encryption keys which have huge values not directly representable with fundamental types in c++.

n-bit intergers are outside the scope of this question so I will leave to the reader to investigate this.

[EDIT:] Note that you cannot rely on the isPrime(...) function on its own since there is no checking for odd/eveness or the special cases for the first couple of primes. Its designed to be used in conjunction with the nthPrime(...) function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM