Finding the nth prime number using Python

前端 未结 4 1778
礼貌的吻别
礼貌的吻别 2020-12-16 08:21

When I run this code, even for just counting to the 10th prime number (instead of 1000) I get a skewed/jacked output--all \"not prime\" titles for my is_composite variable,

4条回答
  •  清酒与你
    2020-12-16 09:20

    This is a code I wrote for C++. But the mentality must be the same.

    // This code was written by Mert Ener
    #include 
    #include 
    #include 
    
    private: System::Void button1_Click_1(System::Object^  sender, 
                                              System::EventArgs^  e) { 
        using namespace std;
        UInt64 cloc = clock();
        long m = 1;
        long k = long::Parse(textBox1->Text)-2;   // The text you entered 
        std::vector p(1,2);                 //   for the nth prime
        for( long n = 0; n <= k; n++ ) {
            m += 2;
            for( long l = 1; l <= n; l++ ) {
                if (m % p[l] == 0) {
                    m += 2;
                    l=0;}}
            p.push_back(m);}
        textBox2->Text = p[k+1].ToString(); // The textbox for the result.
        MessageBox::Show("It took me " + (clock() - cloc).ToString() 
                         + " milliseconds to find your prime.");}
    

提交回复
热议问题