0% found this document useful (0 votes)
2 views

dot1985e-large-numbers

The document discusses the limitations of calculators and computers in performing arithmetic with large integers, highlighting issues with accuracy and representation. It introduces methods for extending BBC BASIC to handle large numbers accurately by using string representations and machine code for faster calculations. Additionally, it outlines algorithms for factorization and finding the highest common factors of large integers, emphasizing the challenges and time requirements involved in these operations.

Uploaded by

6dark6elf6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

dot1985e-large-numbers

The document discusses the limitations of calculators and computers in performing arithmetic with large integers, highlighting issues with accuracy and representation. It introduces methods for extending BBC BASIC to handle large numbers accurately by using string representations and machine code for faster calculations. Additionally, it outlines algorithms for factorization and finding the highest common factors of large integers, emphasizing the challenges and time requirements involved in these operations.

Uploaded by

6dark6elf6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Now that calculators ore readily available they ore sometimes used in

classrooms to explore patterns in sequences of numbers. lt has been


pointed out that some patterns such os
1 1 X1 1 = 121 1 1 1 X1 1 1 = 12321 1 1 1 1 * 1 1 1 1 = 1 234321 ...
only really become interesting when the numbers get too big for a
calculator to handle accurately. Computers ore usually no better but, in
the article that follows, David Tall explains that he has the answer.

Arithmetic with large numbers


odem microcomputers do not usually cope largest integer that can be represented is 231 - 1. This

M well with the arithmetic of large integers;


can give unsatisfactory results. Even in the accepted
integer range the numbers are not printed accurately in
instead they store and display numbers to an
decimal notation.
accuracy of a few digits, with inevitable rounding
errors. This is a consequence of the way A%=2j31- 1: PRINT A%
computer languages represent and process
numbers, rather than an inherent weakness of the gives the expression
2.14748365E9
computer. By using a different representation it is
possible to obtain accuracy to any specified
instead of the exact answer
degree, subject only to the limitations of available
memory. In this article I shall show how BBC 2,147,483,647.
BASIC may be extended to calculate exact
A%=2 i 31- 1 :PRINT 2*A% : PRINT A%+A%
sums, differences, products, quotients,
remainders and powers for whole numbers in the gives two radically different answers:
range±. 10 254 and shall demonstrate how this
facility can be used to factorize large numbers. 2*A %=4.29496729E9

and
Normally BBC BASIC can only cope with integers in
the range ±(231-1). The command A%+A%= -2.

A%=2j31-1
The difference arises from an error in the BASIC
gives an integer variable its maximum value whilst
interpreter. Integer arithmetic is used wherever
A%=2j31 possible, and the interpreter switches to floating-point
routines when integers become inappropriate. The
produces the error message 'Too big'. computation 2*A% clearly exceeds the maximum
The technical reason for this is that integers are integer size and is performed in floating-point
'
stored internally in four memory locations, each of arithmetic. But A%+ A% is erroneously carried out as
' which can hold an eight-digit binary number. The an integer calculation, with the sum of two thirty-one­
number of binary digits available is therefore thirty- digit binary numbers producing an error in the thirty­
48
two. One of them is used to represent the sign. Thus the second place. This sets the minus sign, but fails to

Autumn 1985 MICROMm'B


methods have been devised independently by Trevor PRINT FNadd("123123123","15452345")
Fletcher and Joe Watson. Joe's package is available on
disk from Keele Department of Education for £5 . The immediately prints out the result of adding the
central idea is to hold the number in memory as a string, numbers contained in the strings. Corresponding
giving a: maximum length of 255 digits. To perform the results will be obtained from FNsubtract and
arithmetic the strings of digits are �roken into FNmultiply.
manageable chunks (four digits at a time, say) which
are turned into numbers so that they can be operated on PRINT FNpower ("2","200")
using ordinary BASIC arithmetic. The results are then
reassembled to give the answer as another long string of gives the calculation of2200 mentioned earlier.
digits. Addition and subtraction are fast; multiplication Calculating the quotient or remainder in a division
and division are a little slower. The methods are ideal involves producing both answers simultaneously.
for the demonstration of what can be done in BASIC and Whichever is calculated first, 'FNother' can be used to
are quite satisfactory for normal arithmetic. But time obtain the other one. Thus
becomes a problem when a large number of operations is
required. In such circumstances a method of speeding PRINT FNquotient("14","3") : PRINT FNother
up the calculations becomes highly desirable.
Faster arithmetic is made possible by writing the gives the quotient, 4 , followed by the remainder, 2 ,
routines in machine code. The 6
' 502 processor' that whilst
drives the BBC M icrocomputer includes routines for
decimal arithmetic which are totally ignored by BBC PRINT FNremainder("31","5") : PRINT
BASIC. The program given on the disk available in FNother
connection with this issue of MICROMATH uses these
neglected resources to provide arithmetic. for large gives the remainder, 1 , followed by the quotient, 6 .
numbers. Functions may be combined; typing
·I shall illustrate some of the operations that can be
carried out with large numbers. PRINT FNsubtract (FNpower("2","31"),"1")

To load the procedures into your computer, type reveals the true value of2 i 31- 1.

Values may be held in integer strings, so that the


CHAIN " BIGNUM" previous calculation can be performed in three steps, as
follows:
Typing
P$=FNpower("2"."31") : A$=FNsubtract(P$,"1"):
PROCcalculate PRINT A$

turns the computer into a simple large-number with the obvious bonus that the result of A$ is stored in
calculator that also prints the time taken for each memory, should it be required again.
operation. For instance, the value of 2200 will be A number of useful procedures for factorizing large
printed as numbers are included in the program. One of these is for
the highest common factor of two large integers and has
160693804425899027554196209234116260 exactly the same form as THE BASIC procedure which
2522202993782792835301376 can be written to find the highest common factor of two
integers of normal size.
in just 0 .15 seconds
DEF FNgcd(X%,Y%) : IFX%=0 THEN=Y%
Investigations confirm that the longer the strings are, ELSE=FNgcd(Y%M ODX%,Y%)
the longer it takes, and addition and subtraction are
much faster than the other operations. gives the highest common factor (or greatest common
Playing with vast numbers may soon pall, as divisor) of the two integers, X% and Y%, of normal size.
meaningless strings of large digits are flashed up before Typing
your eyes, though you may get some perverse pleasure
from causing calculation overflow even with these large PRINT FNgcd(3108 ,4095 ) 49

MICROMATH Autumn 1985


will give the greatest common divisor of 3108 and 4095 Here are results of a few typical runs.
as 21.
A function for finding the highest common factor of number=120
two large integers can be defined having exactly the factors= 2*2*2*3*5
same structure: time taken=0.2 seconds

DEF FNhcf(X$, Y$) : IF X$="0" THEN=Y$ number=1024


ELSE=FNhcf(FNremainder(Y$, X$), Y$) factors= 2*2*2*2*2*2*2*2*2*2
time taken=0.37 seconds
and gives a fast and accurate result even with very large
numbers. number= 243912827
Finding the factors of a single number is a different factors=7591*30677
kettle of fish. The machine code is much faster than the time taken=212.61 seconds
BASIC routines, but even on a very fast main-frame the
time taken for calculating factors increases The larger the prime factors, the longer it takes. The
exponentially with size. It has been estimated that the arithmetic operations for large numbers always take at
factorization of a random 200-digit number on a main­ least 1/100 of a second and often take much longer. Even
frame computer could take 3.8 billion years , so do not at 1/100 of a second per search it would require one
expect miracles! The simple routine to be described second to try 100 numbers and lOl
l - 2 seconds to try
copes with numbers containing less than twenty digits lOll numbers. A number containing a prime factor with
in a reasonable time. forty digits would cause the search to continue until the
An elementary algorithm for finding factors uses the divisor had more than twenty digits. This would take
property that, if a number n has a factor it has one which far in excess of 1018 seconds or about thirty-billion
does not exceed the square root of n. The function years.
FNfactor (X%), which can be used for normal numbers , Should you allow your fingers to type a random forty­
seeks factors of X% by starting at 2 and trying digit number for the computer to factorize you may
successively each number until the square root of X% is reach an impasse to which the only solution is to press
reached. When a factor is found it is checked to see if it ESCAPE.
repeats: the quotient is calculated and this searched for
further factors. The square root of the quotient now An attempt to
provides a new maximum for any remaining factors;
because this is less than the square root of X% the PRINT FNfactors(FN power("2","200"))
extent of the search is reduced. The function
FNfactor(X%)produces a string containing the product fails for another reason: the string of factors
of the prime factors:
2*2*2*....
PRINT FNfactor(120)
becomes 'too long'. The alternative routine
yields the string FNpowerfactors copes better by grouping together
powers of a given prime number.
"2*2*2*3*5".
PRINT FNpowerfactors("120")
while
gives
F$=FNfactor( X%): PRINT EVAL F $
(2 i 3)*3*5

evaluates the product o f the factors and gives the so that


original number, X%. PRINT FNpowerfactors(FNpower("2","200"))
FNfactors(X$)is used to find factors of large numbers
by an analogous process. No square-root function is successfully yields the response
available, but the length of the string can be used
instead: if a number n has a factor, it must have one (2 i 200).
whose length does not exceed half the length of n.
You can gain an idea of the time this method takes to The routines may easily be used in other programs. The
calculate factors by using the routine: exact way in which this can be done is described in the
notes accompanying the disk. •
REPEAT : INPUT "number="X$ : TIME=O:
PRINT "factor=" FNfactors(X$): David Tall works at the Mathematics Education Research
PRINT "time taken="; TIME/100; Centre, University of Warwick
"seconds" : UNTIL FALSE

Autumn 1985 MICROMATH

You might also like