Prime Numbers - Help its for college

Reply

Join Date: Apr 2005
Posts: 16,147
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 530
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Prime Numbers - Help its for college

 
0
  #1
Oct 12th, 2006
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?
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 229
Reputation: DavidRyan is an unknown quantity at this point 
Solved Threads: 11
DavidRyan's Avatar
DavidRyan DavidRyan is offline Offline
Posting Whiz in Training

Re: Prime Numbers - Help its for college

 
0
  #2
Oct 12th, 2006
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).
Please anyone, correct me if I am wrong. It is the best way for me to learn.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,147
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 530
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Prime Numbers - Help its for college

 
0
  #3
Oct 13th, 2006
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.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 186
Reputation: aparnesh is an unknown quantity at this point 
Solved Threads: 10
aparnesh's Avatar
aparnesh aparnesh is offline Offline
Junior Poster

Re: Prime Numbers - Help its for college

 
0
  #4
Oct 13th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 186
Reputation: aparnesh is an unknown quantity at this point 
Solved Threads: 10
aparnesh's Avatar
aparnesh aparnesh is offline Offline
Junior Poster

Re: Prime Numbers - Help its for college

 
0
  #5
Oct 14th, 2006
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. ....
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 36
Reputation: sendoshin is an unknown quantity at this point 
Solved Threads: 1
sendoshin sendoshin is offline Offline
Light Poster

Re: Prime Number Test

 
0
  #6
Oct 14th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 186
Reputation: aparnesh is an unknown quantity at this point 
Solved Threads: 10
aparnesh's Avatar
aparnesh aparnesh is offline Offline
Junior Poster

Re: Prime Numbers - Help its for college

 
0
  #7
Oct 14th, 2006
yes, checking till Square root is quicker
Last edited by aparnesh; Oct 14th, 2006 at 4:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,147
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 530
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Prime Numbers - Help its for college

 
0
  #8
Oct 14th, 2006
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
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC