'=========================================================================== ' Subject: GREATEST COMMON DIVISOR (GCD) Date: 11-24-98 (22:29) ' Author: Jeff Connelly Code: QB, QBasic, PDS ' Origin: shellreef@email.msn.com Packet: ALGOR.ABC '=========================================================================== ' Created:09-22-98 ' By Jeff Connelly ' Calcuates the greatest common factor (GFC) of two numbers REM $DYNAMIC ' Dynamic array resizing ' These arrays initally contain only one element, will be REDIMed DIM aa&(0) DIM ab&(0) INPUT "Factor array size: ", size REDIM aa&(size) REDIM ab&(size) INPUT "First number: ", a INPUT "Second number: ", b ' Build the array of factors PRINT "Calculating factors of "; a; " and "; b; "..." free = UBOUND(aa&) FOR i = 2 TO a - 1 IF ((a / i) = INT(a / i)) THEN IF (free > UBOUND(aa&)) THEN EXIT FOR aa&(free) = i free = free - 1 END IF NEXT free = UBOUND(ab&) FOR i = 2 TO b - 1 IF ((b / i) = INT(b / i)) THEN IF (free > UBOUND(ab&)) THEN EXIT FOR ab&(free) = i free = free - 1 END IF NEXT FOR i = UBOUND(aa&) TO 0 STEP -1 ' go backwards IF (aa&(i) = 0) THEN EXIT FOR FOR j = UBOUND(ab&) TO 0 STEP -1 IF (ab&(j) = 0) THEN EXIT FOR IF (aa&(i) = aa&(j)) THEN PRINT "GCF found: ", aa&(i): SYSTEM NEXT NEXT PRINT "GCF not found, increase array size."