Not Yet Answered # Prime Numbers - Help its for college

DavidRyan 5 Discussion Starter jbennet 1,618 aparnesh 9 aparnesh 9 sendoshin 6 Discussion Starter jbennet 1,618

0

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).

0

you think this woul work?

```
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?

0

U can try this

```
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

0

Sorry for the howler in the code. The For Loop should be

```
For x = 2 to Clng(myNum/2)
....
```

0

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

```
Input number: 100
100 / 2: 50
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:

```
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
```

And now for the code which will use the IsPrime function to solve your problem:

```
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
```

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:

```
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
```

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

0

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

**Isn't it about time forums rewarded their contributors?**

Contribute to this discussion and earn rewards points that can be cashed out for dollars.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:

```
try
{
Runtime run = Runtime.getRuntime();
Process pr = ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`