I am writing my own script for a simple calculator that determines if a number is prime, and its prime factors. Currently my code looks like:
- Private Sub primebutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles primebutton.Click
-
Dim oWrite As System.IO.StreamWriter
-
oWrite = File.CreateText("C:\TempFactors.txt")
-
TestLabel.Text = ""
-
'Determines perfect square qualifications on following 5 lines
-
Dim root As Double
-
Dim compare As Integer
-
root = System.Math.Sqrt(answer)
-
compare = CInt(CLng(root) \ 1)
-
If root = compare Then TestLabel.Text = "Perfect Square Composite (Not Prime)"
-
'Determines if number is prime
-
Dim n1 As Double
-
Dim n2 As Double
-
Dim n3 As Double
-
Dim loopcount As Double
-
n3 = compare / 1
-
loopcount = compare - 2
-
Do Until loopcount = 0
-
n1 = answer / n3
-
n2 = CLng(n1) \ 1
-
If n1 = n2 Then oWrite.Write(n2.ToString, n3.ToString)
-
End
-
loopcount = loopcount - 1
-
Loop
-
oWrite.Close()
-
End Sub
The loopcount = compare - 2 means I want the loop to be 2 less than the square root of the answer so that when do a descending calculation (loopcount = loopcount - 1) it doesn't see if 1 or 0 is a factor of the answer. for the calculations after that, I want, every time n1 = n2 (a whole number factor of the answer), I want to report those results to a text file (n2 and n3) in this format "(n2, n3)" then go to the next line. and continue to execute the loop until the loopcount is zero. Currently, when I run the code, the application closes when I click the prime button (button that runs this code) and I don't know why. This is my first semester in school, and have never coded before, so I am a definite laymen with code, but I really really want to learn it (hence why for fun im just creating algorithms for different things). Please help!! What did I do wrong and how can I fix this problem. Thanks in advance.
-Nick