Programming for Number Theory


  1. Introduction
  2. Size & Multiple Precision
  3. Data Types & Object-Oriented Programming
  4. Languages & Implementations


Size & Multiple Precision

Some of the more interesting questions in computational number theory involve large numbers. This can be a problem as most languages and machines only support integers up to a certain fixed size, commonly 264 or 232.

This limit is further reduced by the fact that most of the algorithms require the computation of intermediate results twice the size of the number of interest. For example, the computation mod(a*b,m) is commonly found, where each of a and b are roughly the same size as m. For this computation to work, the intermediate value a*b must be less than the integer limit of the machine. Thus, on a 32-bit machine, the largest value of m for which this can be done is 216=65536. The examples in this paper are written in JavaScript, which allows 53-bit computations on any machine, thus allowing m to take on values as large as 226=67108864.

Various programming methods allow the use of larger integers, ranging from simple methods which double the size of the usable integers (see the reference by Giblin) to more complex multiple precision arithmetic packages. In particular, any of the popular symbolic algebra packages such as SAGE, Maple or Mathematica, allow computations with arbitrary precision arithmetic.

Data Types & Object-Oriented Programming

Languages & Implementations

Depending on the machine and software you have available and your need for speed of programming vice efficiency of code you have a variety of languages to choose from. Those who do not have access to fancy programming languages and who want simple programs, perhaps for educational purposes, should consider the first set of languages. Those who want to work with more sophisticated number theory should consider some of the symbolic algebra languages. Those who are concerned about efficiency and large problems should consider some of the packages listed in the third group.

Simple Interpreted Languages

The following languages are all interpreted, so they lend themselves to simple modification and reuse by students. They are also widely available on a free basis on various computers (and calculators). Their weakness is that they are too slow to be used on large problems.

Symbolic Algebra Systems

The following languages are commercial symbolic algebra packages. They have libraries and features which simplify complex programming, and can be used to teach. In particular, they allow integer computation with numbers of arbitrary size. Their nature as interpreted languages does make them too slow for very large computations, though.

Packages & Libraries for Large Problems

If you are willing to work with compiled languages (C is the best fit) the following libraries can be used to produce very efficient code. Each of these libraries is non-commercial freeware.

Robert Campbell
Last modified: Wed Sep 2 15:24:25 EDT