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

This is a discussion on Code give primes till 200. Maybe, readable. Let community decide it. within the C++ Programming forums, part of the General Programming Boards category; I show another piece of mini-workflow. Can you read it easy. Code: #include<iostream> #include<vector> using namespace std; int main(){ int ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26

    Question 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();
               
                }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    No..
    I never put signature, but I decided to make an exception.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    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();
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26
    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.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26
    .push_back sorry.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26

    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();    
        }

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    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?
    Last edited by kmdv; 03-06-2011 at 11:32 AM.
    I never put signature, but I decided to make an exception.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26
    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.

  11. #11
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26
    No, because that do much.

  12. #12
    Registered User
    Join Date
    Feb 2010
    Location
    Budapest
    Posts
    26
    But thanks

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Treborh View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Illegal Instruction at perfectly fine Code
    By m00ni in forum C Programming
    Replies: 24
    Last Post: 02-14-2011, 01:56 AM
  2. cygwin on win64
    By Vanzemljak in forum Tech Board
    Replies: 3
    Last Post: 01-12-2011, 03:28 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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