VB icon

Determine Prime and return Factors

Email
Submitted on: 1/6/2015 8:40:00 AM
By: Thomas D. Tomlins (from psc cd)  
Level: Advanced
User Rating: By 2 Users
Compatibility: VB 5.0, VB 6.0
Views: 586
 
     Determine if a number is Prime if it is not prime return Factors of the number
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Determine Prime and return Factors
' Description:Determine if a number is Prime if it is not prime return Factors of the number
' By: Thomas D. Tomlins (from psc cd)
'
' Inputs:Long number
'
' Returns:If not prime returns factors otherwise returns that the number is Prime
'
' Side Effects:None Known
'**************************************

Option Explicit
'Add two textboxes 1- txtFactors(Returns Factors) and 1- Text1(Input number
'Two commandbuttons 1-cmdPrimeand 1-cmdPrime2
'One Label 1-Label2
Private Sub cmdPrime_Click()
Dim I As Long, J As Long, Num As Long
Num = Val(Text1.Text)
If Num <= 3 Then
Label2.Caption = "Entry is Prime"
Exit Sub
End If
If Num Mod 2 = 0 Then
Label2.Caption = "Entry is Not Prime"
Exit Sub
End If
I = Int(Sqr(Num))' Should be Sqrt(Num)
For J = 3 To I Step 2
 If Num Mod J = 0 Then
Label2.Caption = "Entry is Not Prime"
Exit Sub
 End If
Next J
 
Label2.Caption = "Entry is Prime"
End Sub
Private Sub cmdPrime2_Click()
Dim Factors As New Collection
Dim I As Long, J As Long, K As Long, L As Long, Num As Long
Num = Val(Text1.Text)
I = Int(Sqr(Num))' Should be Sqrt(Num)
For J = 2 To I
 If Num Mod J = 0 Then
L = Factors.Count \ 2
K = Num \ J
If Factors.Count > 0 Then
Factors.Add J, , , L
If (K <> J) Then Factors.Add K, , , L + 1
Else
Factors.Add J
If (K <> J) Then Factors.Add K
End If
 End If
Next J
If Factors.Count = 0 Then
 txtFactors.Text = Text1.Text & " is prime."
Else
 txtFactors.Text = Text1.Text & " is not prime." & vbCrLf
 txtFactors.Text = txtFactors.Text & "It is divisible by "
For I = 1 To Factors.Count
txtFactors.Text = txtFactors.Text & Factors.Item(I) & " ,"
Next I
End If
End Sub


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.