If If statement is false then continue program flow

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 3341 Views - Last Post: 11 November 2012 - 07:42 PM Rate Topic: -----

#31 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: If If statement is false then continue program flow

Posted 11 November 2012 - 06:13 PM

View PostRaminator, on 11 November 2012 - 04:37 PM, said:

Oops, sorry i forgot to put the code last time, this is how the function code is looking now:


Well as I said, it's close. The reason I turn on Option Strict and Option Explicit is so that problem areas will be pointed out to you as you type, and mostly, will not let you compile when there's a problem.

Your Function getsqrt() should have an "As" clause, so you should add As BigInteger after the ")".

Also, it would have shown you an error saying "Function 'getsqrt' doesn't return a value on all paths." What this means is that there is a possibility of returning a NULL reference. It won't always matter, but it's a good practice to do it right, for the times when it does matter, and you have to figure out why your program is failing, sometimes because of an obscure and difficult to find reason.

As for the Console, you will see, if you run the code using Visual Studio, the output will show up in the Output window.
I put the second button in there to stop the program, though when running from Visual Studio, you can stop it with the Debugger.

Here's the code, for a Console version (as you wanted).
Option Strict On
Option Explicit On

Imports System.Numerics

Module Module1
    Dim fact As BigInteger = 1
    Dim square As BigInteger
    Dim count As BigInteger = 10000

    Sub Main()
        Dim c As BigInteger
        Console.ForegroundColor = ConsoleColor.Yellow
        For n = 4 To count
            For c = 1 To n
                fact = fact * c
            Next
            square = getsqrt(fact + 1)
            If n Mod 10 = 0 Then Console.WriteLine(n) ' uncomment to  monitor progress
            If square <> 0 Then
                Console.Write("Found! ")
                Console.Write((c - 1))
                Console.Write(",")
                Console.WriteLine(square)
            End If
            fact = 1
        Next
    End Sub

    Function getsqrt(ByVal sq As BigInteger) As BigInteger
        Dim upperbound As BigInteger
        Dim lowerbound As BigInteger
        Dim guess As BigInteger
        Dim guessguess As BigInteger
        Dim keepgoing As Boolean = True

        getsqrt = 0
        upperbound = sq / 4
        lowerbound = 0
        Do While keepgoing
            guess = ((upperbound - lowerbound) / 2) + lowerbound
            guessguess = guess * guess
            If (guessguess = sq) Then
                getsqrt = guess
                keepgoing = False
            Else
                If (guessguess > sq) Then
                    upperbound = guess
                End If
                If (guessguess < sq) Then
                    lowerbound = guess
                End If
                If (lowerbound = upperbound) Or (lowerbound + 1 = upperbound) Then
                    keepgoing = False
                End If
            End If
        Loop
    End Function
End Module



A few items of note:
There are two ways of returning a value from a function. One is to use Return(value), This is fine. Perfectly legal and good code. The disadvantage of it is when you have multiple Return statements in different parts of the function. It means that ther are more than one exit from the function. Personally (and you may differ in your opinion), I like to have only one exit from the function, and so, if I use Return(value) I set the value in all the places where a Return would also work, and then use the Return(value) at the end of the function.

Alternatively (and I like this way a lot), you can set the variable named the same as the function, to the return value, and when you fall out of the function, you return the value in that variable.

So up there, right after the Dim statements, I have getsqrt = 0, making it the default return value. As long as I never find a number that I'm looking for, all I need to do is finish the function, and the default value comes back.

You did well. I was glad to see your efforts to try suggestions and your willingness to learn.
Was This Post Helpful? 1
  • +
  • -

#32 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: If If statement is false then continue program flow

Posted 11 November 2012 - 06:48 PM

View Postlar3ry, on 11 November 2012 - 06:13 PM, said:

View PostRaminator, on 11 November 2012 - 04:37 PM, said:

Oops, sorry i forgot to put the code last time, this is how the function code is looking now:


Well as I said, it's close. The reason I turn on Option Strict and Option Explicit is so that problem areas will be pointed out to you as you type, and mostly, will not let you compile when there's a problem.

Your Function getsqrt() should have an "As" clause, so you should add As BigInteger after the ")".

Also, it would have shown you an error saying "Function 'getsqrt' doesn't return a value on all paths." What this means is that there is a possibility of returning a NULL reference. It won't always matter, but it's a good practice to do it right, for the times when it does matter, and you have to figure out why your program is failing, sometimes because of an obscure and difficult to find reason.

As for the Console, you will see, if you run the code using Visual Studio, the output will show up in the Output window.
I put the second button in there to stop the program, though when running from Visual Studio, you can stop it with the Debugger.

Here's the code, for a Console version (as you wanted).
Option Strict On
Option Explicit On

Imports System.Numerics

Module Module1
    Dim fact As BigInteger = 1
    Dim square As BigInteger
    Dim count As BigInteger = 10000

    Sub Main()
        Dim c As BigInteger
        Console.ForegroundColor = ConsoleColor.Yellow
        For n = 4 To count
            For c = 1 To n
                fact = fact * c
            Next
            square = getsqrt(fact + 1)
            If n Mod 10 = 0 Then Console.WriteLine(n) ' uncomment to  monitor progress
            If square <> 0 Then
                Console.Write("Found! ")
                Console.Write((c - 1))
                Console.Write(",")
                Console.WriteLine(square)
            End If
            fact = 1
        Next
    End Sub

    Function getsqrt(ByVal sq As BigInteger) As BigInteger
        Dim upperbound As BigInteger
        Dim lowerbound As BigInteger
        Dim guess As BigInteger
        Dim guessguess As BigInteger
        Dim keepgoing As Boolean = True

        getsqrt = 0
        upperbound = sq / 4
        lowerbound = 0
        Do While keepgoing
            guess = ((upperbound - lowerbound) / 2) + lowerbound
            guessguess = guess * guess
            If (guessguess = sq) Then
                getsqrt = guess
                keepgoing = False
            Else
                If (guessguess > sq) Then
                    upperbound = guess
                End If
                If (guessguess < sq) Then
                    lowerbound = guess
                End If
                If (lowerbound = upperbound) Or (lowerbound + 1 = upperbound) Then
                    keepgoing = False
                End If
            End If
        Loop
    End Function
End Module



A few items of note:
There are two ways of returning a value from a function. One is to use Return(value), This is fine. Perfectly legal and good code. The disadvantage of it is when you have multiple Return statements in different parts of the function. It means that ther are more than one exit from the function. Personally (and you may differ in your opinion), I like to have only one exit from the function, and so, if I use Return(value) I set the value in all the places where a Return would also work, and then use the Return(value) at the end of the function.

Alternatively (and I like this way a lot), you can set the variable named the same as the function, to the return value, and when you fall out of the function, you return the value in that variable.

So up there, right after the Dim statements, I have getsqrt = 0, making it the default return value. As long as I never find a number that I'm looking for, all I need to do is finish the function, and the default value comes back.

You did well. I was glad to see your efforts to try suggestions and your willingness to learn.

So, i believe we are almost over with this project ( :) ) this is how the code is looking like now(i did a few changes to make it look a little better)
Imports System.Numerics
Module Module1
    Dim n As BigInteger = 1
    Dim square As BigInteger
    Dim count As BigInteger = 50
    Dim factorial(2) As BigInteger
    Sub Main()
        For nn As BigInteger = 1 To count
            factorial(0) = getfact(nn)
            factorial(1) = factorial(0) + 1
            writeresult()
            'troubleshoot()
        Next
    End Sub
    Private Sub troubleshoot()
        Console.Write(n)
        Console.Write(",")
        Console.Write(factorial(0))
        Console.Write(",")
        Console.Write(factorial(1))
        Console.Write(",")
        Console.WriteLine(square)
    End Sub
    Private Sub writeresult()
        Console.Write("(")
        Console.Write(n)
        Console.Write(",")
        Console.Write(square)
        Console.Write(")")
        Console.WriteLine(" ")
    End Sub
    Function getfact(ByVal num As BigInteger) As BigInteger
        Dim fact As BigInteger = 1
        For c As BigInteger = 1 To num
            fact = fact * c
        Next
        Return (fact)
    End Function
    Function getsqrt(ByVal sq As BigInteger) As BigInteger
        Dim upperbound As BigInteger
        Dim lowerbound As BigInteger
        Dim guess As BigInteger
        Dim guessguess As BigInteger
        Dim keepgoing As Boolean = True

        getsqrt = 0
        upperbound = sq / 4
        lowerbound = 0
        Do While keepgoing
            guess = ((upperbound - lowerbound) / 2) + lowerbound
            guessguess = guess * guess
            If (guessguess = sq) Then
                getsqrt = guess
                keepgoing = False
            Else
                If (guessguess > sq) Then
                    upperbound = guess
                End If
                If (guessguess < sq) Then
                    lowerbound = guess
                End If
                If (lowerbound = upperbound) Or (lowerbound + 1 = upperbound) Then
                    keepgoing = False
                End If
            End If
        Loop
    End Function
End Module


Now, I believe the only thing missing here now is the if statement to check if the number is or isnt a brown number after all. I believe that If statement would look like this (correct me if im wrong)
    Sub Main()
        For nn As BigInteger = 1 To count
            factorial(0) = getfact(nn)
            factorial(1) = factorial(0) + 1
            square = getsqrt(factorial(1))
            If (nn + 1 = square) Then
                writeresult()
            End If
            'troubleshoot()
        Next
    End Sub

Was This Post Helpful? 0
  • +
  • -

#33 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: If If statement is false then continue program flow

Posted 11 November 2012 - 07:00 PM

Looks good!
Was This Post Helpful? 1
  • +
  • -

#34 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: If If statement is false then continue program flow

Posted 11 November 2012 - 07:14 PM

View Postlar3ry, on 11 November 2012 - 07:00 PM, said:

Looks good!

Yeah, i thought it would work too but it isnt working as I (or we) hoped, its doing like the old code and only returning (4,5) or numbers followed one by another.
This is the full code:
Imports System.Numerics
Module Module1
    Dim n As BigInteger = 1
    Dim square As BigInteger
    Dim count As BigInteger = 500
    Dim factorial(2) As BigInteger
    Sub Main()
        For nn As BigInteger = 1 To count
            factorial(0) = getfact(nn)
            factorial(1) = factorial(0) + 1
            square = getsqrt(factorial(1))
            If (nn + 1 = square) Then
                writeresult()
            End If
            'troubleshoot()
        Next
    End Sub
    Private Sub troubleshoot()
        Console.Write(n)
        Console.Write(",")
        Console.Write(factorial(0))
        Console.Write(",")
        Console.Write(factorial(1))
        Console.Write(",")
        Console.WriteLine(square)
    End Sub
    Private Sub writeresult()
        Console.Write("(")
        Console.Write(n)
        Console.Write(",")
        Console.Write(square)
        Console.Write(")")
        Console.WriteLine(" ")
    End Sub
    Function getfact(ByVal num As BigInteger) As BigInteger
        Dim fact As BigInteger = 1
        For c As BigInteger = 1 To num
            fact = fact * c
        Next
        Return (fact)
    End Function
    Function getsqrt(ByVal sq As BigInteger) As BigInteger
        Dim upperbound As BigInteger
        Dim lowerbound As BigInteger
        Dim guess As BigInteger
        Dim guessguess As BigInteger
        Dim keepgoing As Boolean = True

        getsqrt = 0
        upperbound = sq / 4
        lowerbound = 0
        Do While keepgoing
            guess = ((upperbound - lowerbound) / 2) + lowerbound
            guessguess = guess * guess
            If (guessguess = sq) Then
                getsqrt = guess
                keepgoing = False
            Else
                If (guessguess > sq) Then
                    upperbound = guess
                End If
                If (guessguess < sq) Then
                    lowerbound = guess
                End If
                If (lowerbound = upperbound) Or (lowerbound + 1 = upperbound) Then
                    keepgoing = False
                End If
            End If
        Loop
    End Function
End Module

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#35 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: If If statement is false then continue program flow

Posted 11 November 2012 - 07:35 PM

Oh, sorry, I didn't read it carefully enough.

NN + 1 will only solve for consecutive numbers fitting the criteria.

Stick with If square <> 0 from the code I supplied.

If it's 0, there is no square root of matching number.
Was This Post Helpful? 1
  • +
  • -

#36 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: If If statement is false then continue program flow

Posted 11 November 2012 - 07:42 PM

View Postlar3ry, on 11 November 2012 - 07:35 PM, said:

Oh, sorry, I didn't read it carefully enough.

NN + 1 will only solve for consecutive numbers fitting the criteria.

Stick with If square <> 0 from the code I supplied.

If it's 0, there is no square root of matching number.

It works!!!!
Ill set the counter to a really big value and see if it finds anything by tomorrow.
Thanks a lot for all your help, this would never have been possible without it.
:tup: You are awesome!
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3