| | |
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).
Please anyone, correct me if I am wrong. It is the best way for me to learn.
you think this woul work?
I just need the ocde here bit i think, but how do i get it to know whether a number is whole or not?
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
option explicit dim prime as boolean dim number as integer dim x as integer num = inputbox("Enter a whole number up to 100") for x = 1 to 100 (code here) 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.
U can try this
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
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Do while true Dim x as Long Dim myNum as long myNum = val(InputBox ("Enter A Number")) Dim mFlag as Byte mFlag = 0 For x = 1 to Clng(myNum/2) If myNum Mod x = 0 then mflag = 1 exit for end if next if mflag = 0 then Msgbox "Prime Number" Exit sub end if 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
Sorry for the howler in the code. The For Loop should be
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
For x = 2 to Clng(myNum/2) ....
•
•
Join Date: Sep 2006
Posts: 36
Reputation:
Solved Threads: 1
The above code is a great example, but there is a way to make it faster. Consider the following:
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:
And now for the code which will use the IsPrime function to solve your problem:
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:
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
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Input number: 100 100 / 2: 50 Sqr(100): 10
First, the IsPrime Function:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Function IsPrime(checkValue As Long) As Boolean For testValue = 2 To CLng(Sqr(checkValue)) Step 1 If checkValue Mod testValue = 0 Then IsPrime = False Exit Function End If Next IsPrime = True End Function
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Sub Main() Do userValue = CLng(InputBox("Enter a value to test. I will tell you if it is prime or not.", "Prime Tester", userValue)) If IsPrime(userValue) Then If MsgBox(userValue & " is a prime number! Congratulations! Would you like to try another?", vbYesNo, "Number is Prime!") = vbNo Then End End If End If Loop End Sub
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Sub Main() Do userValue = CLng(InputBox("Enter a value to test. I will tell you every prime up to (and possibly including) it.", "Prime Tester", userValue)) primesList = "1" For countValue = 2 To CLng(userValue) Step 1 If IsPrime(CLng(countValue)) Then primesList = primesList & ", " & CStr(countValue) End If Next 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 End End If Loop End Sub
As always, enjoy!
- Sendoshin
![]() |
Similar Threads
- (Noob) need some help w/ Prime Numbers (C++)
- Prime Numbers (C)
- prime numbers homework trouble (C++)
- help with array that determines prime numbers (C)
- prime numbers (C++)
- prime numbers (C++)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Problem while inserting into oracle date values frm vb
- Next Thread: need suggestion on this project
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows






