1

I have to print the Nth prime number. For example:
1st prime number is 2
2nd prime number is 3
.
.
10th prime number is 29 and so on....

My algorithm is as follows:
1) Add 2 and 3 to stack.
2) If n <= Size of stack, get item at position n and output answer
3) Else, start from last element of stack, check if prime
4) To check for prime, divide from each element in stack. If remainder 0 for any element in stack, not a prime. Break loop
5) If Prime, add to stack
6) Search only odd numbers

My code is :

#include <iostream>
using namespace std;

int main() {
    int number, count = 0;
    cin >> number;          //position of prime number
    int a[number];
    a[0] = 2;
    a[1] = 3;
    int top = 1;
    if (number <= 2) {
        cout << a[number - 1] << endl;
    } else {
        for (int i = 5; i <= 10001; i += 2) {
            for (int j = 0; j <= top; j++) {

                if (i % a[j] != 0) {
                    count++;

                }
                if (count == (top + 1)) {
                    a[++top] = i;
                    if ((count + 1) == number) {
                        cout << a[top];
                        break;
                    }

                }

            }

        }

    }
    return 0;
}

This code abruptly stops working without giving any output.What is the flaw in my code?

user6889367
  • 123
  • 2
  • 7
  • Why, if you increment always by two and you have ensured your number will be odd, to divide by the first number in the array? That check is unnecessary. – Luis Colorado Oct 26 '16 at 06:14
  • @LuisColorado because aprime number will always be odd...basically to reduce time complexity – user6889367 Oct 28 '16 at 14:13
  • @LuisColorado But this code still takes time for larger number.Can you help me? – user6889367 Oct 28 '16 at 14:16
  • I'd go through the code by hand or using a debugger and see how the variables are set. Especially the variable count. – gnasher729 Oct 30 '16 at 16:54
  • you can use "strong pseudoprimality tests" based on fermat's little theorem, just google on with those search strings. They are not mathematical probes, but probabilistical, and you are able to reduce probability of false positives as much as you want (for example one part y `1/10^2000` of being compose number). This is what enciphering software normally uses. And allows you to test numbers as long as thousands of digits long. – Luis Colorado Nov 01 '16 at 08:15
  • also, look for "eratostenes sieve" algorithm, which is more memory consuming but faster for complete and `1` to `N` prime finding. – Luis Colorado Nov 01 '16 at 08:17

3 Answers3

1

It's a problem with your looping logic. You need to trial divide i by all the numbers from 0 to top on your stack, and only if i is divisible by none of them do you increase count. As it is you are increasing it if it is indivisible by any of them.

So, change the logic to test ifi is divisible by a[j]. If it is, then break out of the loop. If you reach the end of the loop (j == top) and it hasn't successfully divided any of them then you know it's prime and you can increase count. Also, the check where you compare count to top should be outside of the j loop (i.e. after you have done all the trial divisions)

    for (int i = 5; i <= 10001; i += 2) {
        for (int j = 0; j <= top; j++) {
            if (i % a[j] == 0) {
                break;
            }
            if(j == top)
            {
                count++;
                a[++top] = i;
                break;
            }
        }
        if (count == number) {
            cout << a[top];
            break;
        }
    }

Edit: you also need to initialize count to 2, not 0, to account for 2 and 3.

samgak
  • 23,944
  • 4
  • 60
  • 82
0

int a[number]; is wrong as number is not a constant. Rather you would like to write it as int *a=new int[number]

macroland
  • 973
  • 9
  • 27
  • No,the code is still not working...but int a[number] is not an issue i guess,i have tried that in many other codes and it has worked – user6889367 Oct 25 '16 at 02:29
  • @macroland, I'm afraid in C++ it's allowed to use an automatic array whose length is determined at runtime. The question is tagged C++ and not C, so I think that's not an error here. By the way, does this answer address the problem being asked? – Luis Colorado Oct 26 '16 at 06:17
  • @LuisColorado: You are right, I realized that later on. – macroland Oct 27 '16 at 08:28
0
    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    int j=2;
    boolean bool=true;
    int counter=0;
    int i=1;
    while(counter<n)
    {
        i++;
        bool=true;
        for(j=2;j<i;j++)
        {
           
            if(i%j==0)
            {
                bool=false;
            }
  
        }
        if(bool)
        {
            counter++;
            System.out.println(counter+". prime number is: "+i);
        }
      
    }
  • Hi! While your answer gives a correct piece of code to find the Nth prime number, it isn't in C++ (which the original question was about), and you don't find the issue in the original piece of code. Answers should help explain, not just give a large dump of code. – Chris Jefferson Oct 06 '22 at 13:58