Elementary Number Theory using HyperCard


  1. General
  2. Basic Operations
  3. GCD
  4. Exponentiation - PowMod
  5. Factoring


HyperCard is a very flexible program (often referred to as a Swiss Army Knife) for the Macintosh. It has strong support of sound, graphics, but we will only be making use of its programming language - HyperTalk. When Apple first released HyperCard in 1987 it was distributed freely to Macintosh owners. Since that time it has gone through several marketing changes - at some points (including the present) it was not even shipped with new Macintoshes. (This seems to have been a function of Apple's relentless efforts to shoot itself in the foot.)

These programs are written as HyperTalk scripts. To use one of them start HyperCard and then select the New Button item from the Objects menu. If this menu is not visible or if you are unable to edit the button's script type the following sequence to change the user level:

Once a new button has been created, edit its script by clicking on it while holding down the option and the apple keys (ie do an Opt-¤-click). A script editing window will appear. Copy the script into the window and close the window. Now press the button to run the program.

HyperCard supports integers which can have values as large as 263=9223372036854775808, so the largest number which can be used in most of these algorithms is about 3000000000.

Basic Operations

The basic arithmetic operations, +, -, /, are available from the keyboard. Note that the division operator normally returns a floating point number (eg 7/2 returns 3.5) so the answer should be truncated with the trunc function (eg trunc(7/2) returns 3).

The modular reduction operator is represented with the mod keyword.



GCD - A script which implements the GCD algorithm can be found in the file gcd.script.

Exponentiation - PowMod

The Russian Peasant method for modular exponentiation is implemented in the program found in powmod.script.


A script which implements the Pollard Rho method for integer factorization is contained in the file factorPR.script.

Robert Campbell
Last modified: Dec 12, 1997