ac-library-hs-1.2.6.0: Data structures and algorithms
Safe HaskellSafe-Inferred
LanguageGHC2021

AtCoder.Extra.Math

Description

Extra math module.

Since: 1.0.0.0

Synopsis

Re-exports from the internal math module

isPrime32 :: HasCallStack => Int -> Bool Source #

\(O(k \log^3 n) (k = 3)\). Returns whether the given Int value is a prime number.

Constraints

  • \(n < 4759123141 (2^{32} < 4759123141)\), otherwise the return value can lie (see Wikipedia).

Since: 1.1.0.0

invGcd :: Int -> Int -> (Int, Int) Source #

Returns \((g, x)\) such that \(g = \gcd(a, b), \mathrm{xa} \equiv g \pmod b, 0 \le x \le b/g\).

Constraints

  • \(1 \le b\) (not asserted)

Since: 1.0.0.0

primitiveRoot32 :: HasCallStack => Int -> Int Source #

Returns the primitive root of the given Int.

Constraints

  • The input must be a prime number.
  • The input must be less than \(2^32\).

Since: 1.2.0.0

Prime numbers and divisors

primes :: Int -> Vector Int Source #

\(O(n \log \log n)\) Creates an array of prime numbers up to the given limit, using Sieve of Eratosthenes.

The minimum computational complexity is \(\Omega(B \log \log B)\), where \(B = 2^{15}\) is the length of segment. This constraint comes from the use of segmented sieve.

Constraints

  • The upper limit must be less than or equal to \(2^{30} (\gt 10^9)\), otherwise the returned prime table is incorrect.

Since: 1.2.6.0

isPrime :: Int -> Bool Source #

\(O(w \log^3 n)\) Miller–Rabin primality test, where \(w = 3\) for \(x \lt 2^{32}\) and \(w = 7\) for \(x \ge 3^{32}\).

Since: 1.2.6.0

primeFactors :: HasCallStack => Int -> Vector (Int, Int) Source #

Returns prime factors in run-length encoding \((p_i, n_i)\), sorted by \(p_i\).

Constraints

  • \(x \ge 1\)

Since: 1.2.6.0

primeFactorsUnsorted :: HasCallStack => Int -> Vector (Int, Int) Source #

Returns prime factors in run-length encoding \((p_i, n_i)\) in arbitrary order.

It internally uses probabilistic method (Pollard's rho algorithm) and it can actually result in runtime error, however, the probability is very low and the API does not return Maybe.

Since: 1.2.6.0

divisors :: Int -> Vector Int Source #

Enumerates divisors of the input value.

Constraints

  • \(x \ge 1\)

Since: 1.2.6.0

divisorsUnsorted :: Int -> Vector Int Source #

Enumerates divisors of the input value.

Constraints

  • \(x \ge 1\)

Since: 1.2.6.0

Binary exponentiation

Examples

Expand
>>> import AtCoder.Extra.Math qualified as M
>>> import Data.Semigroup (Product(..), Sum(..))
>>> getProduct $ M.power (<>) 32 (Product 2)
4294967296
>>> getProduct $ M.stimes' 32 (Product 2)
4294967296
>>> getProduct $ M.mtimes' 32 (Product 2)
4294967296

power :: (a -> a -> a) -> Int -> a -> a Source #

Calculates \(x^n\) with custom multiplication operator using the binary exponentiation technique.

The internal implementation is taken from Data.Semigroup.stimes, but power uses strict evaluation and is often much faster.

Complexity

  • \(O(\log n)\)

Constraints

  • \(n \gt 0\)

Since: 1.0.0.0

stimes' :: Semigroup a => Int -> a -> a Source #

Strict variant of Data.Semigroup.stimes.

Complexity

  • \(O(\log n)\)

Constraints

  • \(n \gt 0\)

Since: 1.0.0.0

mtimes' :: Monoid a => Int -> a -> a Source #

Strict variant of Data.Monoid.mtimes.

Complexity

  • \(O(\log n)\)

Constraints

  • \(n \ge 0\)

Since: 1.0.0.0