Fractions

Publication

Pub Details

Date

Pages

BASIC Terms

See all articles from CATS v6 n1

It is interesting to see the computer work in true fractions instead of converting everything to decimal numbers.

This program first displays a blank equation:

0/0 # 0/0 = ?

Each unknown flashes, in turn, for entry of a number operator.

Lines 55 to 80 provide the necessary cross-multiplication. The result is available as X and Y in line 85, but the numbers may be large.

Finding the least-common-denominator is the real programming problem. To test all the possibilities one by one is slow, so lines 90 to 125 limit the search to submultiples of X.

Eventually this may become counter-productive; if further search is needed it reverts to one-by-one testing for the rest of the way. The result is displayed as:

203/300 + 103/200 = 1 23/120

I have worked out a machine language program which speeds it up further for large numbers.

Products

 

Media

 

Image Gallery

Source Code

   10 REM ***** FRACTIONS
   15 DIM Z(5): LET Q$="#"
   20 FOR n=1 TO 6
   25 PRINT AT 25-PEEK 23689,0; FLASH (n=1);z(1); FLASH 0;"/"; FLASH (n=2);z(2); FLASH 0;" "; FLASH (n=3);q$; FLASH 0;" "; FLASH (n=5);z(5); FLASH 0;" = ";flahs(n=6);"?"; FLASH 0;CHR$ 8;
   30 IF n=6 THEN GO TO 50
   35 IF n=3 THEN INPUT Q$: LET z(3)=CODE q$: GO TO (50-15*(q$ >="0"))
   40 INPUT q: LET z(n)=SGN q*INT ABS (q): GO TO (50-10*(z(n)=0))
   50 NEXT n
   55 LET a=z(1)*z(5)
   60 LET b=z(2)*Z(4)
   65 LET c=z(2)*z(5)
   70 LET d=z(1)*z(4)
   75 LET x=a+b*(q$="+")-b*(q$="-")+(d-a)*(q$="*")
   80 LET y=c+(b-c)*(z(3)=47)
   85 LET x=SGN y*x: LET y=ABS y
   90 REM *** Least Common Denom
   95 LET a$=("-" AND x<0)
  100 LET x=ABS x: LET a$=a$+(STR$ INT (x/y)+" " AND x >= y)+("0" AND x=0): LET x=x-INT (x/y)*y  
  110 FOR j=1 TO x: LET z=INT (x/j)
  115 IF z=INT (x/(j+1)) THEN FOR j=z TO 1 STEP-1: LET z=j
  120 IF INT (x/z)=x/z AND INT (y/z)=yz THEN GO TO 130
  125 NEXT j
  130 LET a$=a$+(STR$ (x/z)+"/"+STR$ (y/z) AND x)
  135 PRINT a$: STOP: GO TO 10

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

Scroll to Top