# Wolfram Language & System 10.0 (2014)|Legacy Documentation

# Integer and Number Theoretic Functions

Mod[k,n] | k modulo n (remainder from dividing k by n) |

Quotient[m,n] | the quotient of m and n (truncation of ) |

QuotientRemainder[m,n] | a list of the quotient and the remainder |

Divisible[m,n] | test whether m is divisible by n |

CoprimeQ[n_{1},n_{2},…] | test whether the are pairwise relatively prime |

GCD[n_{1},n_{2},…] | the greatest common divisor of , , … |

LCM[n_{1},n_{2},…] | the least common multiple of , , … |

KroneckerDelta[n_{1},n_{2},…] | the Kronecker delta equal to 1 if all the are equal, and 0 otherwise |

IntegerDigits[n,b] | the digits of n in base b |

IntegerExponent[n,b] | the highest power of b that divides n |

In[1]:= |

Out[1]= |

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

For any integers a and b, it is always true that b*Quotient[a,b]+Mod[a,b] is equal to .

Mod[k,n] | result in the range 0 to |

Mod[k,n,1] | result in the range 1 to n |

Mod[k,n,-n/2] | result in the range to |

Mod[k,n,d] | result in the range d to |

Integer remainders with offsets.

Particularly when you are using Mod to get indices for parts of objects, you will often find it convenient to specify an offset.

In[5]:= |

Out[5]= |

The *greatest common divisor* function GCD[n_{1},n_{2},…] gives the largest integer that divides all the exactly. When you enter a ratio of two integers, the Wolfram Language effectively uses GCD to cancel out common factors and give a rational number in lowest terms.

The *least common multiple* function LCM[n_{1},n_{2},…] gives the smallest integer that contains all the factors of each of the .

In[6]:= |

Out[6]= |

The *Kronecker delta* function KroneckerDelta[n_{1},n_{2},…] is equal to 1 if all the are equal, and is 0 otherwise. can be thought of as a totally symmetric tensor.

In[7]:= |

Out[7]= |

FactorInteger[n] | a list of the prime factors of n, and their exponents |

Divisors[n] | a list of the integers that divide n |

Prime[k] | the k prime number |

PrimePi[x] | the number of primes less than or equal to x |

PrimeQ[n] | give True if n is a prime, and False otherwise |

PrimeNu[n] | the number of distinct primes in n |

PrimeOmega[n] | the number of prime factors counting multiplicities in n |

LiouvilleLambda[n] | the Liouville function |

MangoldtLambda[n] | the von Mandgoldt function |

FactorInteger[n,GaussianIntegers->True] | |

a list of the Gaussian prime factors of the Gaussian integer n, and their exponents | |

PrimeQ[n,GaussianIntegers->True] | give True if n is a Gaussian prime, and False otherwise |

Integer factoring and related functions.

In[8]:= |

Out[8]= |

In[9]:= |

Out[9]= |

You should realize that according to current mathematical thinking, integer factoring is a fundamentally difficult computational problem. As a result, you can easily type in an integer that the Wolfram Language will not be able to factor in anything short of an astronomical length of time. But as long as the integers you give are less than about 50 digits long, FactorInteger should have no trouble. And in special cases it will be able to deal with much longer integers.

In[10]:= |

Out[10]= |

In[11]:= |

Out[11]= |

Although the Wolfram Language may not be able to factor a large integer, it can often still test whether or not the integer is a prime. In addition, the Wolfram Language has a fast way of finding the prime number.

In[12]:= |

Out[12]= |

In[13]:= |

Out[13]= |

In[14]:= |

Out[14]= |

Particularly in number theory, it is often more important to know the distribution of primes than their actual values. The function PrimePi[x] gives the number of primes that are less than or equal to .

In[15]:= |

Out[15]= |

In[16]:= |

Out[16]= |

In[17]:= |

Out[17]= |

In[18]:= |

Out[18]= |

In[19]:= |

Out[19]= |

By default, FactorInteger allows only real integers. But with the option setting GaussianIntegers->True, it also handles *Gaussian integers*, which are complex numbers with integer real and imaginary parts. Just as it is possible to factor uniquely in terms of real primes, it is also possible to factor uniquely in terms of Gaussian primes. There is nevertheless some potential ambiguity in the choice of Gaussian primes. In the Wolfram Language, they are always chosen to have positive real parts, and non‐negative imaginary parts, except for a possible initial factor of or .

In[20]:= |

Out[20]= |

In[21]:= |

Out[21]= |

PowerMod[a,b,n] | the power modulo n |

DirichletCharacter[k,j,n] | the Dirichlet character |

EulerPhi[n] | the Euler totient function |

MoebiusMu[n] | the Möbius function |

DivisorSigma[k,n] | the divisor function |

DivisorSum[n,form] | the sum of for all i that divide n |

DivisorSum[n,form,cond] | the sum for only those divisors for which gives True |

JacobiSymbol[n,m] | the Jacobi symbol |

ExtendedGCD[n_{1},n_{2},…] | the extended GCD of , , … |

MultiplicativeOrder[k,n] | the multiplicative order of k modulo n |

MultiplicativeOrder[k,n,{r_{1},r_{2},…}] | the generalized multiplicative order with residues |

CarmichaelLambda[n] | the Carmichael function |

PrimitiveRoot[n] | a primitive root of n |

Some functions from number theory.

The *modular power function* PowerMod[a,b,n] gives exactly the same results as Mod[a^b,n] for b>0. PowerMod is much more efficient, however, because it avoids generating the full form of .

You can use PowerMod not only to find positive modular powers, but also to find *modular inverses*. For negative b, PowerMod[a,b,n] gives, if possible, an integer such that . (Whenever such an integer exists, it is guaranteed to be unique modulo n.) If no such integer exists, the Wolfram Language leaves PowerMod unevaluated.

In[22]:= |

Out[22]= |

In[23]:= |

Out[23]= |

In[24]:= |

Out[24]= |

In[25]:= |

Out[25]= |

In[26]:= |

Out[26]= |

In[27]:= |

Out[27]= |

In[28]:= |

Out[28]= |

In[29]:= |

Out[29]= |

In[30]:= |

Out[30]= |

In[31]:= |

Out[31]= |

In[32]:= |

Out[32]= |

There are distinct Dirichlet characters for a given modulus k, as labeled by the index j. Different conventions can give different orderings for the possible characters.

In[33]:= |

Out[33]= |

The *Euler totient function* gives the number of integers less than that are relatively prime to . An important relation (Fermat's little theorem) is that for all relatively prime to .

The *Möbius function* is defined to be if is a product of distinct primes, and if contains a squared factor (other than 1). An important relation is the Möbius inversion formula, which states that if for all , then , where the sums are over all positive integers that divide .

The *divisor function* is the sum of the powers of the divisors of . The function gives the total number of divisors of , and is variously denoted , and . The function , equal to the sum of the divisors of , is often denoted .

In[34]:= |

Out[34]= |

In[35]:= |

Out[35]= |

In[36]:= |

Out[36]= |

In[37]:= |

Out[37]= |

The function DivisorSum[n,form] represents the sum of for all i that divide n. DivisorSum[n,form,cond] includes only those divisors for which gives True.

In[38]:= |

Out[38]= |

In[39]:= |

Out[39]= |

The *Jacobi symbol* JacobiSymbol[n,m] reduces to the *Legendre symbol* when is an odd prime. The Legendre symbol is equal to zero if is divisible by ; otherwise it is equal to if is a quadratic residue modulo the prime , and to if it is not. An integer relatively prime to is said to be a quadratic residue modulo if there exists an integer such that . The full Jacobi symbol is a product of the Legendre symbols for each of the prime factors such that .

The *extended GCD* ExtendedGCD[n_{1},n_{2},…] gives a list where is the greatest common divisor of the , and the are integers such that . The extended GCD is important in finding integer solutions to linear Diophantine equations.

In[40]:= |

Out[40]= |

In[41]:= |

Out[41]= |

The multiplicative order function MultiplicativeOrder[k,n] gives the smallest integer such that . Then is known as the order of modulo . The notation is occasionally used.

The generalized multiplicative order function MultiplicativeOrder[k,n,{r_{1},r_{2},…}] gives the smallest integer such that for some . MultiplicativeOrder[k,n,{-1,1}] is sometimes known as the *suborder function* of modulo , denoted . MultiplicativeOrder[k,n,{a}] is sometimes known as the discrete log of with respect to the base modulo .

The *Carmichael function* or *least universal exponent* gives the smallest integer such that for all integers relatively prime to .

ContinuedFraction[x,n] | generate the first n terms in the continued fraction representation of x |

FromContinuedFraction[list] | reconstruct a number from its continued fraction representation |

Rationalize[x,dx] | find a rational approximation to x with tolerance dx |

In[42]:= |

Out[42]= |

In[43]:= |

Out[43]= |

In[44]:= |

Out[44]= |

In[45]:= |

Out[45]= |

*Continued fractions* appear in many number theoretic settings. Rational numbers have terminating continued fraction representations. Quadratic irrational numbers have continued fraction representations that become repetitive.

ContinuedFraction[x] | the complete continued fraction representation for a rational or quadratic irrational number |

QuadraticIrrationalQ[x] | test whether x is a quadratic irrational |

RealDigits[x] | the complete digit sequence for a rational number |

RealDigits[x,b] | the complete digit sequence in base b |

Complete representations for numbers.

In[46]:= |

Out[46]= |

In[47]:= |

Out[47]= |

In[48]:= |

Out[48]= |

In[49]:= |

Out[49]= |

In[50]:= |

Out[50]= |

*Continued fraction convergents* are often used to approximate irrational numbers by rational ones. Those approximations alternate from above and below, and converge exponentially in the number of terms. Furthermore, a convergent of a simple continued fraction is better than any other rational approximation with denominator less than or equal to .

Convergents[x] | give a list of rational approximations of x |

Convergents[x,n] | give only the first n approximations |

Continued fraction convergents.

In[51]:= |

Out[51]= |

In[52]:= |

Out[52]= |

In[53]:= |

Out[53]= |

In[54]:= |

Out[54]= |

LatticeReduce[{v_{1}v_{2},…}] | a reduced lattice basis for the set of integer vectors |

HermiteDecomposition[{v_{1},v_{2},…}] | the echelon form for the set of integer vectors |

Functions for integer lattices.

The lattice reduction function LatticeReduce[{v_{1},v_{2},…}] is used in several kinds of modern algorithms. The basic idea is to think of the vectors of integers as defining a mathematical *lattice*. Any vector representing a point in the lattice can be written as a linear combination of the form , where the are integers. For a particular lattice, there are many possible choices of the "basis vectors" . What LatticeReduce does is to find a reduced set of basis vectors for the lattice, with certain special properties.

In[55]:= |

Out[55]= |

In[56]:= |

Out[56]= |

Notice that in the last example, LatticeReduce replaces vectors that are nearly parallel by vectors that are more perpendicular. In the process, it finds some quite short basis vectors.

For a matrix , HermiteDecomposition gives matrices and such that is unimodular, , and is in reduced row echelon form. In contrast to RowReduce, pivots may be larger than 1 because there are no fractions in the ring of integers. Entries above a pivot are minimized by subtracting appropriate multiples of the pivot row.

In[57]:= |

Out[57]= |

In[58]:= |

Out[58]= |

In[59]:= |

Out[59]= |

DigitCount[n,b,d] | the number of d digits in the base b representation of n |

In[60]:= |

Out[60]= |

In[61]:= |

Out[61]= |

In[62]:= |

Out[62]= |

BitAnd[n_{1},n_{2},…] | bitwise AND of the integers |

BitOr[n_{1},n_{2},…] | bitwise OR of the integers |

BitXor[n_{1},n_{2},…] | bitwise XOR of the integers |

BitNot[n] | bitwise NOT of the integer n |

BitLength[n] | number of binary bits in the integer n |

BitSet[n,k] | set bit k to 1 in the integer n |

BitGet[n,k] | get bit k from the integer n |

BitClear[n,k] | set bit k to 0 in the integer n |

BitShiftLeft[n,k] | shift the integer n to the left by k bits, padding with zeros |

BitShiftRight[n,k] | shift to the right, dropping the last k bits |

Bitwise operations act on integers represented as binary bits. BitAnd[n_{1},n_{2},…] yields the integer whose binary bit representation has ones at positions where the binary bit representations of all of the have ones. BitOr[n_{1},n_{2},…] yields the integer with ones at positions where any of the have ones. BitXor[n_{1},n_{2}] yields the integer with ones at positions where or but not both have ones. BitXor[n_{1},n_{2},…] has ones where an odd number of the have ones.

In[63]:= |

Out[63]//BaseForm= | |

Bitwise operations are used in various combinatorial algorithms. They are also commonly used in manipulating bitfields in low‐level computer languages. In such languages, however, integers normally have a limited number of digits, typically a multiple of 8. Bitwise operations in the Wolfram Language in effect allow integers to have an unlimited number of digits. When an integer is negative, it is taken to be represented in two's complement form, with an infinite sequence of ones on the left. This allows BitNot[n] to be equivalent simply to .

SquareFreeQ[n] | give True if n does not contain a squared factor, False otherwise |

SquareFreeQ[n] checks to see if n has a square prime factor. This is done by computing MoebiusMu[n] and seeing if the result is zero; if it is, then n is not squarefree, otherwise it is. Computing MoebiusMu[n] involves finding the smallest prime factor q of n. If n has a small prime factor (less than or equal to ), this is very fast. Otherwise, FactorInteger is used to find q.

In[64]:= |

Out[64]= |

In[65]:= |

Out[65]= |

In[66]:= |

Out[66]= |

NextPrime[n] | give the smallest prime larger than n |

RandomPrime[{min,max}] | return a random prime number between min and max |

RandomPrime[max] | return a random prime number less than or equal to max |

RandomPrime[{min,max},n] | return n random prime numbers between min and max |

RandomPrime[max,n] | return n random prime numbers less than or equal to max |

NextPrime[n] finds the smallest prime p such that . For n less than 20 digits, the algorithm does a direct search using PrimeQ on the odd numbers greater than n. For n with more than 20 digits, the algorithm builds a small sieve and first checks to see whether the candidate prime is divisible by a small prime before using PrimeQ. This seems to be slightly faster than a direct search.

In[67]:= |

Out[67]= |

In[68]:= |

Out[68]= |

In[69]:= |

Out[69]= |

For RandomPrime[{min,max}] and RandomPrime[max], a random prime p is obtained by randomly selecting from a prime lookup table if max is small and by a random search of integers in the range if max is large. If no prime exists in the specified range, the input is returned unevaluated with an error message.

In[70]:= |

Out[70]= |

PrimePowerQ[n] | determine whether n is a positive integer power of a rational prime |

Testing for involving prime powers.

The algorithm for PrimePowerQ involves first computing the least prime factor p of n and then attempting division by p until either 1 is obtained, in which case n is a prime power, or until division is no longer possible, in which case n is not a prime power.

In[71]:= |

Out[71]= |

In[72]:= |

Out[72]= |

ChineseRemainder[list_{1},list_{2}] | give the smallest non-negative integer r with Mod[r,list_{2}]==list_{1} |

Solving simultaneous congruences.

The Chinese remainder theorem states that a certain class of simultaneous congruences always has a solution. ChineseRemainder[list_{1},list_{2}] finds the smallest non‐negative integer r such that Mod[r,list_{2}] is . The solution is unique modulo the least common multiple of the elements of .

In[73]:= |

Out[73]= |

In[74]:= |

Out[74]= |

In[75]:= |

Out[75]= |

PrimitiveRoot[n] | give a primitive root of n, where n is a prime power or twice a prime power |

PrimitiveRoot[n] returns a generator for the group of numbers relatively prime to n under multiplication . This has a generator if and only if n is 2, 4, a power of an odd prime, or twice a power of an odd prime. If n is a prime or prime power, the least positive primitive root will be returned.

In[76]:= |

Out[76]= |

In[77]:= |

Out[77]= |

In[78]:= |

Out[78]= |

In[79]:= |

Out[79]= |

In[80]:= |

Out[80]= |

SquaresR[d,n] | give the number of representations of an integer n as a sum of d squares |

PowersRepresentations[n,k,p] | give the distinct representations of the integer n as a sum of k non-negative p integer powers |

Representing an integer as a sum of squares or other powers.

In[81]:= |

Out[81]= |