Results 1 to 2 of 2

Thread: Finding nth prime

Threaded View

  1. #1

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Finding nth prime

    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.");}
    Last edited by Flashbond; Mar 20th, 2013 at 05:49 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width