Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest method implementing number sqare root in string (1000000 digits)

Tags:

algorithm

What is fastest algorithm implementing a square root of decimal contained in strings. This decimal can have 1000000 digits.

Anyone can tell me something about it?

like image 850
Svisstack Avatar asked Nov 06 '22 14:11

Svisstack


2 Answers

Newton's method should work fine for you: Square Root for Bigint in F# .

Newton's method requires big decimal division. A somewhat simpler method which requires only squaring is just binary search on the square root.

like image 199
Keith Randall Avatar answered Nov 15 '22 07:11

Keith Randall


Use 'lsqrt' (Just google for some code) and adjust it for your number type. I used the same approach to deal with big numbers in IronScheme.

Seems to work well.

Edit:

This returns an 'integer' root and a remainder.

like image 32
leppie Avatar answered Nov 15 '22 07:11

leppie