The greatest common divisor function GCD[n1, n2, ...] gives the largest integer that divides all the ni exactly. When you enter a ratio of two integers, Mathematica effectively uses GCD to cancel out common factors, and give a rational number in lowest terms.

The least common multiple function LCM[n1, n2, ...] gives the smallest integer that contains all the factors of each of the ni.

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 Mathematica 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.

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 (x) that are less than or equal to x.

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 Mathematica, they are always chosen to have positive real parts, and non-negative imaginary parts, except for a possible initial factor of -1 or ±i.

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 a^b.

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 k such that ka-b1modn. (Whenever such an integer exists, it is guaranteed to be unique modulo n.) If no such integer k exists, Mathematica leaves PowerMod unevaluated.

The Euler totient function (n) gives the number of integers less than n that are relatively prime to n. An important relation (Fermat's Little Theorem) is that a (n)1modn for all a relatively prime to n.

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

The divisor function k (n) is the sum of the kth powers of the divisors of n. The function 0 (n) gives the total number of divisors of n, and is variously denoted d (n), (n) and (n). The function 1 (n), equal to the sum of the divisors of n, is often denoted (n).

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

The extended gcd ExtendedGCD[n1, n2, ...] gives a list {g, {r1, r2, ...}} where g is the greatest common divisor of the ni, and the ri are integers such that g=r1n1+r2n2+.... The extended gcd is important in finding integer solutions to linear Diophantine equations.

The multiplicative order function MultiplicativeOrder[k, n] gives the smallest integer m such that km1modn. The function is sometimes known as the index or discrete log of k. The notation ordn (k) is occasionally used.

The generalized multiplicative order function MultiplicativeOrder[k, n, {r1, r2, ...}] gives the smallest integer m such that kmrimodn for some i. MultiplicativeOrder[k, n, {-1, 1}] is sometimes known as the suborder function of k modulo n, denoted sordn (k).

The Carmichael function or least universal exponent (n) gives the smallest integer m such that km1modn for all integers k relatively prime to n.

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.

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 p/q of a simple continued fraction is better than any other rational approximation with denominator less than or equal to q.

The lattice reduction function LatticeReduce[{v1, v2, ...}] is used in several kinds of modern algorithms. The basic idea is to think of the vectors vk 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 ckvk, where the ck are integers. For a particular lattice, there are many possible choices of the "basis vectors" vk. What LatticeReduce does is to find a reduced set of basis vectors for the lattice, with certain special properties.

Three unit vectors along the three coordinate axes already form a reduced basis.

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 m, HermiteDecomposition gives matrices u and r such that u is unimodular, u.m=r, and r 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 this case, the original matrix is recovered because it was in row echelon form.

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

This finds the bitwise AND of the numbers 23 and 29 entered in base 2.

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 Mathematica 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 -1-n.

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 1223), this is very fast. Otherwise, FactorInteger is used to find q.

NextPrime[n] finds the smallest prime p such that p>n. 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.

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.

The algorithm for PrimePowerQ involves first computing the least prime factor p of n and then attempting division by n 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.

The Chinese Remainder Theorem states that a certain class of simultaneous congruences always has a solution. ChineseRemainder[list1, list2] finds the smallest non-negative integer r such that Mod[r, list2] is list1. The solution is unique modulo the least common multiple of the elements of list2.

give a primitive root of n, where n is a prime power or twice a prime power

Computing primitive roots.

PrimitiveRoot[n] returns a generator for the group of numbers relatively prime to n under multiplication mod n. 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.