# Code give primes till 200. Maybe, readable. Let community decide it.

• 03-06-2011
Treborh
Code give primes till 200. Maybe, readable. Let community decide it.
I show another piece of mini-workflow. Can you read it easy.

Code:

```#include<iostream> #include<vector> using namespace std; int main(){             int n=3, m; vector<int>primes(0); bool op=1;             primes.push_back(2); primes.push_back(3);                 while(n<200){ n++;             for(m=n-1; m>1; m--)if(n%m==0){op=0; break;}                   switch(op){                   case 0: op=1;break;                   case 1: primes.push_back(n);break;                   }                 }     m=0;         do{ cout<<primes[m]<<endl; m++;}       while(m<primes.size());         cin.get();                       }```
• 03-06-2011
kmdv
No..
• 03-06-2011
laserlight
No, I would expect something like this:
Code:

```#include<iostream> #include<vector> using namespace std; int main() {     int n = 3, m;     vector<int> primes(0);     bool op = 1;     primes.push_back(2);     primes.push_back(3);     while (n < 200) {         n++;         for (m = n - 1; m > 1; m--)             if (n % m == 0) {                 op = 0;                 break;             }         switch(op) {             case 0: op = 1; break;             case 1: primes.push_back(n); break;         }     }     m = 0;     do {         cout << primes[m] << endl;         m++;     } while (m < primes.size());     cin.get(); }```
Actually, I might have written it as:
Code:

```#include <iostream> #include <vector> using namespace std; int main() {     vector<int> primes;     primes.push_back(2);     primes.push_back(3);     for (int n = 4; n <= 200; ++n) {         bool is_prime = true;         for (int m = n - 1; m > 1; --m) {             if (n % m == 0) {                 is_prime = false;                 break;             }         }         if (is_prime) {             primes.push_back(n);         }     }     for (vector<int>::size_type m = 0, size = primes.size(); m < size; ++m) {         cout << primes[m] << endl;     }     cin.get(); }```
• 03-06-2011
Treborh
That' s quite good. Are you a proffessional tester? Just I don't know why I solve it in this way in the beginning push.back(2), push.back(3) why? Though the creater is me. I've written it about 2 months ago. :)
• 03-06-2011
Treborh
.push_back sorry.
• 03-06-2011
iMalc
Much better.
When building a vector of primes, I tend to also use that vector of primes to test the remainder of, to cut down the number of divisions.
• 03-06-2011
Treborh
Hey, Testers! It's another version of prime makers. More user-friend and...
Provide options. Just run it, and say your opinions.;)

Code:

```#include<iostream> #include<vector> using namespace std; int main(){             int n, m, a, b;             vector<int>primes(0); bool op=1;                         cout<<"Give two positive integer for specifying the interval in which you wonder the prime numbers: ";             cin>>a>>b; cin.ignore();  cout<<endl;                         if(a-b==0){cout<<"It is not an interval. "; cin.get(); return 0;}             if(a<0 || b<0){cout<<"Please give POSITIVE numbers. "; cin.get(); return 0;}                         (a<b) ? n=a : n=b;             (n==b) ? b=a : b;             (n>=1) ? n-- : n;                             while(n<b){ n++;                       for(m=n-1; m>=1; m--)if(n%m==0 && m!=1){op=0; break;} ;          switch(op){                   case 0: op=1;break;                   case 1: primes.push_back(n);break;                   }               }   m=0;   do{ cout<<primes[m]<<endl; m++;}while(m<primes.size());           if(m==1)cout<<"\n\nThere is "<<primes.size()<<" prime number between the two whole numbers." ;            if(m>=2)cout<<"\n\nThere are "<<primes.size()<<" prime numbers between the two whole numbers.";             cin.get();        }```
• 03-06-2011
kmdv
Code:

```#include <iostream> #include <vector> int main() {     /*     * No while loops needed.     */     int n = 3;     bool op = true;     vector<int> primes(0);     primes.push_back(2);     primes.push_back(3);     for (int n = 4; n < 200; n++)     {         for (int m = n - 1; m > 1; m--)         {             if (n % m == 0)             {                 op = false;                 break;             }         }         if (op)             primes.push_back(n);         else             op = true;     }     for (int i = 0; i < primes.size(); i++)     {         std::cout << primes[m] << std::endl;     }     std::cin.get();     return 0; }```
Isn't it beatiful?
• 03-06-2011
laserlight
Treborh, I have merged your new thread into this one. It is basically the same issue.

Your indentation still needs work. You need to be consistent. Either use spaces to indent, or use tabs to indent, but not both.
• 03-06-2011
Treborh
Uff! Sometimes there is only one reason that I use a structure, I want to practise it. Anyway, I would forget it. But yes, it is an interesting other key for the same lock. :)
• 03-06-2011
Treborh
No, because that do much.
• 03-06-2011
Treborh
But thanks
• 03-07-2011
iMalc
Quote:

Originally Posted by Treborh
Provide options. Just run it, and say your opinions.;)

Code:

```#include<iostream> #include<vector> using namespace std; int main(){             int n, m, a, b;             vector<int>primes(0); bool op=1;                         cout<<"Give two positive integer for specifying the interval in which you wonder the prime numbers: ";             cin>>a>>b; cin.ignore();  cout<<endl;                         if(a-b==0){cout<<"It is not an interval. "; cin.get(); return 0;}             if(a<0 || b<0){cout<<"Please give POSITIVE numbers. "; cin.get(); return 0;}                         (a<b) ? n=a : n=b;             (n==b) ? b=a : b;             (n>=1) ? n-- : n;                             while(n<b){ n++;                       for(m=n-1; m>=1; m--)if(n%m==0 && m!=1){op=0; break;} ;          switch(op){                   case 0: op=1;break;                   case 1: primes.push_back(n);break;                   }               }   m=0;   do{ cout<<primes[m]<<endl; m++;}while(m<primes.size());           if(m==1)cout<<"\n\nThere is "<<primes.size()<<" prime number between the two whole numbers." ;            if(m>=2)cout<<"\n\nThere are "<<primes.size()<<" prime numbers between the two whole numbers.";             cin.get();        }```

I don't need to run it, I can run that code in my head.

Frankly that code makes me barf! Such a blatant misuse of the ternary operator, amoung other sins.

You don't seem to be paying attention to what other are saying, and unless you want others to do that same you better start paying attention.