# Programming for Number Theory

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

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

• JavaScript - JavaScript is an interpreted scripting language supported by most web browsers, which supports the use of positive integers up to 53 bits in size.
• Python - The Python language is an interpreted language which particularly lends itself to number theory. It has infinite precision integer arithmetic.
• TI Calculator - The Texas Instruments family of programmable calculators has a simple programming language and allows the use of integers up to 46 bits in size. Programs implementing many of the algorithms are available here.
• HyperCard - The HyperCard program was available on older Macintosh machines and allowed the use of integers up to 63 bits in size. Programs implementing many of the algorithms are available here.
• QBASIC - The QBASIC interpreter was available on MS-DOS and older versions of Windows and allows the use of integers up to 31 bits in size. Programs implementing many of the algorithms are available here.

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

• Maple
• Mathematica
• MAGMA
• PARI - The PARI library (see the next section) also has a command line interface, `gp`, which allows the use of the functions in the library without C programming. Note that PARI is a freeware package.
• SAGE

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

• GMP - GNU Multiple Precision. A very efficient package for multiple precision arithmetic. This package only has simple functions though, up through gcd(). More sophisticated number theory functions have to be designed by the programmer from scratch.
• PARI - This package seems to be slightly less efficient library than GMP but it has a very rich collection of functions, covering a very deep slice of computational number theory. (Note that while this is a freeware package it stopped being developed in 1997 - future versions are included in the commercial MAGMA system.

Robert Campbell