If you appreciate these tips, please consider a small donation:

 General Tip How to Determine Whether a Specified Number is Prime Visual Basic Code Tested with VB6 Courtesy of Marshall Ellis A prime number is one that only divides evenly by 1 and itself.  For instance, 3, 5 and 11 are all primes.  The key to determining whether a number is prime is to find every factor, which is time-consuming when you get into larger numbers. The example given here is a slow, "brute force" method, and there are alternatives for larger numbers. I am aware of that and leave it as an exercise for the reader. The sample project described below centers around creating a single function that returns true or false after testing the number passed in.  Using the same function, we'll have a command button that checks a single number, and a command button that lists all primes from 1 to the specified number. 1 – Create a new standard VB project. 2 – In the General Declarations section of the form code, or in a module, place the following: Private Function AmIPrime(CheckMe As Long) As Boolean   AmIPrime = True   '1 is always prime so let's handle it here   If CheckMe = 1 Then     AmIPrime = True     Exit Function   End If   'Positive numbers only   If CheckMe <= 0 Then     AmIPrime = False     Exit Function   End If   'Here's where the real action takes place   'Note that we go from 2 to 1 less than the source   'This is because 1 and the source are known to be _    factors already.  Non-prime means there are others.   For i = 2 To CheckMe - 1     If CheckMe Mod i = 0 Then       AmIPrime = False       'Get out as soon as we know it's not prime       'This speeds things dramatically       Exit Function     End If   Next End Function 3 – On the form place a textbox, two command buttons, a label, and a listbox.  We'll assume the default names for them. 4 – The code for the first command button's click event should look something like this: Private Sub Command1_Click()   'Grab the number to test from the textbox   Dim lngSource As Long   lngSource = Text1.Text   'Get the result by sending the number _    to test to the AmIPrime function.   Dim boolResult As Boolean   boolResult = AmIPrime(lngSource)   'This just displays the result on the label. _   You could also use a msgbox, or simply use _   the result behind the scenes without _   displaying it.   If boolResult = True Then     Label1.Caption = Text1.Text & " IS Prime!"   Else     Label1.Caption = Text1.Text & " isn't Prime!"   End If End Sub 5 – The code for the second command button's click event should look something like this: Private Sub Command2_Click()   Dim lngCounter As Long   Dim lngSource As Long   Dim boolResult as Boolean   'Get the upper value of the range to test for primes. _    Lower bound is 1 here.  You could change the code to _    check a range starting with something other than 1.   lngSource = Text1.Text   'Uncommenting the two debug lines in the IDE will _    allow you optionally to check how long it takes.   'Debug.Print "Start time: " & Now   'Pass every value in the range to test for prime.   For lngCounter = 1 To lngSource     boolResult = AmIPrime(lngCounter)     If boolResult = True Then       'If it's prime we'll toss it in the list.       List1.AddItem lngCounter       'The DoEvents can be used to ensure results _        display immediately, but slows things down.       'DoEvents     End If   Next   'Debug.Print "End time: " & Now End Sub 6 – Run the program, enter a number in the textbox and test Command1.  Now test Command2 to list the primes from 1 to your number in the textbox.  Note that larger numbers will take significant lengths of time to process (1 to 1,000,000 took 7 minutes, 26 seconds on a Pentium III 450 when tested, and returned 11,022 results).  Also note that the program concerns itself only with positive integers.

[ Back To The Top ]

Contact: web@xtremecomp.com
All contents copyright XTreme Computing unless noted otherwise.