help with string manipulation

propagandhi

Member
Joined
Feb 20, 2005
Messages
18
Programming Experience
Beginner
Hello everybody,

Im taking a vb.net class and im having a few problems with a prime number project. i can determine if the number is prime, but im struggling with displaying the divisors of non prime numbers.

Anyone have any ideas?
 
Wouldn't want to do your homework for you, so I'll leave it as mostly conceptual.

VB.NET:
		Dim x As Integer
		Dim y As Integer

		For x = 1 To 10
			For y = 1 To 10
				If x Mod y = 0 Then ' that is if y divides into x evenly
				 TextBox1.Text &= y & "," & x & "	 "
				End If
			Next
		Next

Prints out the following:


1,1 1,2 2,2 1,3 3,3 1,4 2,4 4,4 1,5 5,5 1,7 7,7 1,8 2,8 4,8 8,8 1,9 3,9 9,9 1,10 2,10 5,10 10,10

The print out the first number is y second is x. So y * ? = x, but if you look at the pattern it says the same problem in reverse later, ? * Y = X.

Now if you look at it (i'll use the 6's)
1,6
2,6
3,6
6,6

1*6 = 6
2*3 = 6

for ones like the 9's
1,9
3,9
9,9

1*9 = 9
3*3 = 9

All you have to do is match the first in a set of x values with the last in a set of x values, then remove those, repeat until either 0 or 1 middle set of y,x values remains. The one that is left is a square root. Of course this would be done before the next x value after the the y's are finished


PS

a set of x values = 1,4 2,4 4,4
A set of y,x values = 1,3
 
Last edited:
What i have looks somewhat like what you posted, but it appears you have 2 for next statements? also, im having trouble with my if statement. When i compile .net doesnt report any errors, but when i go to do the calculation, my if statement doesnt work.

Thanks,
Steve


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click

'declaring variables

Dim IntRemainder As Integer

Dim IntNumber As Integer

Dim intDiv As Integer

'converting

IntNumber = Convert.ToInt32(txtInt.Text)

'IF statement to determine prime or not prime

For intDiv = 1 To 100 Step 1

IntRemainder = IntNumber
Mod Convert.ToInt32(intDiv)

Next intDiv

If IntRemainder = 0 Then

lblPrime.Text = "Not Prime"

lbDivisions.Items.Add(IntNumber)

lbDivisions.Items.Add(1)

ElseIf Not IntRemainder = 0 Then

lblPrime.Text = "Prime"

lbDivisions.Items.Add(1)

lbDivisions.Items.Add(intDiv)

End If

End Sub



 
Last edited:
The way your example is coded, which is telling me everything is prime it seems like it is looking to see if a number is divisible by anything without a remander.

What you are doing is not finding prime numbers, but rather just looking to see what the number is evenly divisible by.

You have two main problems that I see, your if statements need to be inside the for...next statement, otherwise they just work with the last result.

Secondly your if else statements are not looking for number of divisors, they are rather looking for the remander.

VB.NET:
		lbDivisions.Items.Clear()

		'declaring variables

		Dim IntRemainder As Integer
		Dim IntNumber As Integer = txtInt.Text
		Dim intDiv As Integer

		'IF statement to determine prime or not prime

		For intDiv = 1 To 100 Step 1

			IntRemainder = IntNumber Mod intDiv

			If IntRemainder = 0 Then

				lbDivisions.Items.Add(intDiv)
			End If

		Next intDiv

		If lbDivisions.Items.Count = 2 Then
			lblPrime.Text = "Prime"
		Else
			lblPrime.Text = "Not Prime"
		End If

This way the code finds the divisors that divide evenly, and adds them to the list, then it checks the list to see how many numbers are there. If there are only two numbers it is logically prime, any more than two and it can not be prime, and less and the number is one which is arguably not prime.


On the second next statement for my code, your code uses a user imput (eg x = 3) and searches for all even divisors for it. My code works by searching for all numbers in the first range (eg x = 2 -10) and then does what your code does and looks for even divisors. With a little modification, your code can be wrapped into my code to have the same result.

VB.NET:
' declarations
	 Dim IntRemainder As Integer
		Dim IntNumber As Integer
		Dim intDiv As Integer

		For IntNumber = 1 To 100 ' this first number replaces the text box

			For intDiv = 1 To 100 ' this number is the divisor

					 ' calculates and checks value at the same time
				If IntNumber Mod intDiv = 0 Then 
					 lbDivisions.Items.Add(intDiv)
					End If
							 
				 Next intDiv ' goes back and tries another divisor

						 'checks to see if their are more than two even divisors and adds it to the label if it is prime
				 If lbDivisions.Items.Count = 2 Then
			    lblPrime.Text &= IntNumber & ", "

			End If
' gets ready for the next dividend
			lbDivisions.Items.Clear()

		Next

Walla, the label displays all numbers that are prime. You could also have a second listbox, instead of the label.
 
Last edited:
Back
Top