Header | Description |
---|---|
#include <gcd.h> | The default header. This header will, in turn, include one of the two headers below. As shipped, this would be <gcdEuclid.h>, but this may be changed when the package is installed. |
#include <gcdEuclid.h> | This header implements the usual Euclidean algorithm for GCD [1]. |
#include <gcdRSB.h> | Implements GCD using the Right Shift Binary algorithm. While this can be much faster than the Euclidean algorithm, it has a worst case runtime that is much worse [2]. |
Name | Type | Description |
---|---|---|
a, b | T | The numbers. Note that they must be of the same type [3]. |
#include <nttl/gcd.h> ... long d = GCD( 1235, 2325 ); |
[1] | See [Knuth2], pp 316-320. | |||
[2] | See [Sorenson1]. This is also mentioned in [Knuth2], pp.321-323, and other places, but the paper by Sorenson is the easiest to follow. | |||
[3] | In general, the requirement that both arguments be of the same type is
not a problem. For example, lets assume that you are using the large number class
ln, and want to do the following:
This will result in a compile-time error, since the types of a and b are not the same. There are two possible solutions here (and in general): cast b up to type ln:
or reduce a down to a long:
In practice, reducing a down to a long is probably the more efficient solution. |
|||