Raminator, 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.

New Topic/Question
Reply





MultiQuote



|