Prime numbers below 25

Mar 30, 2014 at 4:50pm
Hi all!

New to programing and to this forum. Hoping to get some feedback on my code. I want to find all prime numbers below 25. The code I have compiled currently lists the prime numbers it found repeatedly. Can't understand why...would really appreciate some guidance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using namespace std;

int main()
{
    int i;
    for(i=2; i<25; i++)
    {
        for(int divisor=2; divisor<i; divisor++)
        {
            if(i%divisor==0)
                break;
            else
            {cout<<i<<endl;}
        }
    }
    
    cin.ignore();
    cin.get();
    return 0;
    
}
Mar 30, 2014 at 4:56pm
Because you're having a number output every time it proves that it isn't evenly divisible by the divisor, not just when it is prime you are also incorrectly testing for primeality. For example, '9' isn't prime but your program will output it when you test '9' against '2', '4', '5','6','7' and '8'. You should run through all of the divisors before you output the number.
Mar 30, 2014 at 5:17pm
Thanks, I changed it a bit to better test for prime numbers using sqrt(i). New code below...however still don't understand how to stop it from adding the same number to the list.

1
2
3
4
5
6
7
8
9
10
11
12
int i;
    for(i=2; i<25; i++)
    {
        for(int divisor=2; divisor<=i; divisor++)
        {
            if(i%divisor==0)
                break;
            else if
            (divisor+1>sqrt(i))
            {cout<<i<<endl;}
        }
    }
Mar 30, 2014 at 5:24pm
Try using a vector and every time you get a new prime number, add it to the vector. Then whenever you output a number, check that the number isn't already in the vector before you output it, if it is, don't output it.
Mar 30, 2014 at 5:37pm
Add a break; after your cout << i < endl;.
Mar 30, 2014 at 5:53pm
With the break; it now outputs just "3 5 7" :/
Mar 30, 2014 at 6:05pm
I meant to put the break; after the cout << i << endl; but before the }.
Mar 30, 2014 at 6:12pm
ah THANK YOU!
Mar 30, 2014 at 9:05pm
Following up on this...is it possible to add each output into an array?

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
int main()
{
    int i;
    int array[6];    
    for(i=2; i<=25; i++)
    {
        for(int divisor=2; divisor<=i; divisor++)
        {
            if(i%divisor==0)
                break;
            else if
            (divisor+1>sqrt(i))
            {
                array[i]=i;
                cout<<array[6];}
               break;}        
       }
     
    cin.ignore();
    cin.get();
    return 0;
    
}
    
    
Mar 30, 2014 at 9:12pm
I would prefer std::vector instead if at all possible:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::vector<int> primes;
for (int i = 2; i <= 25; ++i)
{
    for (int divisor = 2; divisor <= i; ++divisor)
    {
        if (i % divisor == 0)
            break;
        else if (divisor + 1 > sqrt(i))
        {
            primes.push_back(i);
            std::cout << i << '\n';
            break;
        }
    }
}

But if you must use an array, you'll have to keep track of the last index yourself:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int primes[50]; // Plenty of space, just in case :)
int numPrimes = 0;
for (int i = 2; i <= 25; ++i)
{
    for (int divisor = 2; divisor <= i; ++divisor)
    {
        if (i % divisor == 0)
            break;
        else if (divisor + 1 > sqrt(i))
        {
            primes[numPrimes] = i;
            ++numPrimes; // Or you could just say 'primes[numPrimes++] = i;'
            std::cout << i << '\n';
            break;
        }
    }
}
Mar 30, 2014 at 9:44pm
Using an array...I now want to access the 4th element within it. This doesn't quite work :/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int primes[50]; 
    int numPrimes = 0;
    for (int i = 2; i <= 25; ++i)
    {
    for (int divisor = 2; divisor <= i; ++divisor)
    {
        if (i % divisor == 0)
            break;
        else if (divisor + 1 > sqrt(i))
        {
            primes[numPrimes] = i;
            primes[numPrimes++]=i; 
            cout << primes[4]<<" ";
            break;
        }
    }
Mar 30, 2014 at 9:46pm
Why are you setting 2 elements in the array equal to i each time?

Edit: Nevermind you increment after setting the second time so just take the first one out.
Last edited on Mar 30, 2014 at 9:46pm
Mar 30, 2014 at 11:27pm
Think I got it...thanks for all the help!
Topic archived. No new replies allowed.