Results 1 to 8 of 8

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.

    Code:
    'This code was written by Mert Ener
    Private Declare Function GetTime Lib "winmm.dll" Alias "timeGetTime" () As Long
    Private mStartTime As Object
    
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            mStartTime = GetTime
            Dim l, n As Long : Dim m As Long = 1 : Dim p As New List(Of Long)(New Long() {2})
            For n = 0 To (Me.TextBox1.Text) - 2 'The textbox you entered for the nth prime.
                m = m + 2
                For l = 1 To n
                    If m Mod p(l) = 0 Then
                        m += 2
                        l = 0
                    End If
                Next
                p.Add(m)
            Next
            Me.TextBox2.Text = p(n) 'The textbox you get the result.
            MsgBox("It took me " & GetTime - mStartTime & " milliseconds to find your prime number.")
        End Sub
    Last edited by Flashbond; Aug 11th, 2013 at 06:17 PM.

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