6.2.2.1 Integer Objects

class Integer
The Integer class represents arbitrary precision integers. It derives from the Element class, so integers can be used as ring elements anywhere in SAGE.

Note: The class Integer is implemented in Pyrex, as a wrapper of the GMP mpz_t integer type.
Integer( )

You can create an integer from an int, long, string literal, or integer modulo N.

sage: Integer(495)
495
sage: Integer('495949209809328523')
495949209809328523
sage: Integer(Mod(3,7))
3

Integers also support the standard arithmetic operations, such as +,-,*,/, codeabs, codemod, codefloat

Instances of class Integer have the following methods (in addition to inherited methods and special methods):

binary,$  $ copy,$  $ crt,$  $ denominator,$  $ divides,$  $ factor,$  $ gcd,$  $ inverse_mod,$  $ is_one,$  $ is_prime,$  $ is_square,$  $ is_square_free,$  $ is_unit,$  $ is_zero,$  $ isqrt,$  $ lcm,$  $ numerator,$  $ parent,$  $ powermod,$  $ powermodm_ui,$  $ quo_rem,$  $ set_si,$  $ set_str,$  $ sqrt,$  $ str,$  $ xgcd

Further documentation:

binary( )

Return the binary digits of self as a string.

sage: Integer(15).binary()
'1111'
sage: Integer(16).binary()
'10000'
sage: Integer(16938402384092843092843098243).binary()
'11011010111011000111100011100100101001110100011010100011111110001010000000
00101111000010000011'

copy( )

Return a copy of the integer.

crt( )

Return the unique integer between 0 and $ mn$ that is congruent to the integer modulo $ m$ and to $ y$ modulo $ n$ . We assume that $ m$ and $ n$ are coprime.

denominator( )

Return the denominator of the integer.

divides( )

Return True if self divides n.

sage: Z = IntegerRing()
sage: Z(5).divides(Z(10))
True
sage: Z(0).divides(Z(5))
False
sage: Z(10).divides(Z(5))
False

factor( )

Return the prime factorization of the integer as a list of pairs $ (p,e)$ , where $ p$ is prime and $ e$ is a positive integer.

gcd( )

Return the greatest common divisor of self and $ n$ .

inverse_mod( )

Returns the inverse of self modulo $ n$ , if this inverse exists. Otherwise, raises a exceptionZeroDivisionError exception.

INPUT:
   self -- Integer
   n -- Integer
OUTPUT:
   x -- Integer such that x*self = 1 (mod m), or
        raises ZeroDivisionError.
IMPLEMENTATION:
   Call the mpz_invert GMP library function.

sage: a = Integer(189)
sage: a.inverse_mod(10000)
4709
sage: a.inverse_mod(-10000)
4709
sage: a.inverse_mod(1890)
Traceback (most recent call last):
...
ZeroDivisionError: Inverse does not exist.
sage: a = Integer(19)**100000
sage: b = a*a
sage: c=a.inverse_mod(b)
Traceback (most recent call last):
...
ZeroDivisionError: Inverse does not exist.

is_one( )

Returns True if the integers is $ 1$ , otherwise False.

is_square_free( )

Returns True if this integer is not divisible by the square of any prime and False otherwise.

is_unit( )

Returns true if this integer is a unit, i.e., 1 or $ -1$ .

is_zero( )

Returns True if the integers is 0 , otherwise False.

isqrt( )

Returns the integer floor of the square root of self, or raises an ValueError if self is negative.

sage: a = Integer(5)
sage: a.isqrt()
2

lcm( )

Returns the least common multiple of self and $ n$ .

numerator( )

Return the numerator of this integer.

parent( )

Return the ring $ \mathbf{Z}$ of integers.

powermod( )

powermod(self, Integer exp, Integer mod): Compute self**exp modulo mod.

set_si( )

Coerces $ n$ to a C signed integer if possible, and sets self equal to $ n$ .

set_str( )

Set self equal to the number defined by the string s in the given base.

sqrt( )

Returns the square root of self as a real number to the given number of bits of precision if self is nonnegative, and raises a ValueError exception otherwise.

sage: Z = IntegerRing()
sage: Z(2).sqrt()
1.4142135623730951
sage: Z(2).sqrt(100)
1.4142135623730950488016887242092

str( )

Return the string representation of self in the given base.

Note: String representation of integers with more than about four million digits is not available in bases other than a power of 2. This is because of a bug in GMP. To obtain the base-$ b$ expansion of an integer $ n$ use n.str(b).

xgcd( )

Return a triple $ g, s, t \in\mathbf{Z}$ such that

$\displaystyle g = s \cdot$   self$\displaystyle + t \cdot n.
$

Instances of class Integer also have the following special methods:

__abs__,$  $ __add,$  $ __add__,$  $ __and__,$  $ __cmp__,$  $ __div,$  $ __div__,$  $ __eq__,$  $ __float__,$  $ __floordiv,$  $ __floordiv__,$  $ __ge__,$  $ __gt__,$  $ __hex__,$  $ __int__,$  $ __invert__,$  $ __le__,$  $ __long__,$  $ __lshift__,$  $ __lt__,$  $ __mod__,$  $ __mul,$  $ __mul__,$  $ __ne__,$  $ __neg__,$  $ __or__,$  $ __pos__,$  $ __pow__,$  $ __radd__,$  $ __rand__,$  $ __rdiv__,$  $ __reduce__,$  $ __repr__,$  $ __rfloordiv__,$  $ __rlshift__,$  $ __rmod__,$  $ __rmul__,$  $ __ror__,$  $ __rpow__,$  $ __rrshift__,$  $ __rshift__,$  $ __rsub__,$  $ __str_malloc,$  $ __sub,$  $ __sub__,$  $ _and,$  $ _mpfr_,$  $ _or,$  $ _pari_

Further documentation:

__str_malloc( )

Return the string representation of self in the given base. (Uses malloc then PyMem. This is actually slightly faster than self.str() below, but it is unpythonic to use malloc.) However, self.str() below is nice because we know the size of the string ahead of time, and can work around a bug in GMP nicely. There seems to be a bug in GMP, where non-2-power base conversion for very large integers > 10 million digits (?) crashes GMP.

See About this document... for information on suggesting changes.