May 15, 2008

Find Prime Number - find prime number code

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > VB Programming

free web hosting

Find Prime Number - find prime number code

dedyradix
CODE
Public Function GetPrimeNo (InNoWanted As Integer) As Long

Dim lPrimes() As Long
Dim lTrial As Long
Dim lCount As Integer
Dim lFound As Integer
Dim lCounter As Integer

ReDim lPrimes(InNoWanted)

lTrial = 5
lCount = 3
lFound = 0
'Define the first three prime number
' > lPrimes(0) = 2
lPrimes(1) = 3
lPrimes(2) = 5
Do
lTrial = lTrial + 2
lFound = 0
For lCounter = 0 To lCount - 1
lFound = (lTrial Mod lPrimes(lCounter)) = 0
If lFound Then
Exit For
End If

lCounter = lCounter + 1
Next lCounter

If lFound = 0 Then
lPrimes(lCount) = lTrial
lCount = lCount + 1
End If

Loop While (lCount < InNoWanted)

GetPrimeNo = lPrimes(InNoWanted - 1)
End Function

Reply

ghostrider
Interesting function. What exactly is the purpose of the argument InNoWanted?

Reply

Galahad
That's great function... I'm really no good at math, and I can emember that on one or two occasions I needed such a function, to find a prime number... I'll keep this in my code base, for future reference... Great work smile.gif

Reply

hitmanblood
Are you sure that this works as I checked it at least I scanned it and it seems like it will return an error because of for loop. I am quite new to the visual basic but I am still learning that is in the process of learning. Maybe I am just wrong when I have more time I'll check it.

Reply

ghostrider
I am reviewing your function once and you have a couple mistakes so far. I've been using VB for about 11 years now, and I will point them out to you, and post a bug free function.

You didn't post any information about how your function works. From what I can gather, the argument InNoWanted, will return the nth prime number, where n is the value of InNoWanted. For example, your function should produce the following results.

--------------------------------------
| InNoWanted | Value Returned |
--------------------------------------
| 1 | 2 |
--------------------------------------
| 2 | 3 |
--------------------------------------
| 3 | 5 |
--------------------------------------
| 4 | 7 |
--------------------------------------


The first error I have encountered is this that a value of zero will mess stuff up. You should add a check to make sure a value of zero is not entered:

CODE
Public Function GetPrimeNo(InNoWanted As Integer) As Long

Dim lPrimes() As Long
Dim lTrial As Long
Dim lCount As Integer
Dim lFound As Integer
Dim lCounter As Integer

    If InNoWanted = 0 Then
    Exit Function
    End If


Nextly, it returns an error because you do not have enough elements in your array. You define 3 below this ReDim statement. Therefore you should add 3 to the amount of elements to create.

CODE
ReDim lPrimes(InNoWanted + 3)


You also commented out some code. You need that to have your program run correctly.
CODE
'Define the first three prime number
lPrimes(0) = 2
lPrimes(1) = 3
lPrimes(2) = 5


Other than that, you have no other programming mistakes in your code. Below is the fixed version:
CODE
Public Function GetPrimeNo(InNoWanted As Integer) As Long

Dim lPrimes() As Long
Dim lTrial As Long
Dim lCount As Integer
Dim lFound As Integer
Dim lCounter As Integer

    If InNoWanted = 0 Then
    Exit Function
    End If

ReDim lPrimes(InNoWanted + 3)

lTrial = 5
lCount = 3
lFound = 0
'Define the first three prime number
lPrimes(0) = 2
lPrimes(1) = 3
lPrimes(2) = 5
Do
lTrial = lTrial + 2
lFound = 0
For lCounter = 0 To lCount - 1
lFound = (lTrial Mod lPrimes(lCounter)) = 0
If lFound Then
Exit For
End If

lCounter = lCounter + 1
Next lCounter

If lFound = 0 Then
lPrimes(lCount) = lTrial
lCount = lCount + 1
End If

Loop While (lCount < InNoWanted)

GetPrimeNo = lPrimes(InNoWanted - 1)
End Function

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. c program which generates nth prime number - 32.50 hr back.
  2. php code for prime numbers - 661.15 hr back.
  3. applet "big number" modulo - 915.31 hr back.
Similar Topics

Keywords : prime prime code

  1. Best Vb Source Code Site - Best vb source code site (1)
  2. Quiz Code - (3)
    I saw a nice quiz code posted by medialint where the questions and answers are written in the code.
    What I need help with is adapting it so that it uses an access database or an excel csv file as the
    source of the questions and the choices. In the code itself there is a comment to the fact that
    "ideally they (meaning the Q's & A's) will be read from a database." So how is that done?
    How do you get a quiz code to use a database?Also the table is fairly long and is divided and
    arranged in alphabetical order. Could the questions be limited to the info in the group...



Looking for prime, number, prime, number, code

Searching Video's for prime, number, prime, number, code
advertisement



Find Prime Number - find prime number code



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE