23.2 Elements of the ring $ \mathbf{Z}$ of integers

Module: sage.rings.integer

File: sage/rings/integer.pyx (starting at line 1)

Elements of the ring $ \mathbf{Z}$ of integers

Author Log:

Add 2 integers:

sage: a = Integer(3) ; b = Integer(4)
sage: a + b == 7
True

Add an integer and a real number:

sage: a + 4.0
7.00000000000000

Add an integer and a rational number:

sage: a + Rational(2)/5
17/5

Add an integer and a complex number:

sage: b = ComplexField().0 + 1.5
sage: loads((a+b).dumps()) == a+b
True

sage: z = 32
sage: -z
-32
sage: z = 0; -z
0
sage: z = -0; -z
0
sage: z = -1; -z
1

Multiplication:

sage: a = Integer(3) ; b = Integer(4)
sage: a * b == 12
True
sage: loads((a * 4.0).dumps()) == a*b
True
sage: a * Rational(2)/5
6/5

sage: list([2,3]) * 4
[2, 3, 2, 3, 2, 3, 2, 3]

sage: 'sage'*Integer(3)
'sagesagesage'

COERCIONS: Returns version of this integer in the multi-precision floating real field R.

sage: n = 9390823
sage: RR = RealField(200)
sage: RR(n)
9390823.0000000000000000000000000000000000000000000000000000

Module-level Functions

GCD_list( )

Return the GCD of a list v of integers. Element of v is converted to a SAGE integer if it isn't one already.

This function is used, e.g., by rings/arith.py

INPUT:
    v -- list or tuple

OUTPUT:
    integer

sage: from sage.rings.integer import GCD_list
sage: w = GCD_list([3,9,30]); w
3
sage: type(w)
<type 'sage.rings.integer.Integer'>

The inputs are converted to SAGE integers.

sage: w = GCD_list([int(3), int(9), '30']); w
3
sage: type(w)
<type 'sage.rings.integer.Integer'>

LCM_list( )

Return the LCM of a list v of integers. Element of v is converted to a SAGE integer if it isn't one already.

This function is used, e.g., by rings/arith.py

INPUT:
    v -- list or tuple

OUTPUT:
    integer

sage: from sage.rings.integer import LCM_list
sage: w = LCM_list([3,9,30]); w
90
sage: type(w)
<type 'sage.rings.integer.Integer'>

The inputs are converted to SAGE integers.

sage: w = LCM_list([int(3), int(9), '30']); w
90
sage: type(w)
<type 'sage.rings.integer.Integer'>

clear_mpz_globals( )

free_integer_pool( )

gmp_randrange( )

init_mpz_globals( )

is_Integer( )

Return true if x is of the SAGE integer type.

sage: is_Integer(2)
True
sage: is_Integer(2/1)
False
sage: is_Integer(int(2))
False
sage: is_Integer(long(2))
False
sage: is_Integer('5')
False

make_integer( )

Create a SAGE integer from the base-32 Python *string* s. This is used in unpickling integers.

sage: from sage.rings.integer import make_integer
sage: make_integer('-29')
-73
sage: make_integer(29)
Traceback (most recent call last):
...
TypeError: expected string or Unicode object, sage.rings.integer.Integer
found

pool_stats( )

Returns information about the Integer object pool.

sage: from sage.rings.integer import pool_stats
sage: pool_stats()            # random-ish output
Used pool 0 / 0 times
Pool contains 3 / 100 items

time_alloc( )

Time allocating n integers using PY_NEW. Used for timing purposes.

sage: from sage.rings.integer import time_alloc
sage: time_alloc(100)

time_alloc_list( )

Allocate n a list of n SAGE integers using PY_NEW. (Used for timing purposes.)

sage: from sage.rings.integer import time_alloc_list
sage: v = time_alloc_list(100)

Class: int_to_Z

class int_to_Z

Special Functions: _repr_type

_repr_type( )

Class: Integer

class Integer
File: sage/rings/integer.pyx (starting at line 183)

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
sage: 2^3
8

Functions: additive_order,$  $ binary,$  $ bits,$  $ ceil,$  $ coprime_integers,$  $ crt,$  $ denominator,$  $ digits,$  $ div,$  $ divide_knowing_divisible_by,$  $ divides,$  $ exact_log,$  $ factor,$  $ factorial,$  $ floor,$  $ gcd,$  $ inverse_mod,$  $ inverse_of_unit,$  $ is_integral,$  $ is_one,$  $ is_perfect_power,$  $ is_power,$  $ is_power_of,$  $ is_prime,$  $ is_prime_power,$  $ is_pseudoprime,$  $ is_square,$  $ is_squarefree,$  $ is_unit,$  $ isqrt,$  $ jacobi,$  $ kronecker,$  $ list,$  $ multiplicative_order,$  $ next_prime,$  $ next_probable_prime,$  $ nth_root,$  $ numerator,$  $ ord,$  $ powermod,$  $ powermodm_ui,$  $ quo_rem,$  $ radical,$  $ rational_reconstruction,$  $ set_si,$  $ set_str,$  $ sqrt,$  $ sqrt_approx,$  $ squarefree_part,$  $ str,$  $ val_unit,$  $ valuation

additive_order( )

Return the additive order of self.

sage: ZZ(0).additive_order()
1
sage: ZZ(1).additive_order()
+Infinity

binary( )

Return the binary digits of self as a string.

sage: print Integer(15).binary()
1111
sage: print Integer(16).binary()
10000
sage: print Integer(16938402384092843092843098243).binary()
110110101110110001111000111001001010011101000110101000111111100010100000000
0101111000010000011

bits( )

Return the number of bits in self.

sage: 500.bits()
9
sage: 5.bits()
3
sage: 12345.bits() == len(12345.binary())
True

ceil( )

Return the ceiling of self, which is self since self is an integer.

sage: n = 6
sage: n.ceil()
6

coprime_integers( )

Return the positive integers $ < m$ that are coprime to self.

sage: n = 8
sage: n.coprime_integers(8)
[1, 3, 5, 7]
sage: n.coprime_integers(11)
[1, 3, 5, 7, 9]
sage: n = 5; n.coprime_integers(10)
[1, 2, 3, 4, 6, 7, 8, 9]
sage: n.coprime_integers(5)
[1, 2, 3, 4]
sage: n = 99; n.coprime_integers(99)
[1, 2, 4, 5, 7, 8, 10, 13, 14, 16, 17, 19, 20, 23, 25, 26, 28, 29, 31, 32,
34, 35, 37, 38, 40, 41, 43, 46, 47, 49, 50, 52, 53, 56, 58, 59, 61, 62, 64,
65, 67, 68, 70, 71, 73, 74, 76, 79, 80, 82, 83, 85, 86, 89, 91, 92, 94, 95,
97, 98]

Author: Naqi Jaffery (2006-01-24): examples

ALGORITHM: Naive - compute lots of GCD's. If this isn't good enough for you, please code something better and submit a patch.

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.

sage: n = 17
sage: m = n.crt(5, 23, 11); m
247
sage: m%23
17
sage: m%11
5

denominator( )

Return the denominator of this integer.

sage: x = 5
sage: x.denominator()
1
sage: x = 0
sage: x.denominator()
1

digits( )

Return a list of digits for self in the given base in little endian order.

INPUT:
    base -- integer (default: 2)
    digits -- optional indexable object as source for the digits

sage: 5.digits()
[1, 0, 1]
sage: 5.digits(3)
[2, 1]
sage: 10.digits(16,'0123456789abcdef')
['a']

div( )

Returns the quotient of self divided by other.

INPUT:
    other -- the integer the divisor

OUTPUT:
    q   -- the quotient of self/other

sage: z = Integer(231)
sage: z.div(2)
115
sage: z.div(-2)
-115
sage: z.div(0)
Traceback (most recent call last):
...
ZeroDivisionError: other (=0) must be nonzero

divide_knowing_divisible_by( )

Returns the integer self / right when self is divisible by right.

If self is not divisible by right, the return value is undefined, and may not even be close to self/right for multi-word integers.

sage: a = 8; b = 4
sage: a.divide_knowing_divisible_by(b)
2
sage: (100000).divide_knowing_divisible_by(25)
4000
sage: (100000).divide_knowing_divisible_by(26) # close (random)
3846

However, often it's way off.

sage: a = 2^70; a
1180591620717411303424
sage: a // 11  # floor divide
107326510974310118493
sage: a.divide_knowing_divisible_by(11) # way off and possibly random
43215361478743422388970455040

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

exact_log( )

Returns the largest integer $ k$ such that $ m^k \leq$   self, i.e., the floor of $ \log_m($self$ )$.

This is guaranteed to return the correct answer even when the usual log function doesn't have sufficient precision.

INPUT:
    m -- integer >= 2

Author: David Harvey (2006-09-15)

TODO: - Currently this is extremely stupid code (although it should always work). Someone needs to think about doing this properly by estimating errors in the log function etc.

sage: Integer(125).exact_log(5)
3
sage: Integer(124).exact_log(5)
2
sage: Integer(126).exact_log(5)
3
sage: Integer(3).exact_log(5)
0
sage: Integer(1).exact_log(5)
0

sage: x = 3^100000
sage: RR(log(RR(x), 3))
100000.000000000
sage: RR(log(RR(x + 100000), 3))
100000.000000000

sage: x.exact_log(3)
100000
sage: (x+1).exact_log(3)
100000
sage: (x-1).exact_log(3)
99999

sage: x.exact_log(2.5)
Traceback (most recent call last):
...
ValueError: base of log must be an integer

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.

INPUT:
    algorithm -- string
         * 'pari' -- (default)  use the PARI c library
         * 'kash' -- use KASH computer algebra system (requires
                     the optional kash package be installed)
    proof -- bool (default: True) whether or not to prove primality
            of each factor (only applicable for PARI).

sage: n = 2^100 - 1; n.factor()
3 * 5^3 * 11 * 31 * 41 * 101 * 251 * 601 * 1801 * 4051 * 8101 * 268501

factorial( )

Return the factorial $ n!=1 \cdot 2 \cdot 3 \cdots n$. Self must fit in an unsigned long int.

sage: for n in srange(7):
...    print n, n.factorial()
0 1
1 1
2 2
3 6
4 24
5 120
6 720

floor( )

Return the floor of self, which is just self since self is an integer.

sage: n = 6
sage: n.floor()
6

gcd( )

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

sage: gcd(-1,1)
1
sage: gcd(0,1)
1
sage: gcd(0,0)
0
sage: gcd(2,2^6)
2
sage: gcd(21,2^6)
1

inverse_mod( )

Returns the inverse of self modulo $ n$, if this inverse exists. Otherwise, raises a ZeroDivisionError 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.

inverse_of_unit( )

Return inverse of self if self is a unit in the integers, i.e., self is -1 or 1. Otherwise, raise a ZeroDivisionError.

sage: (1).inverse_of_unit()
1
sage: (-1).inverse_of_unit()
-1
sage: 5.inverse_of_unit()
Traceback (most recent call last):
...
ZeroDivisionError: Inverse does not exist.
sage: 0.inverse_of_unit()
Traceback (most recent call last):
...
ZeroDivisionError: Inverse does not exist.

is_integral( )

Return True since integers are integral, i.e., satisfy a monic polynomial with integer coefficients.

sage: Integer(3).is_integral()
True

is_one( )

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

sage: Integer(1).is_one()
True
sage: Integer(0).is_one()
False

is_perfect_power( )

Retuns True if self is a perfect power.

sage: z = 8
sage: z.is_perfect_power()
True
sage: z = 144
sage: z.is_perfect_power()
True
sage: z = 10
sage: z.is_perfect_power()
False

is_power( )

Returns True if self is a perfect power, ie if there exist integers a and b, $ b > 1$ with $ self = a^b$.

sage: Integer(-27).is_power()
True
sage: Integer(12).is_power()
False

is_power_of( )

Returns True if there is an integer b with $ self = n^b$.

sage: Integer(64).is_power_of(4)
True
sage: Integer(64).is_power_of(16)
False

TESTS:

sage: Integer(-64).is_power_of(-4)
True
sage: Integer(-32).is_power_of(-2)
True
sage: Integer(1).is_power_of(1)
True
sage: Integer(-1).is_power_of(-1)
True
sage: Integer(0).is_power_of(1)
False
sage: Integer(0).is_power_of(0)
True
sage: Integer(1).is_power_of(0)
True
sage: Integer(1).is_power_of(8)
True
sage: Integer(-8).is_power_of(2)
False

NOTES:

For large integers self, is_power_of() is faster than is_power(). The following examples gives some indication of how much faster.

sage: b = lcm(range(1,10000))
sage: b.exact_log(2)
14446
sage: t=cputime()
sage: for a in range(2, 1000): k = b.is_power()
sage: cputime(t)      # random
0.53203299999999976 
sage: t=cputime()
sage: for a in range(2, 1000): k = b.is_power_of(2)
sage: cputime(t)      # random
0.0
sage: t=cputime()
sage: for a in range(2, 1000): k = b.is_power_of(3)
sage: cputime(t)      # random
0.032002000000000308

sage: b = lcm(range(1, 1000))
sage: b.exact_log(2)
1437
sage: t=cputime()
sage: for a in range(2, 10000): k = b.is_power() # note that we change the range from the example above
sage: cputime(t)      # random
0.17201100000000036 
sage: t=cputime(); TWO=int(2)
sage: for a in range(2, 10000): k = b.is_power_of(TWO)
sage: cputime(t)      # random
0.0040000000000000036
sage: t=cputime()
sage: for a in range(2, 10000): k = b.is_power_of(3)
sage: cputime(t)      # random
0.040003000000000011
sage: t=cputime()
sage: for a in range(2, 10000): k = b.is_power_of(a)
sage: cputime(t)      # random
0.02800199999999986

is_prime( )

Retuns True if self is prime

sage: z = 2^31 - 1
sage: z.is_prime()
True
sage: z = 2^31
sage: z.is_prime()
False

is_prime_power( )

Returns True if $ x$ is a prime power, and False otherwise.

INPUT:
    flag (for primality testing) -- int 
            0 (default): use a combination of algorithms.
            1: certify primality using the Pocklington-Lehmer Test.
            2: certify primality using the APRCL test.

sage: (-10).is_prime_power()
False
sage: (10).is_prime_power()
False
sage: (64).is_prime_power()
True
sage: (3^10000).is_prime_power()
True
sage: (10000).is_prime_power(flag=1)
False

is_pseudoprime( )

Retuns True if self is a pseudoprime

sage: z = 2^31 - 1
sage: z.is_pseudoprime()
True
sage: z = 2^31
sage: z.is_pseudoprime()
False

is_square( )

Returns True if self is a perfect square

sage: Integer(4).is_square()
True
sage: Integer(41).is_square()
False

is_squarefree( )

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

sage: Integer(100).is_squarefree()
False
sage: Integer(102).is_squarefree()
True

is_unit( )

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

sage: for n in srange(-2,3):
...    print n, n.is_unit()
-2 False
-1 True
0 False
1 True
2 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

sage: Integer(-102).isqrt()
Traceback (most recent call last):
...
ValueError: square root of negative integer not defined.

jacobi( )

Calculate the Jacobi symbol $ \left(\frac{self}{b}\right)$.

sage: z = -1
sage: z.jacobi(17)
1
sage: z.jacobi(19)
-1
sage: z.jacobi(17*19)
-1
sage: (2).jacobi(17)
1
sage: (3).jacobi(19)
-1
sage: (6).jacobi(17*19)
-1
sage: (6).jacobi(33)
0
sage: a = 3; b = 7
sage: a.jacobi(b) == -b.jacobi(a)
True

kronecker( )

Calculate the Kronecker symbol $ \left(\frac{self}{b}\right)$ with the Kronecker extension $ (self/2)=(2/self)$ when self odd, or $ (self/2)=0$ when $ self$ even.

sage: z = 5
sage: z.kronecker(41)
1
sage: z.kronecker(43)
-1
sage: z.kronecker(8)
-1
sage: z.kronecker(15)
0
sage: a = 2; b = 5
sage: a.kronecker(b) == b.kronecker(a)
True

list( )

Return a list with this integer in it, to be compatible with the method for number fields.

sage: m = 5
sage: m.list()
[5]

multiplicative_order( )

Return the multiplicative order of self.

sage: ZZ(1).multiplicative_order()
1
sage: ZZ(-1).multiplicative_order()
2
sage: ZZ(0).multiplicative_order()
+Infinity
sage: ZZ(2).multiplicative_order()
+Infinity

next_prime( )

Returns the next prime after self.

INPUT:
    proof -- bool or None (default: None, see proof.arithmetic or
                    sage.structure.proof)
                Note that the global Sage default is proof=True

sage: Integer(100).next_prime()
101

Use Proof = False, which is way faster:

sage: b = (2^1024).next_prime(proof=False)

sage: Integer(0).next_prime()
2
sage: Integer(1001).next_prime()
1009

next_probable_prime( )

Returns the next probable prime after self, as determined by PARI.

sage: (-37).next_probable_prime()
2
sage: (100).next_probable_prime()
101
sage: (2^512).next_probable_prime()
134078079299425970995740249982058461274793658205923933777235614437217640300
735469768018742981669034276900318581864860508537538828119465699464336490060
84171        
sage: 0.next_probable_prime()
2
sage: 126.next_probable_prime()
127
sage: 144168.next_probable_prime()
144169

nth_root( )

Returns the truncated nth root of self.

INPUT:
    n -- integer >= 1 (must fit in C int type)
    report_exact -- boolean, whether to report if the root extraction
                  was exact

OUTPUT:
   If report_exact is 0 (default), then returns the truncation of the
   nth root of self (i.e. rounded towards zero).

If report_exact is 1, then returns the nth root and a boolean indicating whether the root extraction was exact.

Author: David Harvey (2006-09-15)

sage: Integer(125).nth_root(3)
5
sage: Integer(124).nth_root(3)
4
sage: Integer(126).nth_root(3)
5

sage: Integer(-125).nth_root(3)
-5
sage: Integer(-124).nth_root(3)
-4
sage: Integer(-126).nth_root(3)
-5

sage: Integer(125).nth_root(2, True)
(11, False)
sage: Integer(125).nth_root(3, True)
(5, True)

sage: Integer(125).nth_root(-5)
Traceback (most recent call last):
...
ValueError: n (=-5) must be positive

sage: Integer(-25).nth_root(2)
Traceback (most recent call last):
...
ValueError: cannot take even root of negative number

numerator( )

Return the numerator of this integer.

sage: x = 5
sage: x.numerator()
5

sage: x = 0
sage: x.numerator()
0

ord( )
Synonym for valuation

sage: n=12
sage: n.ord(3)
1

powermod( )

Compute self**exp modulo mod.

sage: z = 2
sage: z.powermod(31,31)
2
sage: z.powermod(0,31)
1
sage: z.powermod(-31,31) == 2^-31 % 31
True

As expected, the following is invalid:

sage: z.powermod(31,0)
Traceback (most recent call last):
...
ZeroDivisionError: cannot raise to a power modulo 0

powermodm_ui( )

Computes self**exp modulo mod, where exp is an unsigned long integer.

sage: z = 32
sage: z.powermodm_ui(2, 4)
0
sage: z.powermodm_ui(2, 14)
2
sage: z.powermodm_ui(2^32-2, 14)
2
sage: z.powermodm_ui(2^32-1, 14)
Traceback (most recent call last):                              # 32-bit
...                                                             # 32-bit
OverflowError: exp (=4294967295) must be <= 4294967294          # 32-bit
8              # 64-bit
sage: z.powermodm_ui(2^65, 14)
Traceback (most recent call last):                              
...                                                             
OverflowError: exp (=36893488147419103232) must be <= 4294967294  # 32-bit
OverflowError: exp (=36893488147419103232) must be <= 18446744073709551614 
# 64-bit

quo_rem( )

Returns the quotient and the remainder of self divided by other.

INPUT:
    other -- the integer the divisor

OUTPUT:
    q   -- the quotient of self/other
    r   -- the remainder of self/other

sage: z = Integer(231)
sage: z.quo_rem(2)
(115, 1)
sage: z.quo_rem(-2)
(-115, 1)
sage: z.quo_rem(0)
Traceback (most recent call last):
...
ZeroDivisionError: other (=0) must be nonzero

radical( )

Return the product of the prime divisors of self.

If self is 0, returns 1.

sage: Integer(10).radical()
10
sage: Integer(20).radical()
10
sage: Integer(-20).radical()
-10
sage: Integer(0).radical()
1
sage: Integer(36).radical()
6

rational_reconstruction( )

Return the rational reconstruction of this integer modulo m, i.e., the unique (if it exists) rational number that reduces to self modulo m and whose numerator and denominator is bounded by sqrt(m/2).

sage: (3/7)%100
29
sage: (29).rational_reconstruction(100)
3/7

set_si( )

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

sage: n= ZZ(54)
sage: n.set_si(-43344);n
-43344
sage: n.set_si(43344);n
43344

Note that an error occurs when we are not dealing with integers anymore

sage: n.set_si(2^32);n
Traceback (most recent call last):      # 32-bit
...                                     # 32-bit
OverflowError: long int too large to convert to int   # 32-bit
4294967296       # 64-bit
sage: n.set_si(-2^32);n
Traceback (most recent call last):      # 32-bit
...                                     # 32-bit
OverflowError: long int too large to convert to int     # 32-bit
-4294967296      # 64-bit

set_str( )

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

sage: n=100
sage: n.set_str('100000',2)
sage: n
32

If the number begins with '0X' or '0x', it is converted to an hex number:

sage: n.set_str('0x13',0)
sage: n
19
sage: n.set_str('0X13',0)
sage: n
19

If the number begins with a '0', it is converted to an octal number:

sage: n.set_str('013',0)
sage: n
11

'13' is not a valid binary number so the following raises an exception:

sage: n.set_str('13',2)
Traceback (most recent call last):
...
TypeError: unable to convert x (=13) to an integer in base 2

sqrt( )

The square root function.

INPUT:
    prec -- integer (default: None): if None, returns an exact
         square root; otherwise returns a numerical square
         root if necessary, to the given bits of precision.
    extend -- bool (default: True); if True, return a square
         root in an extension ring, if necessary. Otherwise,
         raise a ValueError if the square is not in the base
         ring.
    all -- bool (default: False); if True, return all square
         roots of self, instead of just one.

sage: Integer(144).sqrt()
12
sage: Integer(102).sqrt()
sqrt(102)

sage: n = 2
sage: n.sqrt(all=True)
[sqrt(2), -sqrt(2)]
sage: n.sqrt(prec=10)
1.4
sage: n.sqrt(prec=100)
1.4142135623730950488016887242
sage: n.sqrt(prec=100,all=True)
[1.4142135623730950488016887242, -1.4142135623730950488016887242]
sage: n.sqrt(extend=False)
Traceback (most recent call last):
...
ValueError: square root of 2 not an integer
sage: Integer(144).sqrt(all=True)
[12, -12]
sage: Integer(0).sqrt(all=True)
[0]

sqrt_approx( )

sage: 5.sqrt_approx(prec=200)
2.2360679774997896964091736687312762354406183596115257242709
sage: 5.sqrt_approx()
2.23606797749979
sage: 4.sqrt_approx()
2

squarefree_part( )

Return the square free part of $ x$ (=self), i.e., the unique integer $ z$ that $ x = z y^2$, with $ y^2$ a perfect square and $ z$ square-free.

Use self.radical() for the product of the primes that divide self.

If self is 0, just returns 0.

sage: squarefree_part(100)
1
sage: squarefree_part(12)
3
sage: squarefree_part(17*37*37)
17
sage: squarefree_part(-17*32)
-34
sage: squarefree_part(1)
1
sage: squarefree_part(-1)
-1
sage: squarefree_part(-2)
-2
sage: squarefree_part(-4)
-1

str( )

Return the string representation of self in the given base.

sage: Integer(2^10).str(2)
'10000000000'
sage: Integer(2^10).str(17)
'394'

sage: two=Integer(2)
sage: two.str(1)
Traceback (most recent call last):
...
ValueError: base (=1) must be between 2 and 36

sage: two.str(37)
Traceback (most recent call last):
...            
ValueError: base (=37) must be between 2 and 36

sage: big = 10^5000000
sage: s = big.str()                 # long time (> 20 seconds)
sage: len(s)                        # long time (depends on above defn of s)
5000001
sage: s[:10]                        # long time (depends on above defn of s)
'1000000000'

val_unit( )

Returns a pair: the p-adic valuation of self, and the p-adic unit of self.

INPUT:
    p -- an integer at least 2.

OUTPUT:
    v_p(self) -- the p-adic valuation of self
    u_p(self) -- self / p^{v_p(self)}

sage: n = 60
sage: n.val_unit(2)
(2, 15)
sage: n.val_unit(3)
(1, 20)
sage: n.val_unit(7)
(0, 60)
sage: (2^11).val_unit(4)
(5, 2)
sage: 0.val_unit(2)
(+Infinity, 1)

valuation( )

Return the p-adic valuation of self.

INPUT:
    p -- an integer at least 2.

sage: n = 60
sage: n.valuation(2)
2
sage: n.valuation(3)
1
sage: n.valuation(7)
0
sage: n.valuation(1)
Traceback (most recent call last):
...
ValueError: You can only compute the valuation with respect to a integer
larger than 1.

We do not require that p is a prime:

sage: (2^11).valuation(4)
5

Special Functions: __abs__,$  $ __and__,$  $ __copy__,$  $ __eq__,$  $ __float__,$  $ __floordiv__,$  $ __ge__,$  $ __gt__,$  $ __hex__,$  $ __index__,$  $ __int__,$  $ __invert__,$  $ __le__,$  $ __long__,$  $ __lshift__,$  $ __lt__,$  $ __mod__,$  $ __ne__,$  $ __or__,$  $ __pos__,$  $ __pow__,$  $ __rand__,$  $ __reduce__,$  $ __repr__,$  $ __rfloordiv__,$  $ __rlshift__,$  $ __rmod__,$  $ __ror__,$  $ __rpow__,$  $ __rrshift__,$  $ __rshift__,$  $ _im_gens_,$  $ _interface_init_,$  $ _l_action,$  $ _latex_,$  $ _lcm,$  $ _mathml_,$  $ _pari_,$  $ _r_action,$  $ _xgcd

__copy__( )

Return a copy of the integer.

sage: n = 2
sage: copy(n)
2        
sage: copy(n) is n
False

__reduce__( )

This is used when pickling integers.

sage: n = 5
sage: t = n.__reduce__(); t
(<built-in function make_integer>, ('5',))
sage: t[0](*t[1])
5
sage: loads(dumps(n)) == n
True

_im_gens_( )

Return the image of self under the map that sends the generators of the parent to im_gens. Since ZZ maps canonically in the category of rings, this is just the natural coercion.

sage: n = -10
sage: R = GF(17)
sage: n._im_gens_(R, [R(1)])
7

_interface_init_( )

Return canonical string to coerce this integer to any other math software, i.e., just the string representation of this integer in base 10.

sage: n = 9390823
sage: n._interface_init_()
'9390823'

_l_action( )

sage: [0] * 8
[0, 0, 0, 0, 0, 0, 0, 0]
sage: 'hi' * 8
'hihihihihihihihi'

_latex_( )

Return latex representation of this integer. This is just the underlying string representation and nothing more. This is called by the latex function.

sage: n = -5; n._latex_()
'-5'
sage: latex(n)
-5

_lcm( )

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

sage: n = 60
sage: n._lcm(150)
300

_mathml_( )

Return mathml representation of this integer.

sage: mathml(-45)
<mn>-45</mn>
sage: (-45)._mathml_()
'<mn>-45</mn>'

_pari_( )

Returns the PARI version of this integer.

sage: n = 9390823
sage: m = n._pari_(); m
9390823
sage: type(m)
<type 'sage.libs.pari.gen.gen'>

TESTS:

sage: n = 10^10000000
sage: m = n._pari_() ## crash from trac 875
sage: m % 1234567
1041334

_r_action( )

sage: 8 * [0]
[0, 0, 0, 0, 0, 0, 0, 0]
sage: 8 * 'hi'
'hihihihihihihihi'

_xgcd( )

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

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

sage: n = 6
sage: g, s, t = n._xgcd(8)
sage: s*6 + 8*t
2
sage: g
2

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