P37 Quadratic Equations


In this program we have to solve;

Ax*x + Bx + C = 0(I)

To do this we use the formula;

x=-B ± SQR(B*B - 4*A*C)/(2*A)(II)

This gives the two roots of (I). There are, however, some problems:


Note that because of roundoff errors, this algorithm is unsuitable for the case where B*B is very much greater than 4*A*C.

COMMANDS

Key in program and type RUN.
Enter coefficients in the correct order when prompted.


  100 REM Program P37 - Quadratic Equations
  190 MODE 6
  200 PRINT "This  program solves  equations of the"
  210 PRINT "form A*x*x + B*x + C = 0, thus you are"
  220 PRINT "required to input the 3 parameters in"
  230 PRINT "the correct order"
  240  
  250 INPUT''"What is the coefficient of x*X",A
  260 INPUT''"What is the coefficient of x",B
  270 INPUT''"What is the constant term",C
  280  
  290 REM The next section of code catches the case of A=0
  300  
  310 IF A=0 THEN PRINT (("Solution to"'';B;"x+";C;"=C"''"is
"''"x=";(-C/B)'':END
  320  
  330 REM Notice the use of the line feed character ' and th
e ; to format the output
  340 REM Once a solution has been calculated then the progr
am ends
  350  
  360 REM Before continuing we evaluate the discriminant
  370  
  380 dis=B*B-4*A*C
  390  
  400 IF dis=0 THEN PRINT ''"We have equal roots."''"The sol
ution is x=";(-B/2/A)'':END
  410 REM Again we use ' and ; to format the output
  420  
  430 IF dis>0 THEN PRINT ''"We have 2 real roots"''"Root-1:
 x=";((-B+SQR(dis))/2/A)''"Root-2: x=";((-B-SQR(dis))/2/A)''
:END
  440  
  450 REM In the next section of code we format the output b
y using multiple print lines
  460  
  470 PRINT ''"We have complex roots"
  480 PRINT''"Root-1 =";(-B/2/A);"+i*("(SQR(-dis)/2/A)")"
  490 PRINT''"Root-2 =";(-B/2/A);"-i*("(SQR(-dis)/2/A)")"''
  500 END

RUN
This  program solves  equations of the
form A*x*x + B*x + C = 0, thus you are
required to input the 3 parameters in
the correct order


What is the coefficient of x*x?1


What is the coefficient of x?2


What is the coefficient term?1


We have equal roots.

The solution is x=-1


P38 Factorization


It is an interesting problem in mathematics that, given any whole number, we can find all its prime factors.

That is, given any positive integer N, we wish to express it as a product of powers of primes.

For example:

    180=22*32*5

We use a method of repeated division to find the set of factors of N.

Let us consider an example: find the prime factors of 180

The first possible factor of 180, is 2, and we can write

180=2*90

Now all the factors of 90 are contained in 180, so we need only consider the factors of 90.

180=2*2*45

Thus 2 is also a factor of 90, but 2 is not a factor of 45 because it does not divide evenly into 45, that is REMAINDER(45/2)?0

We can now try 3

180=2*2*3*15

and carrying on:

180=2*2*3*3*5

Thus

180=22*32*5

That, then, is the algorithm used by the program presented here.
Note that when the factors are presented, the first prime is given as 1; this was just to aid the presentation of the result.

COMMANDS

Key in program and type RUN.
Enter number to be factorized.

  100 REM Program P38 - Factorization
  110 MODE 6
  120  
  130 PRINT''"This program can be used to factorize a"
  140 PRINT "positive integer into its prime factors."
  150 PRINT'"The  program,  at the  moment only uses"
  160 PRINT "primes below 100"'''
  170  
  180 dimension=100:REM This is the line to amend to increas
e the range of the program
  190  
  200 DIM factor(dimension),indices(dimension)
  210  
  220 INPUT "What number do you want factorized",number
  230  
  240 quotient=number
  250  
  260 REM The following section of code finds the indices
  270  
  280 FOR I=2 TO dimension
  290   REPEAT
  300     IF quotient MOD I=0 THEN factor(I)=1:indices(I)=in
dices(I)+1:quotient=quotient DIV I
  310   UNTIL quotient MOD I<>0
  320 NEXT I
  330  
  340 REM If there is a 1 in the Ith position of the factor 
array then I is a factor of the number
  350 REM and indices(I) is the index of that factor
  360 REM The next section of code writes out the factorizat
ion
  370  
  380 PRINT ''"1";
  390 FOR I=1 TO dimension
  400   IF factor(I)=1 THEN PRINT ;"*";I;"^";indices(I);
  410    
  420 NEXT I
  430 PRINT''
  440 END


RUN

This program can be used to factorize a
positive integer into its prime factors.

The program, at the  moment only uses
primes below 100



What number do you want factorized?352

1*2^5*11^1



P39 Factorial


In statistics we frequently wish to calculate expressions of the form

N*(N-1)*(N-2)...*3*2*1

For example, if we want to know the number of ways of arranging the word COMPUTER, then :

we have 8 ways of choosing the first letter
we have 7 ways of choosing the second letter
we have 6 ways of choosing the third letter
and so on

Thus, in total, we have

8*7*6*5*4*3*2*1=40320

ways of arranging the word COMPUTER.
Such objects are known as factorials and are defined as follows

N!=N*(N-1)*(N-2)*....*3*2*1

where ! is the symbol for factorial.

A more elegant definition of factorial is:

   N!=   ( N*(N-1)!   , N>1
      ( 1   , N=1.

It is this definition which is used in the following program.

Note that the largest factorial possible using this program is 33!: this is due to the way numbers are stored in computers.

COMMANDS

Key in program and type RUN.
Follow instructions.

  100 REM Program P39 - Factorial
  110 MODE4
  120 PRINT "This program can be used to evaluate"
  130 PRINT "the factorial of a number."
  140 PRINT''"The program uses the formula :"
  150  PRINT '"   N!={1 , if N=1 "
  160  PRINT  "      {n*(n-1)! , otherwise"
  170 PRINT''"Because of the limited range of numbers"
  180 PRINT "in computers, the largest factorial"
  190 PRINT "possible is 33!"
  200 REPEAT
  210   INPUT''"What is the number",number
  220   number=INT(number)
  230   IF number>33 THEN PRINT'"Number is too large":GOTO 2
60
  240   IF number<1 THEN PRINT'"Number is too small":GOTO 26
0
  250   PRINT''number"! = "FNfactorial(number)
  260   PRINT''"Do you wish to evaluate another"'"factorial"
;:INPUT answer$
  270   a$=LEFT$(answer$,1)
  280 UNTIL a$<>"Y" AND a$<>"y"
  290 END
  300 DEF FNfactorial(x)
  310 IF x>33 THEN =0
  320 IF x<1 THEN =0
  330 IF x=1 THEN result=1 ELSE result=x*FNfactorial(x-1)
  340 =result

 RUN
This program can be used to evaluate
the factorial of a number.

The program uses the formula :

   N!={1 , if N=1
      {n*(n-1)! , otherwise

Because of the limited range of numbers
in computers, the largest factorial
possible is 33!

What is the number?23

        23! = 2.58520167E22

Do you wish to evaluate another
factorial?NO


P40 Greatest Common Divisor


This program uses the Euclidean Algorithm to compute the
greatest common divisor of two natural numbers.

COMMANDS

Key in program and type RUN.
Enter numbers as positive integers.


  100 REM Program P40 - Greatest Common Divisor
  110 MODE 6
  120 PRINT ''"This program uses the Euclidean"
  130 PRINT "Algorithm to compute the greatest"
  140 PRINT "common divisor of two natural numbers."
  150 INPUT ''"Enter first number "X1% "Enter second number 
" X2%
  160 A%=X1%:B%=X2%
  170 IF A%<B% THEN T%=B%:B%=A%:A%=T%
  180 REM A is the larger number
  190  
  200 REPEAT
  210   remainder=A% MOD B%
  220   quotient=A% DIV B%
  230   A%=B%
  240   B%=remainder
  250 UNTIL remainder=0
  260  
  270 PRINT '''"The greatest common divisor of"'''
  280 PRINT STR$(X1%) " and " STR$(X2%) " is " STR$(A%)
  290 END


RUN

This program uses the Euclidean
Algorithm to compute the greatest
common divisor of two natural numbers.

Enter first number 36
Enter second number 14

The greatest common divisor of

36 and 14 is 2


P41 Secant Method


This program can be used to find a root of a function of a single variable. The secant method can be interpreted geometrically as follows.

Consider the diagram.

If xr and xr-1 lie on either side of a root, then we can draw the secant between the points (xr-1,(f(xr-1)) and (x,f(xr)). The secant cuts the x-axis at the point xr+1. Let xr+1 be the new approximation, then by similar triangles
The same relationship can be formed even if both approximations lie on the same side of the root.

COMMANDS

Key in program and type RUN.
Enter function and two initial approximations when required.
Enter accuracy when prompted.


100 REM Program P41 - Secant Method 110 ON ERROR PRINT "TRY AGAIN WITH DIFFERENT INITIAL VALS" :REPORT:END 120 MODE 6 130 PRINT "Finding a root to an equation using the" 140 PRINT "Secant Method" 150 PRINT "Enter function of X, using capital " 160 PRINT "leters."' 170 INPUT ''"Function = "f$ 180 DIM X(40):REM max of 40 iterates 190 INPUT "First point "X(0) 200 INPUT "Second point "X(1) 210 INPUT "Accuracy "acc 220 R=1 230 REPEAT 240 X=X(R) 250 fr=EVAL(f$) 260 X=X(R-1) 270 fr_1=EVAL(f$) 280 X(R+1)=X(R)-fr*(X(R)-X(R-1))/(fr-fr_1) 290 R=R+1 300 UNTIL R=40 OR ABS(X(R)-X(R-1))<acc 310 PRINT "Root is "X(R)" at the "STR$(R)"'th iterate" 320 END RUN Finding a root to an equation using the Secant Method Enter function of X, using capital letters. Function = X*X+2*X+1 First point -3 Second point 0 Accuracy .001 Root is -0.998533742 at the 16'th iterate


P42 Method of Bisections


The method of bisections is based on the use of sign changes to find a root of a function.

Consider the following diagram.

If we have two points xr,xr-1, for which f(xr) and f(xr-1) have opposite signs, then there is a root between xr and xr-1. We can then evaluate f(x) at the mid point between xr and xr-1,xm, say. If f(xm)=0 then we have a root. If SGNf(xm)) is the opposite to that of f(xr-1), then the root lies between xr-1 and xm. Otherwise the root lies between xr and xm.

That is the idea of the method of bisections, which is used in this program.

COMMANDS

Key in program and type RUN.
Enter function when prompted.
Enter end points of interval straddling root when prompted.
Enter accuracy desired.

  100 REM Program P42 - Method of Bisections
  110 ON ERROR PRINT "TRY AGAIN WITH DIFFERENT INITIAL VALS"
:REPORT:END
  120 MODE 6
  130 PRINT "Finding a root to an equation using the"
  140 PRINT "method of biections"
  150 INPUT ''"Function = "f$
  160 f$=FNcase(f$)
  170 INPUT "End point 1 = "ep1:a=ep1
  180 INPUT "End point 2 = "ep2:b=ep2
  190 INPUT "Accuracy = "acc
  200 REPEAT
  210   X=(a+b)/2
  220   f1=EVAL(f$)
  230   X=a
  240   fa=EVAL(f$)
  250   IF SGN(f1)=SGN(fa) THEN a=(a+b)/2 ELSE b=(a+b)/2
  260   X=(a+b)/2
  270 UNTIL EVAL(f$)=0 OR ABS(b-a)<acc
  280 d_ep1=SQR((ep1-a)^2+(ep1-b)^2)
  290 d_ep2=SQR((ep2-a)^2+(ep2-b)^2)
  300 IF d_ep2<acc OR d_ep1<acc THEN PRINT "No root found":E
ND
  310 PRINT "Root lies between "'a'"and"'b
  320 END
  330 DEF FNcase(a$)
  340 LOCAL d$,I,c$,c
  350 d$=""
  360 FOR I=1 TO LEN(a$)
  370   c$=MID$(a$,I,1)
  380   c=ASC(c$)
  390   IF c>96 AND c<122 THEN c$=CHR$(c-32)
  400   d$=d$+c$
  410 NEXT I
  420 =d$


P43 Trapezoidal Rule


This program uses the trapezoidal rule to evaluate a definite integral, of the form


Thus the program requires as input:

f(x)
a and b

A definite integral can be considered to be the area under the graph of a function. The trapezoidal rule approximates this area by a series of trapeziums as in the following diagram.

COMMANDS

Key in program and type RUN.
Enter details as prompted.


100 REM Program P43 - Trapezoidal Rule 110 MODE 6 120 INPUT "Function to be integrated",function$ 130 INPUT "Value of a",a,"Value of b",b 140 INPUT "Number of points",n 150 160 h=(b-a)/n 170 X=a 180 low_end_term=(EVAL(function$))/2 190 X=b 200 high_end_term=(EVAL(function$))/2 210 X=a 220 A=0 230 FOR I=1 TO n-1 240 X=X+h 250 A=A+EVAL(function$) 260 NEXT I 270 integral=(low_end_term+A+high_end_term)*h 280 PRINT "Integral=" integral


P44 Simpson's Rule


Simpson's Rule is a little bit more complicated algorithm than the trapezoidal rule. In Simpson's Rule we use a quadratic curve between the end points of the interval rather than a straight line. This leads to the following rule.

COMMANDS

Key in program and type RUN.
Enter function, a and b when prompted.
Enter number of points.
Note that if you enter an odd number, then 1 will be added.

  100 REM Program P44 - Simpson's Rule
  110 MODE 6
  120 INPUT "Function to be integrated", function$
  130 INPUT "Value of a",a,"Value of b",b
  140 INPUT "Value of n, must be even",n
  150 IF n MOD 2=1 THEN n=n+1:PRINT "Have added 1 to n to ma
ke it even"
  160 h=(b-a)/n
  170 X=a
  180 first_value=EVAL(function$)
  190 X=b
  200 last_value=EVAL(function$)
  210 X=a:even_vals=0:odd_vals=0
  220 FOR I=1 TO n-3 STEP 2
  230   X=X+h
  240   odd_vals=odd_vals + EVAL(function$)
  250   X=X+h
  260   even_vals=even_vals + EVAL(function$)
  270 NEXT I
  280 X=X+h
  290 odd_vals=odd_vals + EVAL(function$)
  300 integral=(first_value+last_value+4*odd_vals+2*even_val
s)*h/3
  310 PRINT "Integral= "integral