Ignore please. Just figured it out (after a couple of weeks trying)

Help! The purpose of this exercise is to list all primes up to 100.
It seems no matter what I do, I either miss at least one prime or I flag some composite numbers as prime. I have been working on this for a couple of weeks now. It is not for a grade so I'm not asking for help with homework. I am using "Programming - Principles And Practice Using C++" by Stroustrup. No answers or hints in book. I don't want to just give up and move on. If anyone can help, thanks a million! (As the code stands now, it lists 45 as prime.)

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
 #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
    vector <int> primes(0);

    primes.push_back(2);
    primes.push_back(3);
    primes.push_back(5);
    primes.push_back(7);
    primes.push_back(11);
    primes.push_back(13);
    primes.push_back(17);
    primes.push_back(19);
    primes.push_back(23);
    primes.push_back(29);
    primes.push_back(37);
    primes.push_back(41);
    primes.push_back(43);
    primes.push_back(47);
    /*
    primes.push_back(53);
    primes.push_back(59);
    primes.push_back(61);
    primes.push_back(67);
    primes.push_back(71);
    primes.push_back(73);
    primes.push_back(79);
*/


//2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
bool is_prime=0;

    for(unsigned int i=41; i<=47; i++)
    {
       // cout << "i=" << i << endl;



        is_prime=false;

        for(unsigned int a=0; a<primes.size() && a < i; ++a)
        {
            //cout << "primes.size() =" << primes.size() << endl;
            is_prime=false;
            cout << "i=" << i<< endl;
            cout << "primes.at(" << a << ")" << primes.at(a) << endl;

            cout << "i % primes.at(" << a << ")" << (i % primes.at(a)) << endl;




            if (i% primes.at(a) == 0 && primes.at(a) < i && i !=1)
                { is_prime = false; break;}
            else if (i % primes.at(a) == 0 && primes.at(a) == i)
            {
                is_prime = false;
                break;
            }
            else if (i % primes.at(a) == 0 && i > primes.at(a))
            {
                is_prime = true;
                break;
            }
            else if (i % primes.at(a) == 0 & primes.at(a) < i)
            {
                is_prime = true;
                break;
            }
            else {is_prime = true; break;}





        } if (is_prime==true)
                {cout << i << " is PRIME" << endl;
                  is_prime=false;}




    }





    return 0;
}

Last edited on
That isn't how you factor prime numbers. Your problem is that you are starting 'i' off at 41.
Thank you. I know. I have been trying to get the inner loop to work with the first few primes and then change the outer loop to get the last few numbers. I've got it working now, but thanks a million!
That code looks a lot messier than it had to be, given you know about vectors, here's a quick and dirty way of doing it.

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
#include <iostream>
#include <vector>

using namespace std;
vector<int> primes;

bool numberisprime(int number, vector<int>primes)
{
    for (int i=0; i<primes.size(); i++)
    {
        if (number%primes[i]==0)
        {
            return false;
        }
    }
    return true;
}
int main()
{
    for (int i=2; i<=100; i++) // start at i=2 to stop 1 appearing in list
    {
        if (numberisprime(i,primes))
        {
            primes.push_back(i);
            cout << i << endl;
        }
    }
    string any; // this line and the next is just to stop
    cin >> any; // program closing
    return 0;
}
Last edited on
Thank you.
Topic archived. No new replies allowed.