number_gcd — Computes the greatest common divisor.
result = number_gcd ( m , n ) result = number_gcd ( m , n , method )
a 1x1 matrix of floating point integers
a 1x1 matrix of floating point integers
a 1x1 matrix of strings, the algorithm to use (default method=euclidian)
a 1x1 matrix of floating point integers, the least common multiple
Returns the greatest common divisor of m and n.
The following values of method are availble.
Uses a naive Euclid's recursive algorithm.
Uses a brute-force method, based on systematic search for divisors.
Uses a naive binary recursive method. This is similar to Algorithm B in section 4.5.2 of TAO.
Uses Euclid's algorithm with a loop (instead of recursive).
Uses binary algorithm with a loop (instead of recursive).
number_gcd ( 84 , 18 ) // 6 number_gcd ( 18 , 84 ) // 6 // With negative integers computed = number_gcd ( 18 , -84 ) // 6 // Test all algos algos = [ "euclidian" "brute" "binary" "euclidianiterative" "binaryiterative" ]; mprintf(" %s\n", strcat(algos," ")); for m = 1 : 20 for n = 1 : 20 msg=""; msg=msg+msprintf("gcd(%d,%d) = ",m, n); for method = algos' d = number_gcd (m,n,method); msg=msg+msprintf("%s ",string(d)); end msg=msg+msprintf("\n"); mprintf("%s\n",msg); end end