If statement

  • Thread starter Thread starter Jon Cosby
  • Start date Start date
J

Jon Cosby

Someone see why this is? If GaussPrime returns true, the debugger doesn't
reach the next line in Factor. It goes directly the next line in the Click
event. If false, it goes to the next block until it finds a prime.


Private Function Factor(ByVal g As CNum) As CNum
...
If GaussPrime(g) Then
' No more factors
index += 1 ' Never reaches this breaking point
...


Private Function GaussPrime(ByVal g As CNum) As Boolean
Dim N As Integer = Norm(g) ' Norm: re*re + im*im
If PrimeNum(N) Then
GaussPrime = True
ElseIf g.Im = 0 Then
If PrimeNum(g.Re) And g.Re Mod 4 = 3 Then
GaussPrime = True
End If
Else
GaussPrime = False
End If
End Function
 
Hi Jon,

I do not like the ElseIf,

I wrote it like this and it has a lot of false situations in my opinion.

If PrimeNum(N) Then
GaussPrime = True
Else
If g.Im = 0 Then
If PrimeNum(g.Re) And g.Re Mod 4 = 3 Then
GaussPrime = True
End If
Else
GaussPrime = False
End If
End If
 
Well, if im is 0, the Norm is a square and not a prime number. I tried your
suggestion, but get the same problem.


Jon
 
Jon Cosby said:
Someone see why this is? If GaussPrime returns true, the debugger doesn't
reach the next line in Factor. It goes directly the next line in the Click
event. If false, it goes to the next block until it finds a prime.

Jon... you seem to be making an assumption that the GaussPrime function has
something to do with it. You need to find that out before you stare at the
code...

Edit your GaussPrime and have it return "False" (and alternatively "True")
without any algorithm executing. If you experience the same problem I sure
wouldn't spend much time checking the function any further.

Tom
 
Jon Cosby said:
Someone see why this is? If GaussPrime returns true, the debugger doesn't
reach the next line in Factor. It goes directly the next line in the Click
event. If false, it goes to the next block until it finds a prime.

What have you done to verify that GaussPrime actually returns true in those
cases where you believe it should? I find that when I experience something
so blatantly wrong with one segment of code, the error I have made is
somewhere else.
Private Function Factor(ByVal g As CNum) As CNum
...
If GaussPrime(g) Then
' No more factors
index += 1 ' Never reaches this breaking point
...

Does the above if-endif block have an else clause?
Private Function GaussPrime(ByVal g As CNum) As Boolean
Dim N As Integer = Norm(g) ' Norm: re*re + im*im
If PrimeNum(N) Then
GaussPrime = True
ElseIf g.Im = 0 Then
If PrimeNum(g.Re) And g.Re Mod 4 = 3 Then
GaussPrime = True

suggest that all possibilities be considered by adding this else clause:

else
GaussPrime = False
End If
Else
GaussPrime = False
End If
End Function


/Al
 
Back
Top