سؤال

Say I have a large integral number of the type Integer. Does there exist a library function (in Prelude or elsewhere) which, when given an Integer X will return the integral portion of the square root of X as an Integer?

هل كانت مفيدة؟

المحلول 2

The arithmoi package has an Integer square root function called integerSquareRoot that works for any Integral instance.

It also has specialization rules so that you can use it on plain Ints and have the root computed via the sqrt function on Doubles.

نصائح أخرى

You can go via CReal.

isqrt :: Integer -> Integer
isqrt = floor . (sqrt :: CReal -> CReal) . fromInteger
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top