I am trying to create a function that will determine if a number is prime and then if it is, add it to a Richtextbox: The function is: isitprime(x)
The way I have set it up is so that the function takes x, puts it in a loop and divides x by N. N is set to x and then decreased at the end of each loop.
Within that loop, if x/n has a remainder of 0, it adds it to a list box.
This part has been successful so far. However, next I need it to read each item in the listbox and if that number is not x or 1 then set my boolean of itcouldbeprime = false.
When its done reading the items in the listbox it either adds x to my RTB if itcouldbeprime = true or does nothing.
Here is my code so far. I think I might just need a third loop? Thanks in advance and sorry the code is so messy right now.
Public Class Form1
Public Function isitprime(ByVal x As Decimal)
Dim loopcount As Integer = x
Dim loop2count As Integer
Dim itemcounter As Integer = 0
Dim itcouldbeprime As Boolean = True
Do While loopcount > 0
Dim x2 As Decimal = x
If Decimal.Remainder(x2, loopcount) = 0 Then ListBox3.Items.Add(x2 / loopcount)
loopcount = loopcount - 1
Loop
loop2count = ListBox3.Items.Count - 1
'MsgBox(loop2count)
Do While loop2count >= 0
ListBox3.SelectedIndex = itemcounter
Label1.Text = ListBox3.SelectedItem
If ListBox3.SelectedItem.ToString <> x Or 1 Then itcouldbeprime = False Else itcouldbeprime = True
MsgBox(itcouldbeprime) 'this is returning false for x = 17 right now
If itcouldbeprime = True Then RichTextBox1.Text = RichTextBox1.Text + x.ToString + ", "
If itcouldbeprime = True Then loop2count = -1
itemcounter = itemcounter + 1
' loop2count = loop2count - 1
Loop
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
isitprime(17)
End Sub
The way I have set it up is so that the function takes x, puts it in a loop and divides x by N. N is set to x and then decreased at the end of each loop.
Within that loop, if x/n has a remainder of 0, it adds it to a list box.
This part has been successful so far. However, next I need it to read each item in the listbox and if that number is not x or 1 then set my boolean of itcouldbeprime = false.
When its done reading the items in the listbox it either adds x to my RTB if itcouldbeprime = true or does nothing.
Here is my code so far. I think I might just need a third loop? Thanks in advance and sorry the code is so messy right now.
Public Class Form1
Public Function isitprime(ByVal x As Decimal)
Dim loopcount As Integer = x
Dim loop2count As Integer
Dim itemcounter As Integer = 0
Dim itcouldbeprime As Boolean = True
Do While loopcount > 0
Dim x2 As Decimal = x
If Decimal.Remainder(x2, loopcount) = 0 Then ListBox3.Items.Add(x2 / loopcount)
loopcount = loopcount - 1
Loop
loop2count = ListBox3.Items.Count - 1
'MsgBox(loop2count)
Do While loop2count >= 0
ListBox3.SelectedIndex = itemcounter
Label1.Text = ListBox3.SelectedItem
If ListBox3.SelectedItem.ToString <> x Or 1 Then itcouldbeprime = False Else itcouldbeprime = True
MsgBox(itcouldbeprime) 'this is returning false for x = 17 right now
If itcouldbeprime = True Then RichTextBox1.Text = RichTextBox1.Text + x.ToString + ", "
If itcouldbeprime = True Then loop2count = -1
itemcounter = itemcounter + 1
' loop2count = loop2count - 1
Loop
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
isitprime(17)
End Sub