What do you say about the code to find simple numbers?



  • I should have found some simple numbers. How do you like that?

    #include <iostream>
    

    using namespace std;

    int main(){

    //      Prosti numerics
    int n = 10,q = 0;
    cout &lt;&lt; "2 3 5 7 ";
    
    while (q &lt; 10)
    {
        n++;
        if (n%2 != 0) if (n%3!= 0) if (n%5!= 0) if (n%7!= 0) cout &lt;&lt; n &lt;&lt; " ";
    }
    
    return 0;
    

    }



  • Your code doesn't keep any criticism. Here's the simplest code, computing the numbers. Algorithm is terribly ineffective, but it works.

    #include <vector>
    #include <iostream>
    

    int main()
    {
    const unsigned max = 100;
    std::vector<unsigned> simple;

    for (unsigned n = 2; n &lt;= max; ++n) {
        bool isSimple = true;
        for (size_t i = 0; i &lt; simple.size(); ++i) {
            if (n % simple[i] == 0) {
                isSimple = false;
                break;
            }
        }
        if (isSimple) {
            simple.push_back(n);
            std::cout &lt;&lt; n &lt;&lt; std::endl;
        }
    }
    

    }

    P.S. Inefficiency, by the way, relative. This algorithm is almost as economical as possible, and the speed of work leaves much to be desired. However, the mere numbers to a million are less than a minute.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2