Results 1 to 3 of 3

Thread: prime calculator

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    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:
    1. Option Explicit
    2. Dim calculating As Byte
    3. Dim prime As Byte
    4. Private Declare Function GetTickCount Lib "kernel32" () As Long
    5.  
    6.  
    7.  
    8.  
    9. Private Sub Command1_Click()
    10. If Text1.Text < 10 Then
    11. MsgBox "nuber must be bigger than 10 "
    12. Else
    13.  
    14.  
    15. calculating = True
    16. Dim lngStart As Long
    17. Dim lngFinish As Long
    18. Dim lngCounterOne As Long
    19. Dim lngCounterTwo As Long
    20. Command1.Caption = "calculating..."
    21. Form1.Tag = 2
    22. Text1.Tag = Str(Val(Text1.Text) - (1))
    23. lngStart = GetTickCount()
    24. Do
    25. If Text1.Text Mod Form1.Tag = 1 Then
    26. calculating = False
    27. prime = False
    28.  
    29. Else
    30. Form1.Tag = Str(Val(Form1.Tag) + (1))
    31. End If
    32. If Form1.Tag >= Text1.Tag Then
    33. calculating = False
    34. prime = True
    35. End If
    36. Loop Until calculating = False
    37. lngFinish = GetTickCount()
    38.  
    39. Label1.Tag = CStr(lngFinish - lngStart)
    40. Text2.Text = Str(Val(Form1.Tag) / (1000))
    41. If prime = True Then
    42. Label1.Caption = "number is prime"
    43. Else
    44. Label1.Caption = "number is not prime"
    45. End If
    46. Command1.Caption = "calculated"
    47. End If
    48.  
    49.  
    50.  
    51.  
    52.  
    53. End Sub

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  3. #3
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    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:
    1. Private Function isPrime(lInput As Long) As Boolean
    2. Dim ldivisor As Long
    3.  
    4. For ldivisor = 2 To lInput - 1
    5.     If lInput Mod ldivisor = 0 Then
    6.         isPrime = False
    7.         Exit Function
    8.     End If
    9. Next
    10. isPrime = True
    11.  
    12. End Function
    13.  
    14. Private Sub Command2_Click()
    15. If isPrime(CLng(Text1)) Then
    16.     Label1 = CStr(CLng(Text1)) & " is prime."
    17. Else
    18.     Label1 = CStr(CLng(Text1)) & " is not prime."
    19. End If
    20. 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
  •  



Click Here to Expand Forum to Full Width