ZX Computings - OpenLibra
ZX Computings - OpenLibra
ZX Computings - OpenLibra
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Dr. Frank O 'Hara at home in<br />
Surrey proudly holding a <strong>ZX</strong>81<br />
printout of the largest known<br />
prime number: 2 ' *44497 -1.lt<br />
was discovered by Harry Nelson,<br />
47, and David Slowinski. 25, in<br />
1979, at the Lawrence Livermore<br />
Laboratory in California, after a<br />
two-month run on a Cray One<br />
computer. It has 13395 decimal<br />
digits. It took just over two and a<br />
half hours to calculate these digits<br />
on a <strong>ZX</strong>81, using a machine code<br />
program. The printout, which is<br />
seven feet, one and a haff inches<br />
long, took 15 minutes to produce<br />
on the <strong>ZX</strong> Printer.<br />
32<br />
Mathematics<br />
Delving<br />
numerically<br />
deeper<br />
Frank O'Hara from Surbiton in<br />
Surrey helped lan Logan decode<br />
the 8K ROM. Dr O'Hara has<br />
continued his investigations<br />
into the operation of tne <strong>ZX</strong>81,<br />
and here shares with us some<br />
of his discoveries, with notes<br />
on some programs on<br />
elementary number theory for<br />
the <strong>ZX</strong>81.<br />
Over the past year or so, Dr<br />
O'Hara has developed a few<br />
programs on elementary<br />
number theory for the <strong>ZX</strong>81,<br />
having previously run a few<br />
such programs on a programmable<br />
calculator, a Texas Tl<br />
58, over about 2 Vi years from<br />
mid-1 978.<br />
A couple of these programs<br />
are "one off", ie. ad hoc pro<br />
grams to solve a single program.<br />
There is one of about<br />
100 bytes of machine code<br />
which generates the decimal<br />
representation of quite large<br />
powers of 2. He used this to obtain<br />
the 13395 digits of (2 to<br />
the 44497} minus 1, the<br />
largest known prime number,<br />
discovered by Nelson and<br />
Slowinski using a Cray One<br />
computer in 1979. The program<br />
took 2 hours 31 minutes<br />
to obtain this number on the<br />
<strong>ZX</strong>81.<br />
Another even more exciting<br />
result was given by about 400<br />
bytes of machine code, including<br />
a multiple precision<br />
multiplication routine. This program<br />
actually proved the<br />
primality of the first 1 5<br />
Mersenne primes, up to and including<br />
(2 to the 1 279) minus<br />
1, a number of 386 digits. It<br />
thus repeated some of the work<br />
of "SWAC" in 1 953, going far<br />
beyond what the desk calcu<br />
lators had done and capturing<br />
some of the flavour of a historic<br />
moment (although 28 years<br />
later!).<br />
Three other programs have a<br />
more general purpose flavour, I<br />
have called them:<br />
(a) SPRF: single precision prime<br />
factorization;<br />
(b) MPRF: multiple precision<br />
prime factorization;<br />
(c) FE24: Fermat's theorem used<br />
to test numbers up to 24<br />
digits long for compositeness,<br />
ie. lack of primality.<br />
The kernel of these 3 programs<br />
is the machine code<br />
multiple precision integer division<br />
routine which finds the<br />
true integer quotient and true<br />
integer remainder of an integer<br />
of arbitrary length with respect<br />
to another arbitrarily long integer.<br />
Barden is quite mistaken<br />
when he describes this process<br />
as a "cop-out" (How to Program<br />
Microcomputers, by<br />
William Barden, Jr. Sams, Indianapolis,<br />
1 977, page 109). It<br />
is in fact very easy to program.<br />
It is a simple extension of the<br />
standard restoring division of<br />
one or two bytes by one byte.<br />
The shift is just a loop with its<br />
kernel as: LD A, (DE): RLA : LD<br />
{DE), A. The addition or subtraction<br />
is another loop centred<br />
on: LD A, (DE) : ADC A, (HL> :<br />
LD (DE), A; and so on. In fact<br />
the only complexity arises<br />
when one has to shorten the<br />
process in order to speed it up,<br />
as in the first of the 3 programs,<br />
SPRF. This program has a 4<br />
byte dividend and 2 byte<br />
divisor, and needs to use the exchange<br />
resisters H', L\ D' and<br />
E' to gain speed.<br />
The first program, SPRF,<br />
finds the smallest prime factor<br />
of any odd number from 5 to<br />
4294967255 (2 to the 32<br />
minus 1) in not more than 20<br />
seconds. If the numberis prime,<br />
the program reports this. It<br />
goes about 1000 times as fast<br />
as the Texas calculator did.<br />
This has been achieved by a<br />
series of improvements, starting<br />
with a BASIC program that<br />
was only about 10 times as fast<br />
as the calculator. The program<br />
contains about 400 bytes of<br />
machine code; 300 or so of<br />
these are just a simple linear<br />
sieve designed to exclude<br />
multiples of 3, 5 and 7 as well<br />
as 2 and so gain a factor of<br />
35/16 in speed. The speed has<br />
to be seen to be believed.<br />
Numbers up to 7 digits long are<br />
dealt with instantaneously. The<br />
largest 8. 9 and 1 0 digit primes<br />
in its range take 3, 10 and 20<br />
seconds respectively. The program<br />
can easily be adapted to<br />
print screenfuls of results and<br />
so, for example, find the largest<br />
prime less than 2 to the 32 in<br />
one run. By using random 8, 9<br />
or 10 digit input one can use it<br />
to see primes probably never<br />
seen before. (Only the first 10<br />
or 1 1 million numbers are completely<br />
and accurately listed as<br />
prime or composite.)<br />
<strong>ZX</strong> COMPUTING AUG/SEPT 1982 28