# Elementary Number Theory using QBASIC

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

• integer - The largest integer value is 215=32768, so the largest number which can be used in most of these algorithms is 181. Integer variables end with the character "%", eg x%.
• long integer - The largest integer value is 231=2147483648, so the largest number which can be used in most of these algorithms is 46340. Long integer variables end with the character "&", eg x&. The long integer is an extension to the BASIC language.

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

• print 8 + 7 mod 13
• print 5 * 4 mod 13
• print 7 \ 2

### GCD

• GCD - A simple example of the GCD algorithm (computing the GCD of 38 and 126) can be found in the file GCD1.BAS. The file GCD2.BAS contains a more complete and flexible implementation of GCD for long integers. This second program is written in the form of a function call so it can be used as part of larger programs.
• EXTGCD - The extended GCD algorithm is implemented in the file EXTGCD1.BAS. This file computes the extended GCD of 4 and 13 (hence computing 4(-1) (mod 13)).

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

• Sieving - The program in the file PRIMSIEV.BAS finds all the primes up to some bound by implementing the sieve of Eratosthenes.
• Pseudoprime Test - The program in the file PSPRIME.BAS tests a number for primality with the Euler pseudoprime test using bases 2, 3 and 5. This is implemented as a function call so it is callable from larger programs and uses long integers.

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