Actually this was something for C++ codebank. Not the fastest but an elegant code written by me.
Code:// This code was written by Mert Ener #include <time.h> #include <iostream> private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { UInt64 cloc = clock(); long o,m = 1; long k = long::Parse(textBox1->Text)-2; // The textbox you entered for the nth prime. vector<int> p(1,2); for( long n = 0; n <= k; n++ ) { m += 2; for( long l = 1;o=p[l],o*o<=m; l++ ) { if (m % p[l] == 0) { m += 2; l=0;}} p[n+1]=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.");}




Reply With Quote