# If If statement is false then continue program flow

• (3 Pages)
• 1
• 2
• 3

## 35 Replies - 6435 Views - Last Post: 11 November 2012 - 07:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=299114&amp;s=7132af1b5af3497c3df1d94308647fd3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 lar3ry

• Coding Geezer

Reputation: 313
• Posts: 1,296
• Joined: 12-September 12

## Re: If If statement is false then continue program flow

Posted 08 November 2012 - 07:15 PM

Raminator, on 08 November 2012 - 12:39 PM, said:

lar3ry, on 08 November 2012 - 11:31 AM, said:

If the square root of (fact +1) is an integer (evenly divisible), then fact and the square root of fact + 1 is the other number.

Hmm sorry, but i didn't really get this.

OK.

4! = 24
24 + 1 = 25
sqrt(25) = 5

5! = 120
120 + 1 = 121
sqrt(121) = 11

7! = 5040
5040 + 1 =5041
sqrt(5041) = 71

4,5 5,11 and 7,71 are Brown Numbers
But...

6! = 720
720 + 1 = 721
sqrt(721) = 26.85

6 does not generate a Brown Number.

Your current program will find the only know Brown Numbers that are conecutive. The method I propose will find all known Brown Numbers, and with luck, you may even find a few more.

### #17 Raminator

• D.I.C Regular

Reputation: 1
• Posts: 292
• Joined: 16-July 12

## Re: If If statement is false then continue program flow

Posted 09 November 2012 - 07:08 AM

lar3ry, on 08 November 2012 - 07:15 PM, said:

Raminator, on 08 November 2012 - 12:39 PM, said:

lar3ry, on 08 November 2012 - 11:31 AM, said:

If the square root of (fact +1) is an integer (evenly divisible), then fact and the square root of fact + 1 is the other number.

Hmm sorry, but i didn't really get this.

OK.

4! = 24
24 + 1 = 25
sqrt(25) = 5

5! = 120
120 + 1 = 121
sqrt(121) = 11

7! = 5040
5040 + 1 =5041
sqrt(5041) = 71

4,5 5,11 and 7,71 are Brown Numbers
But...

6! = 720
720 + 1 = 721
sqrt(721) = 26.85

6 does not generate a Brown Number.

Your current program will find the only know Brown Numbers that are conecutive. The method I propose will find all known Brown Numbers, and with luck, you may even find a few more.

Hmm i understand it, its a really smart method! I really suck at maths so sorry to make another question but, i would i make so that it only gets numbers without decimals (like 1,2,3 and not 6.25,7.88, etc)?(im starting to write down the code, will post here when im done, thanks again)

### #18 Raminator

• D.I.C Regular

Reputation: 1
• Posts: 292
• Joined: 16-July 12

## Re: If If statement is false then continue program flow

Posted 09 November 2012 - 09:13 AM

Ok, i think i just managed to write a really alpha version of the code, but i believe ill need some help to get it working...
Code for now:
```Imports System.Numerics
Module Module1
Dim n As BigInteger = 1
Dim square As BigInteger
Dim count As BigInteger = 50
Dim factorial(2) As BigInteger
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)
fact = 1
End Function
Sub Main()
For nn As BigInteger = 1 To count
n = nn
factorial(0) = getfact(nn)
factorial(1) = factorial(0) + 1
square = Math.Sqrt(factorial(1))
Console.Write("(")
Console.Write(n)
Console.Write(",")
Console.Write(square)
Console.Write(")")
Console.WriteLine(" ")
'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
End Modul
```

### #19 lar3ry

• Coding Geezer

Reputation: 313
• Posts: 1,296
• Joined: 12-September 12

## Re: If If statement is false then continue program flow

Posted 09 November 2012 - 11:02 AM

Raminator, on 09 November 2012 - 10:13 AM, said:

Ok, i think i just managed to write a really alpha version of the code, but i believe ill need some help to get it working...
Code for now:
```Imports System.Numerics
Module Module1
Dim n As BigInteger = 1
Dim square As BigInteger
Dim count As BigInteger = 50
Dim factorial(2) As BigInteger
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)
fact = 1
End Function
Sub Main()
For nn As BigInteger = 1 To count
n = nn
factorial(0) = getfact(nn)
factorial(1) = factorial(0) + 1
square = Math.Sqrt(factorial(1))

```

In getfact(), the last line (fact = 1) will never execute. You don't need it anyway, because you set fact to 1 as you enter the function. You also don't need another copy of n, because getfact() doesn't mess with it.

Now you need to find the square root of square. The problem you will run into is that there is no square root function for BigInteger data type. This means that you will have to obtain the square root by other means. As well, you cannot assign the whole square root to a BigInteger if it contains a decimal.

I would suggest taking an initial guess at what two numbers the square root might lie between. In my test, I figured it would lie between 0 and square/4. So here's my method...

Make two variables: upperbound and lowerbound, and assigned the two bounds to them.
Take a guess at the number by picking a number about halfway between upperbound and lowerbound
Multiply guess * guess, and check for result being =, <, or > square.
If equal, you've found the square root, and because it's a BigInteger, there is no decimal.
if > square, make upperbound = guess, and try again
if < square, make lowerbound = guess, and try again.
When you get to a point where upperbound and lowerbound are fairly close (I used a fidderence of 10),
use a For loop that counts from lowerbound to upperbound, and try guess * guess for each value.
if any of them are equal to square, you've found one.
if not, it's not a Brown Number.

The code that narrows it down by guessing halfway between bounds is the math equivalent of a binary search, and it converges quite quickly.

I also suggest putting that calculation in a function.

### #20 Raminator

• D.I.C Regular

Reputation: 1
• Posts: 292
• Joined: 16-July 12

## Re: If If statement is false then continue program flow

Posted 09 November 2012 - 11:36 AM

lar3ry, on 09 November 2012 - 11:02 AM, said:

Raminator, on 09 November 2012 - 10:13 AM, said:

Ok, i think i just managed to write a really alpha version of the code, but i believe ill need some help to get it working...
Code for now:
```Imports System.Numerics
Module Module1
Dim n As BigInteger = 1
Dim square As BigInteger
Dim count As BigInteger = 50
Dim factorial(2) As BigInteger
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)
fact = 1
End Function
Sub Main()
For nn As BigInteger = 1 To count
n = nn
factorial(0) = getfact(nn)
factorial(1) = factorial(0) + 1
square = Math.Sqrt(factorial(1))

```

In getfact(), the last line (fact = 1) will never execute. You don't need it anyway, because you set fact to 1 as you enter the function. You also don't need another copy of n, because getfact() doesn't mess with it.

Now you need to find the square root of square. The problem you will run into is that there is no square root function for BigInteger data type. This means that you will have to obtain the square root by other means. As well, you cannot assign the whole square root to a BigInteger if it contains a decimal.

I would suggest taking an initial guess at what two numbers the square root might lie between. In my test, I figured it would lie between 0 and square/4. So here's my method...

Make two variables: upperbound and lowerbound, and assigned the two bounds to them.
Take a guess at the number by picking a number about halfway between upperbound and lowerbound
Multiply guess * guess, and check for result being =, <, or > square.
If equal, you've found the square root, and because it's a BigInteger, there is no decimal.
if > square, make upperbound = guess, and try again
if < square, make lowerbound = guess, and try again.
When you get to a point where upperbound and lowerbound are fairly close (I used a fidderence of 10),
use a For loop that counts from lowerbound to upperbound, and try guess * guess for each value.
if any of them are equal to square, you've found one.
if not, it's not a Brown Number.

The code that narrows it down by guessing halfway between bounds is the math equivalent of a binary search, and it converges quite quickly.

I also suggest putting that calculation in a function.

I just didnt get what upperbound and lowerbound are supposed to be, wich numbers?
We could use this (also here) to find the sqrt of a number

This post has been edited by Raminator: 09 November 2012 - 11:44 AM

### #21 Raminator

• D.I.C Regular

Reputation: 1
• Posts: 292
• Joined: 16-July 12

## Re: If If statement is false then continue program flow

Posted 09 November 2012 - 11:53 AM

Messing around some C# codes i have here i managed to convert a C# code that solves sqrts to VB.NET, see what you think of it.
```    Function Sqrt(number As BigInteger) As BigInteger
Dim bitLength As Integer = number.ToByteArray().Length * 8 + 1
Dim G As BigInteger = number >> bitLength \ 2
Dim LastG As BigInteger = BigInteger.Zero
Dim One As New BigInteger(1)
While True
LastG = G
G = (number / G + G) >> 1
Dim i As Integer = G.CompareTo(LastG)
If i = 0 Then
Return G
End If
If i < 0 Then
If (LastG - G).CompareTo(One) = 0 Then
If (G * G).CompareTo(number) < 0 AndAlso (LastG * LastG).CompareTo(number) > 0 Then
Return G
End If
End If
Else
If (G - LastG).CompareTo(One) = 0 Then
If (LastG * LastG).CompareTo(number) < 0 AndAlso ((G * G).CompareTo(number) > 0) Then
Return (LastG)
End If
End If
End If
End While
End Function
```

### #22 lar3ry

• Coding Geezer

Reputation: 313
• Posts: 1,296
• Joined: 12-September 12

## Re: If If statement is false then continue program flow

Posted 09 November 2012 - 11:57 AM

As I said above, "I would suggest taking an initial guess at what two numbers the square root might lie between. In my test, I figured it would lie between 0 and square/4"

Those numbers are assigned to lowerbound and upperbound. The idea is to reduce the range of numbers we need to search. Then they are further reduced by making guesses halfway between upper and lower bound, and adjusting the bounds as we go.

Yes, you could try a Newton-Raphson iteration. I didn't, because I don't know how well it would converge if using BigItegers. The Newton-Raphson iteration uses doubles, not integers.

### #23 Raminator

• D.I.C Regular

Reputation: 1
• Posts: 292
• Joined: 16-July 12

## Re: If If statement is false then continue program flow

Posted 09 November 2012 - 12:06 PM

lar3ry, on 09 November 2012 - 11:57 AM, said:

As I said above, "I would suggest taking an initial guess at what two numbers the square root might lie between. In my test, I figured it would lie between 0 and square/4"

Those numbers are assigned to lowerbound and upperbound. The idea is to reduce the range of numbers we need to search. Then they are further reduced by making guesses halfway between upper and lower bound, and adjusting the bounds as we go.

Yes, you could try a Newton-Raphson iteration. I didn't, because I don't know how well it would converge if using BigItegers. The Newton-Raphson iteration uses doubles, not integers.

You are right, after a few test newtons method fails a lot, will make a function for your method (aka. lar3ry's method).

### #24 Raminator

• D.I.C Regular

Reputation: 1
• Posts: 292
• Joined: 16-July 12

## Re: If If statement is false then continue program flow

Posted 09 November 2012 - 12:26 PM

Raminator, on 09 November 2012 - 12:06 PM, said:

lar3ry, on 09 November 2012 - 11:57 AM, said:

As I said above, "I would suggest taking an initial guess at what two numbers the square root might lie between. In my test, I figured it would lie between 0 and square/4"

Those numbers are assigned to lowerbound and upperbound. The idea is to reduce the range of numbers we need to search. Then they are further reduced by making guesses halfway between upper and lower bound, and adjusting the bounds as we go.

Yes, you could try a Newton-Raphson iteration. I didn't, because I don't know how well it would converge if using BigItegers. The Newton-Raphson iteration uses doubles, not integers.

You are right, after a few test newtons method fails a lot, will make a function for your method (aka. lar3ry's method).

Im sorry to be such a noob for coding but all this mathematical logic is melting my brain. I'm trying really hard to write that function down and im kind of stuck right now on this point.(i also believe some of it is wrong)
```    Function getsqrt(ByVal sq As BigInteger)
Dim upperbound As BigInteger
Dim lowerbound As BigInteger
Dim guess As BigInteger
Dim lp As BigInteger
upperbound = square / 4
lowerbound = 0
guess = upperbound / 2
For lp = 1 To 100
If (guess = square) Then
Return (guess)
Else
If (guess > square) Then
upperbound = square / lp
End If
If (guess < square) Then
lowerbound = (lowerbound + 0.2)
End If
End If
Next

End Function
```

### #25 lar3ry

• Coding Geezer

Reputation: 313
• Posts: 1,296
• Joined: 12-September 12

## Re: If If statement is false then continue program flow

Posted 09 November 2012 - 02:53 PM

Raminator, on 09 November 2012 - 01:26 PM, said:

Im sorry to be such a noob for coding but all this mathematical logic is melting my brain. I'm trying really hard to write that function down and im kind of stuck right now on this point.(i also believe some of it is wrong)
```    Function getsqrt(ByVal sq As BigInteger)
Dim upperbound As BigInteger
Dim lowerbound As BigInteger
Dim guess As BigInteger
Dim lp As BigInteger
upperbound = square / 4
lowerbound = 0
guess = upperbound / 2   'you want a guess about halfway between upper and lower. so (upper-lower)/2 + lower
' The calculation above should be within your loop, because it will keep changing.
For lp = 1 To 100
If (guess = square) Then    ' you want to check guess * guess = square, but you'll want to use guess * guess later, so you need another variable
Return (guess)
Else
If (guess > square) Then      ' again, guess * guess (but use the new variable)
upperbound = square / lp  ' should be upperbound = guess (do you know why?)

End If
If (guess < square) Then            'same as above
lowerbound = (lowerbound + 0.2) ' lowerbound = guess
End If
End If
Next

End Function
```

So this will narrow it down, and keep narrowing it down until you reach an interesting place.
We call this sort of place a "boundary condition", and it's where you have to consider the possible results as the two bounds come together. There are two distinct possibilities.

1. The two bounds will be consecutive numbers. In this case, you are out of guess numbers, as there is no number between the bounds. Does this mean that guess is not the square root of the number being tested?

2. The 2 bounds will be farther apart by a small amount. Try to visualize what happens when they are separated by 2, 3, or 4 numbers, that is, with 1, 2, or 3 numbers between them. I avoided that problem by switching tactics when the difference between upper and lower bounds was 10 or less, but that may not be the optimum solution.

When in doubt, give it a try. I took the easy way out, but it would be better if just narrowing it down to two consecutive numbers, or to the single number between the two bounds does the trick.

Edit: I left comments in the quoted code.

This post has been edited by lar3ry: 09 November 2012 - 02:54 PM

### #26 Raminator

• D.I.C Regular

Reputation: 1
• Posts: 292
• Joined: 16-July 12

## Re: If If statement is false then continue program flow

Posted 09 November 2012 - 05:36 PM

lar3ry, on 09 November 2012 - 02:53 PM, said:

Raminator, on 09 November 2012 - 01:26 PM, said:

Im sorry to be such a noob for coding but all this mathematical logic is melting my brain. I'm trying really hard to write that function down and im kind of stuck right now on this point.(i also believe some of it is wrong)
```    Function getsqrt(ByVal sq As BigInteger)
Dim upperbound As BigInteger
Dim lowerbound As BigInteger
Dim guess As BigInteger
Dim lp As BigInteger
upperbound = square / 4
lowerbound = 0
guess = upperbound / 2   'you want a guess about halfway between upper and lower. so (upper-lower)/2 + lower
' The calculation above should be within your loop, because it will keep changing.
For lp = 1 To 100
If (guess = square) Then    ' you want to check guess * guess = square, but you'll want to use guess * guess later, so you need another variable
Return (guess)
Else
If (guess > square) Then      ' again, guess * guess (but use the new variable)
upperbound = square / lp  ' should be upperbound = guess (do you know why?)

End If
If (guess < square) Then            'same as above
lowerbound = (lowerbound + 0.2) ' lowerbound = guess
End If
End If
Next

End Function
```

So this will narrow it down, and keep narrowing it down until you reach an interesting place.
We call this sort of place a "boundary condition", and it's where you have to consider the possible results as the two bounds come together. There are two distinct possibilities.

1. The two bounds will be consecutive numbers. In this case, you are out of guess numbers, as there is no number between the bounds. Does this mean that guess is not the square root of the number being tested?

2. The 2 bounds will be farther apart by a small amount. Try to visualize what happens when they are separated by 2, 3, or 4 numbers, that is, with 1, 2, or 3 numbers between them. I avoided that problem by switching tactics when the difference between upper and lower bounds was 10 or less, but that may not be the optimum solution.

When in doubt, give it a try. I took the easy way out, but it would be better if just narrowing it down to two consecutive numbers, or to the single number between the two bounds does the trick.

Edit: I left comments in the quoted code.

ok, i rewrote the function correcting it accordingly with your comments. No, i dont understand why both the upper and lower bound must be = to the guess(0) (look at the new function)
New Function:
```    Function getsqrt(ByVal sq As BigInteger)
Dim upperbound As BigInteger
Dim lowerbound As BigInteger
Dim guess(2) As BigInteger
Dim lp As BigInteger
upperbound = square / 4
lowerbound = 0
For lp = 1 To 100
guess(0) = ((upperbound - lowerbound) / 2) + lowerbound
guess(1) = guess(0) * guess(0)
If (guess(1) = square) Then
Return (guess)
Else
If (guess(1) > square) Then
upperbound = guess(0)
End If
If (guess(1) < square) Then            'same as above
lowerbound = guess(0) ' lowerbound = guess
End If
End If
Next
End Function
```

### #27 lar3ry

• Coding Geezer

Reputation: 313
• Posts: 1,296
• Joined: 12-September 12

## Re: If If statement is false then continue program flow

Posted 09 November 2012 - 09:29 PM

Raminator, on 09 November 2012 - 06:36 PM, said:

ok, i rewrote the function correcting it accordingly with your comments. No, i dont understand why both the upper and lower bound must be = to the guess(0) (look at the new function)
New Function:
```    Function getsqrt(ByVal sq As BigInteger)
Dim upperbound As BigInteger
Dim lowerbound As BigInteger
Dim guess(2) As BigInteger
Dim lp As BigInteger
upperbound = square / 4   ' change square to sq. You are testing only one number.
lowerbound = 0
For lp = 1 To 100         ' I just realized you are using a For. This needsa to be a Do/Loop, and you need a way to drop out of it or use Return(square root) or Return(0)

```

OK, a few minor things, and you're on the right track. See comments inthe code.

The purpose of upperbound and lowerbound is to narrow the search for the square root. First you generate a guess. When you multiply guess*guess and find it to be larger than square, it means that your next guess has to be lower. So you set upperbound to guess, and generate another guess. Then if guess*guess is smaller than square, you know the your next guess has to be higher, so you set lowerbound to guess, and generate another guess.

Remembering that all operations and results on these numbers are integers, here's what happens when we follow the algotithm:
Looking for square root of 25
upperbound = 25/4 (6)
lowerbound = 0
first guess = ((6-0)/2) +0 = 3 ((upper-lower)/2) + lower
3*3 = 9 (too low)
upperbound still 6
lowerbound = guess (3)
next guess = ((6-3)/2) + 3 = 4 ((upper-lower)/2) + lower
upperbound still 6
lowerbound = ((6-4)/2) + 3 = 5 ((upper-lower)/2) + lower
guess * guess = 25 (Yes!)

By the way, if you want to see what the result of an integer division will be, bring up the windows calculator and click on the menu View->Programmer. Select RadioButton 'Decimal'. and do your divides.

### #28 Raminator

• D.I.C Regular

Reputation: 1
• Posts: 292
• Joined: 16-July 12

## Re: If If statement is false then continue program flow

Posted 10 November 2012 - 06:36 AM

lar3ry, on 09 November 2012 - 09:29 PM, said:

Raminator, on 09 November 2012 - 06:36 PM, said:

ok, i rewrote the function correcting it accordingly with your comments. No, i dont understand why both the upper and lower bound must be = to the guess(0) (look at the new function)
New Function:
```    Function getsqrt(ByVal sq As BigInteger)
Dim upperbound As BigInteger
Dim lowerbound As BigInteger
Dim guess(2) As BigInteger
Dim lp As BigInteger
upperbound = square / 4   ' change square to sq. You are testing only one number.
lowerbound = 0
For lp = 1 To 100         ' I just realized you are using a For. This needsa to be a Do/Loop, and you need a way to drop out of it or use Return(square root) or Return(0)

```

OK, a few minor things, and you're on the right track. See comments inthe code.

The purpose of upperbound and lowerbound is to narrow the search for the square root. First you generate a guess. When you multiply guess*guess and find it to be larger than square, it means that your next guess has to be lower. So you set upperbound to guess, and generate another guess. Then if guess*guess is smaller than square, you know the your next guess has to be higher, so you set lowerbound to guess, and generate another guess.

Remembering that all operations and results on these numbers are integers, here's what happens when we follow the algotithm:
Looking for square root of 25
upperbound = 25/4 (6)
lowerbound = 0
first guess = ((6-0)/2) +0 = 3 ((upper-lower)/2) + lower
3*3 = 9 (too low)
upperbound still 6
lowerbound = guess (3)
next guess = ((6-3)/2) + 3 = 4 ((upper-lower)/2) + lower
upperbound still 6
lowerbound = ((6-4)/2) + 3 = 5 ((upper-lower)/2) + lower
guess * guess = 25 (Yes!)

By the way, if you want to see what the result of an integer division will be, bring up the windows calculator and click on the menu View->Programmer. Select RadioButton 'Decimal'. and do your divides.

Ok, I re-wrote the function. Now its uses sq instead of square and a Do loop instead of a For loop.( I'm not sure im using the Do loop the right way, i checked the MSDN documentationfor it.

### #29 lar3ry

• Coding Geezer

Reputation: 313
• Posts: 1,296
• Joined: 12-September 12

## Re: If If statement is false then continue program flow

Posted 10 November 2012 - 08:00 AM

Raminator, on 10 November 2012 - 07:36 AM, said:

Ok, I re-wrote the function. Now its uses sq instead of square and a Do loop instead of a For loop.( I'm not sure im using the Do loop the right way, i checked the MSDN documentationfor it.

Sounds good. Even though you didn't post your code for those changes, I think you were close enough on the previous code you did post. I used your code, made those changes I suggested, and it works, so I am going to post it here. I won't explain much right now, as I have to go out for a while, but I'd like you to look it over, try researching anything you don't understand, and ask any questions here on any concepts you are unsure of after that.

There are a few things in the code that I like to do, that are optional, and are just one way of doing things.

Please start a new project, a windows form one, and place two buttons on it (Button1 and Button2), and paste the entire code into it., just to make sure it all matches, subroutines and functions, etc.

```Option Strict On
Option Explicit On
Imports System.Numerics

Public Class Form1
Dim fact As BigInteger = 1
Dim square As BigInteger
Dim count As BigInteger = 10000
Dim PartyOn As Boolean

Private Sub Main()
Dim c As BigInteger
Console.ForegroundColor = ConsoleColor.Yellow
PartyOn = True
For n = 4 To count
Application.DoEvents()
If Not PartyOn Then Exit For
For c = 1 To n
fact = fact * c
Next
square = getsqrt(fact + 1)
' If n Mod 100 = 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
Console.WriteLine("Done")
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
Application.DoEvents()
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

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Main()
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
PartyOn = False
End Sub
End Class

```

### #30 Raminator

• D.I.C Regular

Reputation: 1
• Posts: 292
• Joined: 16-July 12

## Re: If If statement is false then continue program flow

Posted 11 November 2012 - 03:37 PM

lar3ry, on 10 November 2012 - 08:00 AM, said:

Raminator, on 10 November 2012 - 07:36 AM, said:

Ok, I re-wrote the function. Now its uses sq instead of square and a Do loop instead of a For loop.( I'm not sure im using the Do loop the right way, i checked the MSDN documentationfor it.

Sounds good. Even though you didn't post your code for those changes, I think you were close enough on the previous code you did post. I used your code, made those changes I suggested, and it works, so I am going to post it here. I won't explain much right now, as I have to go out for a while, but I'd like you to look it over, try researching anything you don't understand, and ask any questions here on any concepts you are unsure of after that.

There are a few things in the code that I like to do, that are optional, and are just one way of doing things.

Please start a new project, a windows form one, and place two buttons on it (Button1 and Button2), and paste the entire code into it., just to make sure it all matches, subroutines and functions, etc.

```Option Strict On
Option Explicit On
Imports System.Numerics

Public Class Form1
Dim fact As BigInteger = 1
Dim square As BigInteger
Dim count As BigInteger = 10000
Dim PartyOn As Boolean

Private Sub Main()
Dim c As BigInteger
Console.ForegroundColor = ConsoleColor.Yellow
PartyOn = True
For n = 4 To count
Application.DoEvents()
If Not PartyOn Then Exit For
For c = 1 To n
fact = fact * c
Next
square = getsqrt(fact + 1)
' If n Mod 100 = 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
Console.WriteLine("Done")
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
Application.DoEvents()
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

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Main()
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
PartyOn = False
End Sub
End Class

```

Oops, sorry i forgot to put the code last time, this is how the function code is looking now:
```    Function getsqrt(ByVal sq As BigInteger)
Dim upperbound As BigInteger
Dim lowerbound As BigInteger
Dim guess(2) As BigInteger
Dim lp As BigInteger
upperbound = sq / 4
lowerbound = 0
Do While (guess(1) <> sq)
guess(0) = ((upperbound - lowerbound) / 2) + lowerbound
guess(1) = guess(0) * guess(0)
If (guess(1) = sq) Then
Return (guess)
Exit Do
Else
If (guess(1) > sq) Then
upperbound = guess(0)
End If
If (guess(1) < sq) Then
lowerbound = guess(0)
End If
End If
Loop
End Function
```

I hope im doing it right.
I did what you sad, created a windows forms application with the 2 buttons,etc,etc and clicked button 1 and button 2 sometimes, it seemed to be working fine, even though i could see no console window, and therefore no output of the code.