prime, for, while..help

so, i have got a problem that says a number x is read, with that number, find the x'th prime number, e.g x=4, the x'th prime number will be 7 (2 is first, 3, 5, 7 is 4th prime number) so 7 will be shown in the console(restriction0<x<1001)
my program doesn't show anything, but am i at least on the right path?
like is the idea at least a good one?i'd love some help/explanation.
knowledge of : if else while for..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace std;

int main()
{
 int x, i, ci, k, prim, esteprim;
 cin>>x;
 int rest=1001;
 esteprim=1; ci=2; prim=0;
 k=0;
 while (k<=x)
  {
  for ( i = 5; i <= rest; i+=2)
   {
         while (ci <=i/2)
         {
             if (i % ci == 0)
             {
                 esteprim = 0;
             } else {esteprim = 1;
             }
         }  ci++;
          if (i == 1)
          {
             esteprim = 0;
          }
          if (i == 2 || i == 3 || i == 5)
           {
               esteprim=1;
           }
           if (esteprim ==1)
            {
                prim = prim * 0 + i;
 
            }
            }
   } cout << prim;
    return 0;
}
Last edited on
Let's see, I'll tell you what I observed.

Your while(k <= x) loop is an infinite loop. Side note, you want k to be initialized with 1 if you want the loop to iterate 'x' times (otherwise make the condition k<x).
You forgot to increment your k, so do that.

Since there's no break statement, for (i = 5; i <= rest; i += 2) will always iterate 997 times. Probably not what you want..

Can you explain the basic logic you're using to find the prime number (need to know this if anybody has to correct you)? Maybe we can tweak it a bit.. I have to appreciate that you didn't just google the logic and are trying to work it out yourself! ;)
closed account (E0p9LyTq)
my program doesn't show anything, but am i at least on the right path?

Hard to say, your program flow and lack of whitespace formatting makes understanding what is going on hard. I'd say no.

Using code tags would make reading your source much easier.
http://www.cplusplus.com/articles/jEywvCM9/

(You can edit your post and add them.)

Finding primes might be best put in a separate function.

Choosing meaningful variable names helps readability.

Something like this, maybe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>

bool is_prime(int n);

int main()
{
   // low/high range of numbers to check for prime
   const int min_num = 1;
   const int max_num = 1001;

   std::cout << "Position of prime? ";
   int pos = 0;
   std::cin >> pos;

   int count = 0;

   // lets loop from min to max to brute force find primes
   for (int loop = min_num;  loop <= max_num; loop++)
   {
      // find one? increment the count
      if (is_prime(loop) == true)
      {
         count++;

         // is the current count the desired position?
         if (count == pos)
         {
            // output you've found the desired position
            std::cout << loop << " is the " << count << "th prime number.\n";

            // no need to check, terminate the loop
            break;
         }
      }
   }
}

bool is_prime(int n)
{
   int i;
   int count = 0;

   // one is not prime
   if (n == 1)
   {
      return false;
   }

   // two is prime
   if (n == 2)
   {
      return true;
   }

   // any number divisable by two is not prime
   if (n % 2 == 0)
   {
      return false;
   }

   // all other numbers check for number of divisors
   for (i = 1; i <= n; i++)
   {
      if (n % i == 0)
      {
         count++;
      }
   }

   // if only 2 divisors number is prime
   if (count == 2)
   {
      return true;
   }
   else
   {
      return false;
   }
}

This program can produce no output, if you choose the position of a prime that is greater than the maximum allowed number.
closed account (E0p9LyTq)
Another method is creating a look-up table, generating a list of primes using the Sieve of Eratosthenes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>

std::vector<int>& get_primes(int, int);

int main()
{
   // low/high range of numbers to check for prime
   const int min_num = 1;
   const int max_num = 1001;

   std::vector<int> primes;
   primes = get_primes(min_num, max_num);

   std::cout << "Position of prime? ";
   int pos = 0;
   std::cin >> pos;

   if (pos > primes.size())
   {
      std::cout << "Error!  Trying to find too large a prime!\n";
      return 0;
   }

   std::cout << primes[pos - 1] << " is the " << pos << "th prime number.\n";
}

std::vector<int>& get_primes(int min, int max)
{
   // generate a vector of numbers for the Sieve of Eratosthenes
   // making it false filled
   std::vector<int> sieve(max - min + 1, 0);

   for (int i = 2; i <= max; i++)
   {
      for (int j = i * i; j <= max; j += i)
      {
         sieve[j - 1] = 1;
      }
   }

   std::vector<int> table;

   for (int i = 1; i <= max; i++)
   {
      // exclude 1 from the prime table.
      if (i == 1)
      {
         continue;
      }

      if (sieve[i - 1] == 0)
      {
         table.push_back(i);
      }
   }

   return table;
}
Last edited on
I can understand your thoughts, in fact this is not a good idea.
As Grime said, you have made a infinite loop.
But I still use your thoughts to rewrite this program. Although it's not perfect, this is one of the possible ways.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

int main()
{
    int x, ci=2, k=1, prim=2, esteprim=1, rest=1001;
    cin>>x;
    if(k<x)
    {
        for(int i=3; i<rest; i+=2)
        {
            esteprim=1;
            ci=2;
            while(ci<=i/2)
            {
                if(i%ci==0)
                {
                    esteprim=0;
                    break;
                }
                ++ci;
            }
            if(esteprim)
            {
                prim=i;
                ++k;
            }
            if(k>=x)break;
        }
    }
    cout << prim;
    return 0;
}
Last edited on
Grime, the way I use to check if the number is prime or not, is by opening a while with the condition that the counter is less or equal the number divided by 2, then i open an if statement, saying that if the number % counter equals 0, it's false, increment the counter until true.
and about the break statement, I actually haven't learnt about it, or I passed it but I'm more than sure I haven't, I'm learning off an online programming module, which contains the basics of c++ and further, I haven't gone that far yet, so those are the only statements I can and am using to solve problems and so on.
FurryGuy, I ussually write the program way trimmer and I did, but something happened when I pasted it from CB to this site, and it lost the "whitespace" that wasn't needed.
after posting I decided to take a break, been meditating a lot of time into this problem, so I close the CB without saving it, after looking how the program pasted, but couldn't solve it unless rewriting, I kept it how it was, altough it's hard to read it, my apologies.
In my language, they variables make sense, sorry for that and thanks for telling me to use code tags, didn't know about them since I'm new here.
also, not sure what bool statement is, so I won't be using it yet..neither vectors.
Trial Faith why is it not a good idea doing what I did, can you explain please?
also, why are you saying it is not perfect, if it works just well and you wrote it, guess no mistakes were made?

thank you guys for answering and helping me out, appreciate it and my apologies!
Of course my code runs without error, but it is lack of generality. Everytime it runs, it only give one prime that you want. If you want another, you have to rerun the program, and the program have to recalculate all the numbers -- even if some of the numbers have been calculated just now.
One better way is to make a function and call the function in the main() function, and avoid recalculating.
And in terms of algorithm, it is a very "naive" algorithm. You can use other more efficient algorithms, like the Sieve of Eratosthenes as FurryGuy said.
In addition, this code can not handle exceptions.
Last edited on
Topic archived. No new replies allowed.