Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Find Prime Number, find prime number code
dedyradix
post Mar 28 2007, 01:27 PM
Post #1


Newbie [Level 1]
*

Group: Members
Posts: 19
Joined: 7-May 06
Member No.: 23,209



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
Go to the top of the page
 
+Quote Post
ghostrider
post Mar 28 2007, 08:04 PM
Post #2


Super Member
*********

Group: Members
Posts: 397
Joined: 9-June 06
From: Wisconsin
Member No.: 24,924



Interesting function. What exactly is the purpose of the argument InNoWanted?
Go to the top of the page
 
+Quote Post
Galahad
post Apr 4 2007, 11:31 AM
Post #3


Neurotical Squirrel
*********

Group: [HOSTED]
Posts: 579
Joined: 4-November 04
From: Novi Sad, Vojvodina
Member No.: 2,127



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
Go to the top of the page
 
+Quote Post
hitmanblood
post May 9 2007, 09:04 PM
Post #4


Privileged Member
*********

Group: [HOSTED]
Posts: 775
Joined: 13-April 07
From: mreža
Member No.: 41,558



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.
Go to the top of the page
 
+Quote Post
ghostrider
post May 9 2007, 11:08 PM
Post #5


Super Member
*********

Group: Members
Posts: 397
Joined: 9-June 06
From: Wisconsin
Member No.: 24,924



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

Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Redirect Code Help(8)
  2. Google A Number?(44)
  3. Simple C File Handling In Action(3)
  4. Add Flashing Inbox To Invisionfree Forum(0)
  5. Why Doesn't This Code Work On Computinghost?(2)
  6. How Do I Code A Design?(6)
  7. Each And Every Nokia Code For Your Mobile(6)
  8. How do you test your php code(75)
  9. Html Code Tester. Online Script(15)
  10. Need Help With C Program To Test If A Number Is Prime(12)
  11. Ti Basic: Pick A Number(1)
  12. Myspace Music Player(9)
  13. Mozilla: Firefox Plugin Shipped With Malicious Code(3)
  14. Wap Source Code Viewer(4)
  15. Diablo 2 D2pk Custom Private Pvp Private Realm(1)
  1. Php Code?(12)
  2. Create Table - Mysql Code - Help(1)
  3. Php Source Code Unveiled In Browser?(7)
  4. Get Pan Number Online(0)
  5. Error Code In Subscriptions Page(5)
  6. Java Script Drop Down Menu With Css(2)
  7. Runescape 2 Private Server: Code/guide 1(10)
  8. Da Vinci Code(15)
  9. Malicious Code Injection(3)
  10. How To Make A Search Form And Php Code?(10)
  11. How To Put A Phpbb Login Box On Your Main Site.(18)
  12. Php And Mysql Programming(2)
  13. Most Efficient Code To Get Prime Numbers(7)
  14. Need Help With Some Code....(6)
  15. any free serial code for macromedia flash 2004?(7)


 



- Lo-Fi Version Time is now: 20th July 2008 - 11:45 AM