MIT licensed by Daniel Fischer, Andrew Lelechenko
Maintained by Andrew Lelechenko andrew dot lelechenko at gmail dot com
This version can be pinned in stack with:integer-roots-1.0@sha256:5ea7ecb80633fb05a8a36acd40e9da6fe22a6cef320b1310cb9f0330801ac124,2271

Module documentation for 1.0

Depends on 2 packages(full list with versions):
Used by 1 package in lts-16.23(full list with versions):

integer-roots Build Status Hackage Hackage CI Stackage LTS Stackage Nightly

Calculating integer roots and testing perfect powers of arbitrary precision.

Integer square root

The integer square root (integerSquareRoot) of a non-negative integer n is the greatest integer m such that . Alternatively, in terms of the floor function, .

For example,

> integerSquareRoot 99
> integerSquareRoot 101

It is tempting to implement integerSquareRoot via sqrt :: Double -> Double:

integerSquareRoot :: Integer -> Integer
integerSquareRoot = truncate . sqrt . fromInteger

However, this implementation is faulty:

> integerSquareRoot (3037000502^2)
> isqrt (2^1024) == 2^1024

The problem here is that Double can represent only a limited subset of integers without precision loss. Once we encounter larger integers, we lose precision and obtain all kinds of wrong results.

This library features a polymorphic, efficient and robust routine integerSquareRoot :: Integral a => a -> a, which computes integer square roots by Karatsuba square root algorithm without intermediate Doubles.

Integer cube roots

The integer cube root (integerCubeRoot) of an integer n equals to .

Again, a naive approach is to implement integerCubeRoot via Double-typed computations:

integerCubeRoot :: Integer -> Integer
integerCubeRoot = truncate . (** (1/3)) . fromInteger

Here the precision loss is even worse than for integerSquareRoot:

> integerCubeRoot (4^3)
> integerCubeRoot (5^3)

That is why we provide a robust implementation of integerCubeRoot :: Integral a => a -> a, which computes roots by generalized Heron algorithm.

Higher powers

In spirit of integerSquareRoot and integerCubeRoot this library covers the general case as well, providing integerRoot :: (Integral a, Integral b) => b -> a -> a to compute integer k-th roots of arbitrary precision.

There is also highestPower routine, which tries hard to represent its input as a power with as large exponent as possible. This is a useful function in number theory, e. g., elliptic curve factorisation.

> map highestPower [2..10]



Initial release.