Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB10

VS.NET 2010 (BIGINTEGER): MISSING 'SQRT' FUNCTION

3.33/5 (3 votes)
6 May 2010CPOL 10.8K  
Function Sqrt(ByVal value As BigInteger) As BigInteger Dim a As BigInteger = BigInteger.One Dim b As BigInteger = (value >> 5) + 8 While (b.CompareTo(a) >= 0) Dim m As BigInteger = BigInteger.Add(a, b) >> 1 If (BigInteger.Multiply(m,...
VB
Function Sqrt(ByVal value As BigInteger) As BigInteger

     Dim a As BigInteger = BigInteger.One
     Dim b As BigInteger = (value >> 5) + 8

     While (b.CompareTo(a) >= 0)

          Dim m As BigInteger = BigInteger.Add(a, b) >> 1

          If (BigInteger.Multiply(m, m).CompareTo(value) > 0) Then
               b = BigInteger.Subtract(m, BigInteger.One)
          Else
               a = BigInteger.Add(m, BigInteger.One)
          End If

     End While

     Return BigInteger.Subtract(a, BigInteger.One)

End Function
'//
'// example:
'//
Dim bI As BigInteger = New BigInteger

BigInteger.TryParse("265657159959580629138389106654927260761", bI)
bI = Sqrt(bI) '// bI={16298992605666787931}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)