Program PERFECT and PRIME
By 4md (Graham Gallagher)
Use Experiment with numbers
This program was inspired by a
question from my son Ian (aged 11).
He'd been reading the section on
MATHEMATICS in the GUINNESS BOOK OF
RECORDS and was intrigued by perfect
numbers. I had to be honest and
confess that I didn't know what these
were. The explanation followed that a
number is "perfect" if all of it's
factors, other than itself, add up to
equal the number:
ie. 1+2+3 = 6, the lowest perfect no.
The next is 1+2+4+7+14 = 28
These were both known to my son. What
he asked was did I know of any others?
I obviously didn't but suggested that
I could write a short program to
search them out. Hence a fore-runner
of the program PERFECT was produced
which sluggishly found the next
perfect number and after a few
incremental restarts over 2 or 3 days
found the next.
After the above exercise I decided to
read the bit in GUINESS BOOK OF
RECORDS for myself and discovered
that to dated there are only 32
known perfect numbers; the highest
being:
((2^756839)-1)*(2^756838)
This number if printed out in full has
455663 digits and would fill over 100
sheets of A4 paper. The expression on
the left hand side of the multiplier
is the largest known prime number and
was discovered in February 1992.
Armed with this information I thought
that I could reduce the search time by
using a sub-routine which identifies
prime numbers. Unfortunately, I've not
been able to make use of such a
sub-routine in the program searching
for perfect numbers. Hence I've
included it as a stand alone program
(ie.PRIME). This simply identifies a
screen-full of prime numbers and then
stops.
A more useful approach was to assume
that all perfect numbers follow the
same expression as the highest known
perfect number:
ie. perfect no. A=(2^J)*((2^(J-1))-1)
The latest program (ie.PERFECT)
therefore produces a value for "A" and
then importantly checks to see if the
number is in fact "perfect". This soon
takes the BEEB quite some time to do,
but fortunately the numbers found do
agree with those calculated by my
first version of the program which
laboriously checked each number from
one upwards.
I'll now point out that it takes the
BEEB about 90 seconds to find the
fourth perfect number using the
program PERFECT as it now stands. I
have to admit that I haven't left it
running long enough to find the fifth
one. However, I asked a work colleague
to run the program on a PC486 machine
and managed to calculate this fifth
perfect number with some certainty.
Some further refinements have been
made in order to deal with the very
large numbers involved and the PC486
has now regurtitated what we believe
to be 8 out of the 32 known perfect
numbers. We are not entirely sure that
they are correct and there may be
other perfect numbers lower than the
ones we've found which don't fit the
expression used in the program. If
anyone has a list or knows of a source
of reference I'd appreciate the
assistance (Tel. 0788 823214).
Finally, perhaps someone would care to
convert the program to machine code.
From: K4D (Lorin Knight)
Programs: MAGIC3D, MODE4PR
Associated files: MEX-HAT, FURROWS
Use: Creating "magic" 3-D illusions
You may have seen the MAGIC EYE
books or posters in which a 3-D image
mysteriously appears from what, at
first sight, appears to be a random
pattern of coloured dots or shapes.
MAGIC3D enables simple black-and-white
pictures of this type to be produced
on a BBC computer.
A MODE-4 picture is built up on the
monitor screen, a process typically
taking a little over an hour, and when
completed is dumped onto disk. In
order to see the 3-D effect you need
to print it out. This can be done
using the associated program MODE4PR
If you have an Epson 9-wire dot matrix
(or compatible) printer. Otherwise I'm
afraid you will have to find some
other way to print it.
Two sample screen dumps, MEX-HAT
and FURROWS are included on the drive
2 side of this disk so that you can
print out examples showing what the
program can produce.
EDITOR..... ADFS version the files are
in DIR $.Software
The recommended way of seeing the
3-D illusion is to hold the printout
right up to the nose and to stare
straight ahead, while moving the
picture away very very slowly. The
illusion should then gradually appear.
You may not succeed first time but
you will soon get the knack after a
litle practice. Don't try doing this
with the display on the monitor. It
could be harmful to put your face right
up to the screen .
In MAGIC3D a mathematical formula
(in line 80) is used to define the 3-D
image. As written it produces a
pyramid. Within the REM statements at
the end of the program you will find
alternative lines which can be used to
produce other images.
HOW IS THE 3-D ILLUSION PRODUCED?
For each line of the picture the
program produces a different 60-pixel
loop of random dots and spaces (1s and
0s). To produce a background plane,
the loop is repeated throughout the
line, making a pattern which repeats
itself five times across the
picture. When you are "tuned in" to
the 3-D mode, instead of both eyes
focussing on the same pattern, they
focus separately onto two adjacent
patterns. The brain is then fooled
into seeing a single pattern which is
some distance behind the plane of the
paper.
To produce a foreground plane, the
program reduces the loop length to 45.
This brings the repeating patterns
closr together and the brain perceives
the image to be nearer the paper. By
varying the length of the loop between
45 and 60, one can choose any of 16
different depths for the image.
Surprisingly, it turns out that it
is quite simple to mould a 3-D shape
from these 16 depths, just by suitably
varying the length of the loop along
each line of the picture.
All one has to do is to specify the
required depth, and hence loop length,
for each pixel position in the
picture. If the loop is required to be
x digits shorter, the next x digits
are removed from the loop and the
resultant gap closed up. If the loop
is required to be x digits longer, a
gap is opened up and an extra x random
digits inserted.
IMAGE DESIGN
The formula used for defining the
image calculates the depth, D%, for
the screen position, X% Y%. D% should
be between the limits, 0 (foreground)
to 15 (background), corresponding to
loop lengths from 45 to 60. When we
define the loop length at any
particular spot in the picture, we are
really defining the position which
will be seen by the right eye.
The corresponding position seen by
the left eye will be 45 to 60 pixels
to the left and the resultant position
perceived by the brain will be midway,
i.e. between 22 and 30 pixels to the
left. I have found that an adequate,
although not perfect, correction for
this can be made by assuming the
centre line of the picture to be at
X%=740 instead of X%=640.
NOTES ON USING THE PROGRAMS
Before using MAGIC3D both programs
should be copied onto a separate blank
disk - so that there is room for
screen dumps and any amended versions
of MAGIC3D that you may want to
experiment with. This is not essential
however if you just want to take
printed copies from the two sample
screen dumps.
The first line of each program is
*RAMOFF. This is essential if you have
a shadow RAM. Otherwise the line
should be deleted to prevent it
producing an error message.
EDITOR........ For compatibility I have
added line 5 to both programs. This
line runs the program from line 20 if
the *ROMOFF instruction is not
recognised by your machine.
The screen dump produced by MAGIC3D
is always called PICTURE. You will
need to rename each dump before
producing another - otherwise you will
find it overwritten!
POSSIBLE ENHANCEMENTS
With suitable modifications MAGIC3D
can be made to work in MODE 1 and
produce a pattern in four colours
instead of just black and white. I have
not pursued this possibility because I
don't have a colour printer.
Alternatively it can be modified to
produce a MODE-0 picture. There will
then be twice as many pixels in each
line and the loop length can vary from
90 to 120, giving 31 graduations of
depth. The resultant image is smoother
but, to my mind, loses some of the
impact of the coarser MODE-4 picture -
and the time taken to compose the
picture is much longer.
Although it is easy to define a
mathematically derived image it is
much more difficult to define, say, a
dinosaur or an elephant. Conceivably
such an image could be defined using a
MODE-2 picture in which each of the 16
possible colours represented a
different depth - but I don't think I
shall be attempting this!
To: 999 (all members)
From: K2K (Peter Davy)
Subject: PALINDROMIC NUMBERS
I could not resist K3X's challenge
in the no.34 issue to write a program
to find palindromic numbers whose
squares are also palindromic.
Elsewhere on this disk you will find
the program LAPPY which is my final
solution to this problem. You should
now find LAPPY and produce a listing
on the printer to look at as you read
this article.
It is evident from the example quoted
by K3X (101010101 squared equals
10203040504030201) that we need to go
well beyond the normal arithmetic
capability of the Beeb i.e. 9
significant figures. I remembered
having read somewhere about a program
which enables arithmetic to be
performed on string representations of
long numbers. A search through my
quite large collection of BBC micro
books eventually turned up Utility 31:
Multiple Precision Arithmetic in the
book "INVALUABLE UTILITIES for the BBC
MICRO" by Jeff Aughton, published by
PAN in 1984.
Utility 31 takes the form of a
procedure PROCfermat which calls up
several other procedures. The complete
utility enables addition, subtraction,
multiplication and division. In the
version which appears at the end of my
program LAPPY I have only included the
parts that are needed for
multiplication.
If two numbers are assigned to A$ and
B$, for example A$="98765432123456789"
and B$="12345678987654321", PROCfermat
will produce the product of the two
numbers as C$. For PROCfermat to work
the arrays A%( ), B%( ) and C%( ) must
be dimensioned 255. This is done at
line no.15.
My first attempt was to start with the
smallest two digit number, 10 and to
repeatedly add one to it. Each answer
was turned back to front, i.e.
reversed and then compared with the
original. If the two were the same a
palindromic number had been generated,
and it was squared and the same
operations applied to see if the
square was palindromic. This program
worked all right but it was far too
slow. I had set my sights on going up
to at least 999999999 for the numbers
being squared. But 999999999 is a very
big number. 999999999 seconds is more
than 31 years! Even if numbers are
generated at the rate of 10 to the
second it would take 3 years to get
from 10 to 999999999.
I decided to try to generate the
palindromes for squaring, directly.
For example the smallest 6 digit
palindrome is 100001 which could be
made by joining together 100 and
100-reversed i.e. joining 100 and 001.
The next six digit palindrome is
101101 made by joining 101 and 101.
Then comes 102201 by joining 102 and
201. Now the program only needs to add
1 repeatedly to 100 until 999 is
reached to generate all the six digit
palindromes from 100001 to 999999. For
numbers with an odd number of digits
things are slightly different. For
example the smallest 7 digit
palindrome is 1000001. This and
subsequent palindromes can be built up
as follows:
1000001 100 + 0 + 001
1001001 100 + 1 + 001
1002001 100 + 2 + 001
and so on to:
1009001 100 + 9 + 001
1010101 101 + 0 + 101
1011101 101 + 1 + 101
1012101 101 + 2 + 101
and so on to:
1019101 101 + 9 + 101
1020201 102 + 0 + 201
and so on to:
9999999 999 + 9 + 999
Testing a number to see if it is a
palindrome by reversing it and then
comparing with the original is slow.
In PROCtest I have used a quicker way
of comparing the first and last digits
and then the second and last but one
and then the third and last but two
and so on but stopping as soon as a
mis-match is found indicating that the
number is not palindromic.
The values of 2 and 9 in line 20 means
that all palindromes in the range of 2
digits to 9 digits will be generated
i.e. from 11 to 999999999.
Before running the program, press
CTRL-N to put into paged mode. Then
type RUN and press RETURN. All the
palindromes generated will scroll past
on the screen and those which have
palindromic squares are sent to the
printer. To keep the program running
you will need a weight to hold down
one of the SHIFT keys. To stop the
output to examine it more closely,
just take the weight off. It took my
Master-128 about 19 hours to generate
all the palindromes between 11 and
99999999 and 67 of them have
palindromic squares.
The print-out is interesting but I
will say no more about it. You can run
the program for yourself.
If you would like to put PROCfermat
through its paces, RUN the program for
a short time then press ESCAPE. Now
type A$="12345678987654321" and press
RETURN and type B$="98765432123456789"
and press RETURN. Any other big
numbers will work just as well.
Now type PROCfermat:PRINT C$ and press
RETURN. The product of those two big
numbers will appear on the screen.
My thanks to K3X for raising this
matter. What's the betting that
someone has sent in six lines of code
which does the job in five minutes?
Editor......... The program if run from
the 8BS menu does not produce anything
readable on the screen, so I have added
a MODE 7 instruction at the start of
"Lappy". When running programs such as
this, I use a piece of sellotape stuck
to the SHIFT key, pulled taut and
pressed down on to the front edge of
the computer. This holds the key down
much better than a coffee mug.