Elementary Number Theory using QBASIC


Contents

  1. General
  2. Basic Operations
  3. GCD
  4. Exponentiation - PowMod
  5. Element Orders
  6. Finding Primes
  7. Factoring
  8. Multiple Precision

General

QBASIC is the BASIC interpreter which has been shipped as part of MS-DOS since version 5.0 (circa 1991). This includes MS-DOS running on machines running Windows. QBASIC extends the usual BASIC language in several ways, but the only extension we will use is that of long integer. Those new to QBASIC can find information on the language at www.qbasic.com. Although QBASIC is no longer distributed with some newer versions of Windows, it can be gotten from Microsoft as part of a package of utilities in the file olddos.exe, currently available at http://download.microsoft.com/download/win95upg/tool_s/1.0/w95/en-us/olddos.exe.

QBASIC supports two types of integers:

Basic Operations

The basic arithmetic operations, +, -, are available from the keyboard. Integer division (dropping any remainder) is represented by the \ operator.

The modular reduction operator is represented with the mod keyword.

Examples:

GCD

Exponentiation - PowMod

The Russian Peasant method for modular exponentiation is implemented in the simple program found in POWMOD1.BAS. A more flexible implementation of the algorithm for long integers can be found in the file POWMOD2.BAS. Here the algorithm is in the form of a function call.

Element Orders

The program located in the file ORDER.BAS computes the order of a number modulo a specified modulus (if it has an order) by a simple exhaustive method.

Finding Primes

Factoring

The program in the file FACTORPR.BAS will extract a single prime factor from a composite number using Pollard's Rho algorithm. This algorithm uses long integers and is implemented as a function call so it is usable as part of larger programs.

Multiple Precision

The 32 bit (about 10 digit) machine word length of QBASIC can be exceeded by using either a multiple precision library or a BASIC interpreter which includes multiple precision arithmetic.

The package BIGNUM.BAS is a simple multiple precision package written for QBASIC. BIGNUM.BAS was written by Marc Kummel of Dunn Middle School, Santa Barbara, CA, and can be found at http://www.rain.org/~mkummel/tbvault.html.

UBASIC, a BASIC interpreter with built in multiple precision was written by Yuji Kida of Rikkyo Univ and is available from ftp://rkmath.rikkyo.ac.jp/pub/ubibm/.


Robert Campbell
Last modified: Oct 6, 2002