gcd |
Table of contents
ProcedureGCD ( Greatest Common Divisor ) INTEGER FUNCTION GCD ( A, B ) AbstractReturn the greatest common divisor of two integers. Required_ReadingNone. KeywordsMATH NUMBERS DeclarationsIMPLICIT NONE INTEGER A INTEGER B Brief_I/OVARIABLE I/O DESCRIPTION -------- --- -------------------------------------------------- A I Any integer B I Any integer The function returns the greatest common divisor of A and B. Detailed_InputA is any integer. B is any integer. Detailed_OutputThe function returns the greatest common divisor of A and B. ParametersNone. ExceptionsError free. 1) If both A and B are zero, then GCD returns zero. 2) If exactly one of A and B is zero, then GCD is by definition the maximum of the absolute values of A and B. FilesNone. ParticularsThis routine uses Euclid's Algorithm to find the greatest common divisor (GCD) of the integers A and B. In other words the largest integer, G, such that A = k*G for some k and B = j*G for some G. Note if either A or B is zero, then we return the maximum of the two integers ABS(A) and ABS(B). If one is non-zero we have just what the definition says. If both are zero the definition above does not give us a GCD, so we take the GCD of 0 and 0 to be 0. ExamplesA B GCD ----- ----- ----- 8 4 4 120 44 4 15 135 15 101 97 1 119 221 17 144 81 9 0 111 111 0 0 0 RestrictionsNone. Literature_References[1] D. Knuth, "The Art of Computer Programming Vol 1. -- Fundamental Algorithms," 2nd Ed., Addison-Wesley, 1973 Author_and_InstitutionJ. Diaz del Rio (ODC Space) W.L. Taber (JPL) VersionSPICELIB Version 1.1.0, 06-JUL-2021 (JDR) Added IMPLICIT NONE statement. Edited the header to comply with NAIF standard. SPICELIB Version 1.0.1, 10-MAR-1992 (WLT) Comment section for permuted index source lines was added following the header. SPICELIB Version 1.0.0, 31-JAN-1990 (WLT) |
Fri Dec 31 18:36:23 2021