|
-
Jul 26th, 2005, 04:38 PM
#1
Thread Starter
Fanatic Member
prime calculator
i have made a program t see if a number entered into text1.text is prime and it is not working it is just giving the wrng results some prime numbers it says they are nt prime sme nt prime numbers it says they are prime but only sometimes it is right i dont know what i have doen wrong, can someone help me? this is the cde:
VB Code:
Option Explicit
Dim calculating As Byte
Dim prime As Byte
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
If Text1.Text < 10 Then
MsgBox "nuber must be bigger than 10 "
Else
calculating = True
Dim lngStart As Long
Dim lngFinish As Long
Dim lngCounterOne As Long
Dim lngCounterTwo As Long
Command1.Caption = "calculating..."
Form1.Tag = 2
Text1.Tag = Str(Val(Text1.Text) - (1))
lngStart = GetTickCount()
Do
If Text1.Text Mod Form1.Tag = 1 Then
calculating = False
prime = False
Else
Form1.Tag = Str(Val(Form1.Tag) + (1))
End If
If Form1.Tag >= Text1.Tag Then
calculating = False
prime = True
End If
Loop Until calculating = False
lngFinish = GetTickCount()
Label1.Tag = CStr(lngFinish - lngStart)
Text2.Text = Str(Val(Form1.Tag) / (1000))
If prime = True Then
Label1.Caption = "number is prime"
Else
Label1.Caption = "number is not prime"
End If
Command1.Caption = "calculated"
End If
End Sub
-
Jul 26th, 2005, 04:51 PM
#2
Re: prime calculator
There was a contest recently to test for the first 1000 primes. There are lot of examples posted. They are in the contest forum.
-
Jul 26th, 2005, 06:06 PM
#3
Re: prime calculator
I don't think it is a good idea to use tags to store your info, why not use variables?
Anyway here is what I think you are trying to do
VB Code:
Private Function isPrime(lInput As Long) As Boolean
Dim ldivisor As Long
For ldivisor = 2 To lInput - 1
If lInput Mod ldivisor = 0 Then
isPrime = False
Exit Function
End If
Next
isPrime = True
End Function
Private Sub Command2_Click()
If isPrime(CLng(Text1)) Then
Label1 = CStr(CLng(Text1)) & " is prime."
Else
Label1 = CStr(CLng(Text1)) & " is not prime."
End If
End Sub
see how indenting your code makes it easier to read?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|