'=========================================================================== ' Subject: SOLVING LCM AND GCD Date: 10-28-95 (12:00) ' Author: The ABC Programmer Code: QB, QBasic, PDS ' Origin: Computer Science Book Packet: ALGOR.ABC '=========================================================================== '========================================== ' Finding the Lowest(Least) Common Multiple ' and the Greatest Common Divisor ' Programmed by William Yu (10-28-1995) ' ' This program helps, where calculators ' can not compute the exact fractions. '========================================== DECLARE SUB FindLCM (Numbers(), Max, X) DECLARE SUB FindGCD (Numbers(), Max, X) DECLARE SUB AddFractionDemo (Numbers(), Max, Denominator) DIM Numbers(10) CLS ' Keeping the numbers simple to avoid long waits Numbers(1) = 28 ' You can modify/add/remove as you wish Numbers(2) = 42 ' If you add or remove one or more numbers Numbers(3) = 126 ' make sure you do the same for the AddFractionDemo Numbers(4) = 98 ' Numerators() Max = 4 ' Change this to correspond to Max Numbers() ' and Max Numerators() PRINT "The LCM for"; : COLOR 15 FOR I = 1 TO Max PRINT Numbers(I); NEXT I COLOR 7: PRINT "is"; : COLOR 15 FindLCM Numbers(), Max, X Denominator = X: PRINT X COLOR 7: PRINT "The GCD for"; : COLOR 15 FOR I = 1 TO Max PRINT Numbers(I); NEXT I COLOR 7: PRINT "is"; : COLOR 15 FindGCD Numbers(), Max, X PRINT X AddFractionDemo Numbers(), Max, Denominator SUB AddFractionDemo (Numbers(), Max, Denominator) ' Adding fractions using LCM as our Denominator ' Can you think of another way to add fractions? ' There is, but is not commonly used because it can be tedious doing by ' hand, and even on the computer it is quite slow. ' But the other way of adding fractions is to multiply all the denominators ' to get your grand total. And then add your numerators. Now that you ' have two great big numbers to work with, you find the GCD to reduce ' the fraction. Try it, which one is faster? Using LCM or GCD? ' Obviously GCD works best if you cannot find the LCM, but sometimes it ' will not work because the numbers are too large. LCM is your best bet. DIM Numerator(10), Fraction(2) Numerator(1) = 22 Numerator(2) = 3 Numerator(3) = 35 Numerator(4) = 4 PRINT : PRINT "Add Fraction Demo:"; : COLOR 7: PRINT : PRINT PRINT " "; FOR I = 1 TO Max PRINT USING "###"; Numerator(I); : PRINT " "; NEXT I PRINT PRINT " "; FOR I = 1 TO Max PRINT "---"; COLOR 15: IF I <> Max THEN PRINT " + "; COLOR 7 NEXT I PRINT " = ----" PRINT " "; FOR I = 1 TO Max PRINT USING "###"; Numbers(I); : PRINT " "; NEXT I PRINT Denominator ' Next, we calculate the numerator FOR I = 1 TO Max Numerator = Numerator + (Denominator / Numbers(I) * Numerator(I)) NEXT I PRINT : PRINT " "; Numerator PRINT " ---- = Find GCD": PRINT " "; Denominator Fraction(1) = Numerator ' Plug in the numbers Fraction(2) = Denominator ' to be reduced FindGCD Fraction(), 2, X ' Find the GCD to reduce the fraction Numerator = Numerator / X ' Divide by the GCD Denominator = Denominator / X PRINT : PRINT " "; Numerator PRINT " ---- = Divided by GCD": PRINT " "; Denominator I = 0 IF Numerator > Denominator THEN ' Let's convert this to mixed DO ' fraction I = I + 1 Numerator = Numerator - Denominator LOOP UNTIL Numerator <= Denominator END IF ' We are finally done! Let's print the answer. PRINT : PRINT " "; Numerator: PRINT ; " "; IF I > 0 THEN PRINT I; ELSE PRINT " "; PRINT "---- = Answer": PRINT " "; Denominator END SUB SUB FindGCD (Numbers(), Max, X) HighestNumber = Numbers(1) HoldX = 1 C = 1 FOR I = 2 TO Max IF Numbers(I) = Numbers(1) THEN C = C + 1 IF Numbers(I) > HighestNumber THEN HighestNumber = Numbers(I) / 2 NEXT I IF C = Max THEN X = Numbers(1): EXIT SUB X = 2: I = 1 DO IF Numbers(I) MOD X THEN ' Check for Remainder X = X + 1 I = 1 ELSE ' No remainder, passed I = I + 1 END IF IF I = Max + 1 THEN HoldX = X: I = 1: X = X + 1 IF X = HighestNumber + 1 THEN EXIT DO LOOP X = HoldX END SUB SUB FindLCM (Numbers(), Max, X) X = Numbers(1): I = 1 DO IF INT(X / Numbers(I)) = X / Numbers(I) THEN ' Are they really equal? IF I = Max THEN ' Last number checked is EXIT DO ' equal, end do..loop ELSE I = I + 1 ' Equal, check next number IF I = Max + 1 THEN I = Max END IF ELSE ' No they're not X = X + 1: I = 1 ' Start from beginning END IF ' Incr X (Slow) LOOP END SUB