65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.33/5 (3 votes)

May 6, 2010

CPOL
viewsIcon

10796

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

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}