944,198 Members | Top Members by Rank

Ad:
Oct 12th, 2006
0

Prime Numbers - Help its for college

Expand Post »
Ok i need the user to be asked for a number by an inputbox (Using VB 6) and have this number checked. If its prime i want them to be told via msgnox , if not i want them to be given another attempt

Any ideas?
Similar Threads
Moderator
Featured Poster
Reputation Points: 1800
Solved Threads: 575
Moderator
jbennet is offline Offline
16,534 posts
since Apr 2005
Oct 12th, 2006
0

Re: Prime Numbers - Help its for college

You'll have to pick a maximum input number. Then work out all the prime numbers up to that number and put them in an array to check the user's input against. There isn't a shorthand method of finding out if a number is prime (though using base 6 can narrow the options down).
Reputation Points: 22
Solved Threads: 11
Posting Whiz in Training
DavidRyan is offline Offline
229 posts
since Jul 2006
Oct 13th, 2006
0

Re: Prime Numbers - Help its for college

you think this woul work?

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1.  
  2. option explicit
  3.  
  4. dim prime as boolean
  5. dim number as integer
  6. dim x as integer
  7.  
  8. num = inputbox("Enter a whole number up to 100")
  9.  
  10. for x = 1 to 100
  11.  
  12. (code here)
  13.  
  14. next x




I just need the ocde here bit i think, but how do i get it to know whether a number is whole or not?
Last edited by jbennet; Oct 13th, 2006 at 4:05 am.
Moderator
Featured Poster
Reputation Points: 1800
Solved Threads: 575
Moderator
jbennet is offline Offline
16,534 posts
since Apr 2005
Oct 13th, 2006
0

Re: Prime Numbers - Help its for college

U can try this

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1.  
  2. Do while true
  3.  
  4. Dim x as Long
  5. Dim myNum as long
  6. myNum = val(InputBox ("Enter A Number"))
  7.  
  8. Dim mFlag as Byte
  9. mFlag = 0
  10.  
  11. For x = 1 to Clng(myNum/2)
  12. If myNum Mod x = 0 then
  13. mflag = 1
  14. exit for
  15. end if
  16. next
  17. if mflag = 0 then
  18. Msgbox "Prime Number"
  19. Exit sub
  20. end if
  21. loop

This works in an infinite loop, asking the user for a number, until the user has entered a prime number. To ensure it is a whole number, I typecasted it to a long. I avoided integer in case the user enters a number greater than 32676 (max value for an integer, IIRC). In this was you can also avoid having a maximum limit for the user input
Reputation Points: 20
Solved Threads: 10
Junior Poster
aparnesh is offline Offline
193 posts
since Jul 2005
Oct 14th, 2006
0

Re: Prime Numbers - Help its for college

Sorry for the howler in the code. The For Loop should be
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. For x = 2 to Clng(myNum/2)
  2. ....
Reputation Points: 20
Solved Threads: 10
Junior Poster
aparnesh is offline Offline
193 posts
since Jul 2005
Oct 14th, 2006
0

Re: Prime Number Test

The above code is a great example, but there is a way to make it faster. Consider the following:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Input number: 100
  2. 100 / 2: 50
  3. Sqr(100): 10
Once you hit the square root of a number, you can stop, as it is likely to be a great deal smaller than the same number halved. Here's how I would do this (this is in fact an update of the program I used to learn QBASIC, which some of you may remember from the days of MS-DOS):

First, the IsPrime Function:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Function IsPrime(checkValue As Long) As Boolean
  2. For testValue = 2 To CLng(Sqr(checkValue)) Step 1
  3. If checkValue Mod testValue = 0 Then
  4. IsPrime = False
  5. Exit Function
  6. End If
  7. Next
  8. IsPrime = True
  9. End Function
And now for the code which will use the IsPrime function to solve your problem:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Sub Main()
  2. Do
  3. userValue = CLng(InputBox("Enter a value to test. I will tell you if it is prime or not.", "Prime Tester", userValue))
  4. If IsPrime(userValue) Then
  5. If MsgBox(userValue & " is a prime number! Congratulations! Would you like to try another?", vbYesNo, "Number is Prime!") = vbNo Then
  6. End
  7. End If
  8. End If
  9. Loop
  10. End Sub
Just for kicks and giggles (and since the program I'm redoing to bring you this solution did it this way too), I'll provide the code to list every number LESS THAN the user's input that is a prime number, taking advantage of the IsPrime Function again:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Sub Main()
  2. Do
  3. userValue = CLng(InputBox("Enter a value to test. I will tell you every prime up to (and possibly including) it.", "Prime Tester", userValue))
  4. primesList = "1"
  5. For countValue = 2 To CLng(userValue) Step 1
  6. If IsPrime(CLng(countValue)) Then
  7. primesList = primesList & ", " & CStr(countValue)
  8. End If
  9. Next
  10. If MsgBox("The prime numbers up to (and possibly including) " & userValue & " are:" & vbCrLf & primesList & vbCrLf & vbCrLf & "Would you like to try another?", vbYesNo, "Your Prime Numbers") = vbNo Then
  11. End
  12. End If
  13. Loop
  14. End Sub
Other variations include the smallest prime >= <input number>, the largest prime <= <input number>, the first <input number> prime numbers, or any number of other things you might use the IsPrime function to do. This is a (relatively) quick function, and takes a mere fraction of the time you would use checking every number up to <input number> / 2 - especially for larger numbers.

As always, enjoy!

- Sendoshin
Reputation Points: 16
Solved Threads: 1
Light Poster
sendoshin is offline Offline
39 posts
since Sep 2006
Oct 14th, 2006
0

Re: Prime Numbers - Help its for college

yes, checking till Square root is quicker
Last edited by aparnesh; Oct 14th, 2006 at 4:04 pm.
Reputation Points: 20
Solved Threads: 10
Junior Poster
aparnesh is offline Offline
193 posts
since Jul 2005
Oct 14th, 2006
0

Re: Prime Numbers - Help its for college

thats great it runs fine here in vb6. couldnt get it to run in net but fixed that, its just the sqr function is in system.math instead

Thanks for helping me i really needed to do it for school
Moderator
Featured Poster
Reputation Points: 1800
Solved Threads: 575
Moderator
jbennet is offline Offline
16,534 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Problem while inserting into oracle date values frm vb
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: need suggestion on this project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC