# Integers

The default integer type in Nemo is provided by Flint. The associated ring of integers is represented by the constant parent object called FlintZZ.

For convenience we define

ZZ = FlintZZ

so that integers can be constructed using ZZ instead of FlintZZ. Note that this is the name of a specific parent object, not the name of its type.

The types of the integer ring parent objects and elements of the associated rings of integers are given in the following table according to the library provding them.

LibraryElement typeParent type
FlintfmpzFlintIntegerRing

All integer element types belong directly to the abstract type RingElem and all the integer ring parent object types belong to the abstract type Ring.

A lot of code will want to accept both fmpz integers and Julia integers, that is, subtypes of Base.Integer. Thus for convenience we define

IntegerUnion = Union{Integer,fmpz}

## Integer functionality

Nemo integers provide all of the ring and Euclidean ring functionality of AbstractAlgebra.jl.

https://nemocas.github.io/AbstractAlgebra.jl/stable/ring

https://nemocas.github.io/AbstractAlgebra.jl/stable/euclidean_interface

Below, we describe the functionality that is specific to the Nemo/Flint integer ring.

### Constructors

ZZ(n::Integer)

Coerce a Julia integer value into the integer ring.

ZZ(n::String)

Parse the given string as an integer.

ZZ(n::Float64)
ZZ(n::Float32)
ZZ(n::Float16)
ZZ(n::BigFloat)

Coerce the given floating point number into the integer ring, assuming that it can be exactly represented as an integer.

### Basic manipulation

Base.sizeMethod
size(a::fmpz)

Return the number of limbs required to store the absolute value of $a$.

source
Nemo.fitsMethod
fits(::Type{UInt}, a::fmpz)

Return true if $a$ fits into a UInt, otherwise return false.

source
Nemo.fitsMethod
fits(::Type{Int}, a::fmpz)

Return true if $a$ fits into an Int, otherwise return false.

source

Examples

a = ZZ(12)

is_unit(a)
sign(a)
s = size(a)
fits(Int, a)
n = numerator(a)
d = denominator(a)

### Euclidean division

Nemo also provides a large number of Euclidean division operations. Recall that for a dividend $a$ and divisor $b$, we can write $a = bq + r$ with $0 \leq |r| < |b|$. We call $q$ the quotient and $r$ the remainder.

We distinguish three cases. If $q$ is rounded towards zero, $r$ will have the same sign as $a$. If $q$ is rounded towards plus infinity, $r$ will have the opposite sign to $b$. Finally, if $q$ is rounded towards minus infinity, $r$ will have the same sign as $b$.

In the following table we list the division functions and their rounding behaviour. We also give the return value of the function, with $q$ representing return of the quotient and $r$ representing return of the remainder.

FunctionReturnRounding of the quotient
modrtowards minus infinity
remrtowards zero
divqtowards minus infinity
divrem(a::fmpz, b::fmpz)q, rtowards minus infinity
tdivrem(a::fmpz, b::fmpz)q, rtowards zero
fdivrem(a::fmpz, b::fmpz)q, rtowards minus infinity
cdivrem(a::fmpz, b::fmpz)q, rtowards plus infinity
ntdivrem(a::fmpz, b::fmpz)q, rnearest integer, ties toward zero
nfdivrem(a::fmpz, b::fmpz)q, rnearest integer, ties toward minus infinity
ncdivrem(a::fmpz, b::fmpz)q, rnearest integer, ties toward plus infinity

N.B: the internal definition of Nemo.div and Nemo.divrem are the same as fdiv and fdivrem. The definitions in the table are of Base.div and Base.divrem which agree with Julia's definitions of div and divrem.

Nemo also offers the following ad hoc division operators. The notation and description is as for the other Euclidean division functions.

FunctionReturnRounding
mod(a::fmpz, b::Int)rtowards minus infinity
rem(a::fmpz, b::Int)rtowards zero
div(a::fmpz, b::Int)qtowards zero
tdiv(a::fmpz, b::Int)qtowards zero
fdiv(a::fmpz, b::Int)qtowards minus infinity
cdiv(a::fmpz, b::Int)qtowards plus infinity

N.B: the internal definition of Nemo.div is the same as fdiv. The definition in the table is Base.div which agrees with Julia's definition of div.

The following functions are also available, for the case where one is dividing by a power of $2$. In other words, for Euclidean division of the form $a = b2^{d} + r$. These are useful for bit twiddling.

FunctionReturnRounding
tdivpow2(a::fmpz, d::Int)qtowards zero
fdivpow2(a::fmpz, d::Int)qtowards minus infinity
fmodpow2(a::fmpz, d::Int)rtowards minus infinity
cdivpow2(a::fmpz, d::Int)qtowards plus infinity

Examples

a = fmpz(12)
b = fmpz(5)

q, r = divrem(a, b)
c = cdiv(a, b)
d = fdiv(a, b)
f = tdivpow2(a, 2)
g = fmodpow2(a, 3)

### Comparison

Instead of isless we implement a function cmp(a, b) which returns a positive value if $a > b$, zero if $a == b$ and a negative value if $a < b$. We then implement all the other operators, including == in terms of cmp.

For convenience we also implement a cmpabs(a, b) function which returns a positive value if $|a| > |b|$, zero if $|a| == |b|$ and a negative value if $|a| < |b|$. This can be slightly faster than a call to cmp or one of the comparison operators when comparing nonnegative values for example.

Here is a list of the comparison functions implemented, with the understanding that cmp provides all of the comparison operators listed above.

Function
cmp(a::fmpz, b::fmpz)
cmpabs(a::fmpz, b::fmpz)

We also provide the following ad hoc comparisons which again provide all of the comparison operators mentioned above.

Function
cmp(a::fmpz, b::Int)
cmp(a::Int, b::fmpz)
cmp(a::fmpz, b::UInt)
cmp(a::UInt, b::fmpz)

Examples

a = ZZ(12)
b = ZZ(3)

a < b
a != b
a > 4
5 <= b
cmpabs(a, b)

### Shifting

Base.:>>Method
>>(x::fmpz, c::Int)

Return $x/2^c$, discarding any remainder, where $c \geq 0$.

source

Examples

a = fmpz(12)

a << 3
a >> 5

### Modular arithmetic

Nemo.sqrtmodMethod
sqrtmod(x::fmpz, m::fmpz)

Return a square root of $x (\mod m)$ if one exists. The remainder will be in the range $[0, m)$. We require that $m$ is prime, otherwise the algorithm may not terminate.

source
AbstractAlgebra.crtMethod
crt(r1::fmpz, m1::fmpz, r2::fmpz, m2::fmpz, signed=false)

Return $r$ such that $r \equiv r_1 (\mod m_1)$ and $r \equiv r_2 (\mod m_2)$. If signed = true, $r$ will be in the range $-m_1m_2/2 < r \leq m_1m_2/2$. If signed = false the value will be in the range $0 \leq r < m_1m_2$.

source
AbstractAlgebra.crtMethod
crt(r1::fmpz, m1::fmpz, r2::Int, m2::Int, signed=false)

Return $r$ such that $r \equiv r_1 (\mod m_1)$ and $r \equiv r_2 (\mod m_2)$. If signed = true, $r$ will be in the range $-m_1m_2/2 < r \leq m_1m_2/2$. If signed = false the value will be in the range $0 \leq r < m_1m_2$.

source

Examples

c = sqrtmod(ZZ(12), ZZ(13))
d = crt(ZZ(5), ZZ(13), ZZ(7), ZZ(37), true)

### Integer logarithm

Nemo.clogMethod
clog(x::fmpz, c::fmpz)

Return the ceiling of the logarithm of $x$ to base $c$.

source

Examples

a = fmpz(12)
b = fmpz(2)

c = flog(a, b)
d = clog(a, 3)

### Integer roots

Nemo.isqrtremMethod
isqrtrem(x::fmpz)

Return a tuple $s, r$ consisting of the floor $s$ of the square root of $x$ and the remainder $r$, i.e. such that $x = s^2 + r$. We require $x \geq 0$.

source
AbstractAlgebra.rootMethod
root(x::fmpz, n::Int; check::Bool=true)

Return the $n$-the root of $x$. We require $n > 0$ and that $x \geq 0$ if $n$ is even. By default the function tests whether the input was a perfect $n$-th power and if not raises an exception. If check=false this check is omitted.

source
AbstractAlgebra.irootMethod
iroot(x::fmpz, n::Int)

Return the integer truncation of the $n$-the root of $x$ (round towards zero). We require $n > 0$ and that $x \geq 0$ if $n$ is even.

source

Examples

a = ZZ(13)
b = ZZ(27)

c = isqrt(a)
s, r = isqrtrem(a)
d = iroot(a, 3)
k = root(b, 3; check=true)

### Number theoretic functionality

Nemo.divisibleMethod
divisible(x::fmpz, y::Int)

Return true if $x$ is divisible by $y$, otherwise return false. We require $x \neq 0$.

source
Nemo.divisibleMethod
divisible(x::fmpz, y::fmpz)

Return true if $x$ is divisible by $y$, otherwise return false. We require $x \neq 0$.

source
AbstractAlgebra.is_squareMethod
is_square(f::PolyElem{T}) where T <: RingElement

Return true if $f$ is a perfect square.

is_square(a::FracElem{T}) where T <: RingElem

Return true if $a$ is a square.

Nemo.is_primeMethod
is_prime(x::fmpz)

Return true if $x$ is a prime number, otherwise return false.

source
AbstractAlgebra.is_probable_primeMethod
is_probable_prime(x::fmpz)

Return true if $x$ is very probably a prime number, otherwise return false. No counterexamples are known to this test, but it is conjectured that infinitely many exist.

source
AbstractAlgebra.factorMethod
factor(a::fmpz)
factor(a::UInt)
factor(a::Int)

Return a factorisation of $a$ using a Fac struct (see the documentation on factorisation in Nemo).

source
Nemo.divisor_lenstraMethod
divisor_lenstra(n::fmpz, r::fmpz, m::fmpz)

If $n$ has a factor which lies in the residue class $r (\mod m)$ for $0 < r < m < n$, this function returns such a factor. Otherwise it returns $0$. This is only efficient if $m$ is at least the cube root of $n$. We require gcd$(r, m) = 1$ and this condition is not checked.

source
Nemo.rising_factorialMethod
rising_factorial(x::fmpz, n::fmpz)

Return the rising factorial of $x$, i.e. $x(x + 1)(x + 2)\cdots (x + n - 1)$. If $n < 0$ we throw a DomainError().

source
Nemo.rising_factorialMethod
rising_factorial(x::fmpz, n::Int)

Return the rising factorial of $x$, i.e. $x(x + 1)(x + 2)\ldots (x + n - 1)$. If $n < 0$ we throw a DomainError().

source
Nemo.rising_factorialMethod
rising_factorial(x::Int, n::Int)

Return the rising factorial of $x$, i.e. $x(x + 1)(x + 2)\ldots (x + n - 1)$. If $n < 0$ we throw a DomainError().

source
Nemo.primorialMethod
primorial(x::fmpz)

Return the primorial of $x$, i.e. the product of all primes less than or equal to $x$. If $x < 0$ we throw a DomainError().

source
Nemo.primorialMethod
primorial(x::Int)

Return the primorial of $x$, i.e. the product of all primes less than or equal to $x$. If $x < 0$ we throw a DomainError().

source
Nemo.fibonacciMethod
fibonacci(x::Int)

Return the $x$-th Fibonacci number $F_x$. We define $F_1 = 1$, $F_2 = 1$ and $F_{i + 1} = F_i + F_{i - 1}$ for all integers $i$.

source
Nemo.fibonacciMethod
fibonacci(x::fmpz)

Return the $x$-th Fibonacci number $F_x$. We define $F_1 = 1$, $F_2 = 1$ and $F_{i + 1} = F_i + F_{i - 1}$ for all integers $i$.

source
Base.binomialMethod
binomial(n::fmpz, k::fmpz)

Return the binomial coefficient $\frac{n (n-1) \cdots (n-k+1)}{k!}$. If $k < 0$ we return $0$, and the identity binomial(n, k) == binomial(n - 1, k - 1) + binomial(n - 1, k) always holds for integers n and k.

source
Base.binomialMethod
binomial(n::UInt, k::UInt, ::FlintIntegerRing)

Return the binomial coefficient $\frac{n!}{(n - k)!k!}$ as an fmpz.

source
Nemo.moebius_muMethod
moebius_mu(x::Int)

Return the Moebius mu function of $x$ as an Int. The value returned is either $-1$, $0$ or $1$. If $x \leq 0$ we throw a DomainError().

source
Nemo.moebius_muMethod
moebius_mu(x::fmpz)

Return the Moebius mu function of $x$ as an Int. The value returned is either $-1$, $0$ or $1$. If $x \leq 0$ we throw a DomainError().

source
Nemo.jacobi_symbolMethod
jacobi_symbol(x::Int, y::Int)

Return the value of the Jacobi symbol $\left(\frac{x}{y}\right)$. The modulus $y$ must be odd and positive, otherwise a DomainError is thrown.

source
Nemo.jacobi_symbolMethod
jacobi_symbol(x::fmpz, y::fmpz)

Return the value of the Jacobi symbol $\left(\frac{x}{y}\right)$. The modulus $y$ must be odd and positive, otherwise a DomainError is thrown.

source
Nemo.kronecker_symbolMethod
kronecker_symbol(x::fmpz, y::fmpz)
kronecker_symbol(x::Int, y::Int)

Return the value of the Kronecker symbol $\left(\frac{x}{y}\right)$. The definition is as per Henri Cohen's book, "A Course in Computational Algebraic Number Theory", Definition 1.4.8.

source
Nemo.divisor_sigmaMethod
divisor_sigma(x::Int, y::Int)

Return the value of the sigma function, i.e. $\sum_{0 < d \;| x} d^y$. If $x \leq 0$ or $y < 0$ we throw a DomainError().

source
Nemo.divisor_sigmaMethod
divisor_sigma(x::fmpz, y::Int)

Return the value of the sigma function, i.e. $\sum_{0 < d \;| x} d^y$. If $x \leq 0$ or $y < 0$ we throw a DomainError().

source
Nemo.divisor_sigmaMethod
divisor_sigma(x::fmpz, y::fmpz)

Return the value of the sigma function, i.e. $\sum_{0 < d \;| x} d^y$. If $x \leq 0$ or $y < 0$ we throw a DomainError().

source
Nemo.euler_phiMethod
euler_phi(x::Int)

Return the value of the Euler phi function at $x$, i.e. the number of positive integers up to $x$ (inclusive) that are coprime with $x$. An exception is raised if $x \leq 0$.

source
Nemo.euler_phiMethod
euler_phi(x::fmpz)

Return the value of the Euler phi function at $x$, i.e. the number of positive integers up to $x$ (inclusive) that are coprime with $x$. An exception is raised if $x \leq 0$.

source

Examples

is_prime(ZZ(13))
n = factorial(ZZ(100))
s = divisor_sigma(ZZ(128), 10)
a = euler_phi(ZZ(12480))
p = number_of_partitions(ZZ(1000))
f = factor(ZZ(12))

### Digits and bases

Nemo.baseMethod
base(n::fmpz, b::Integer)

Return $n$ as a string in base $b$. We require $2 \leq b \leq 62$.

source
Base.ndigitsMethod
ndigits(x::fmpz, b::Integer)

Return the number of digits of $x$ in the base $b$ (default is $b = 10$).

source
Nemo.nbitsMethod
nbits(x::fmpz)

Return the number of binary bits of $x$. We return zero if $x = 0$.

source

Examples

a = fmpz(12)

s1 = bin(a)
s2 = base(a, 13)
n1 = nbits(a)
n2 = ndigits(a, 3)

### Bit twiddling

Nemo.clrbit!Method
clrbit!(x::fmpz, c::Int)

Clear bit $c$ of $x$, where the least significant bit is the $0$-th bit. Note that this function modifies its input in-place.

source
Nemo.setbit!Method
setbit!(x::fmpz, c::Int)

Set bit $c$ of $x$, where the least significant bit is the $0$-th bit. Note that this function modifies its input in-place.

source
Nemo.combit!Method
combit!(x::fmpz, c::Int)

Complement bit $c$ of $x$, where the least significant bit is the $0$-th bit. Note that this function modifies its input in-place.

source
Nemo.tstbitMethod
tstbit(x::fmpz, c::Int)

Return bit $i$ of x (numbered from 0) as true for 1 or false for 0.

source

Examples

a = fmpz(12)

p = popcount(a)
b = nextpow2(a)
combit!(a, 2)

### Random generation

Nemo.rand_bitsMethod
rand_bits(::FlintIntegerRing, b::Int)

Return a random signed integer whose absolute value has $b$ bits.

source
Nemo.rand_bits_primeMethod
rand_bits_prime(::FlintIntegerRing, n::Int, proved::Bool=true)

Return a random prime number with the given number of bits. If only a probable prime is required, one can pass proved=false.

source

Examples

a = rand_bits(ZZ, 23)
b = rand_bits_prime(ZZ, 7)

# Complex Integers

The Gaussian integer type in Nemo is provided by a pair of Flint integers. The associated ring of integers is represented by the constant parent object called FlintZZi or ZZi, and the fraction field is called FlintQQi or QQi.

Examples

a = ZZ(5)*im
b = ZZi(3, 4)

is_unit(a)
factor(a)
a//b
abs2(a//b)