1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
/// Greatest Common Divisor
///
/// Return the greatest common divisor of two integers.
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// A I Any integer
/// B I Any integer
///
/// The function returns the greatest common divisor of A and B.
/// ```
///
/// # Detailed Input
///
/// ```text
/// A is any integer.
///
/// B is any integer.
/// ```
///
/// # Detailed Output
///
/// ```text
/// The function returns the greatest common divisor of A and B.
/// ```
///
/// # Exceptions
///
/// ```text
/// Error 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.
/// ```
///
/// # Particulars
///
/// ```text
/// This 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.
/// ```
///
/// # Examples
///
/// ```text
/// A 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
/// ```
///
/// # Literature References
///
/// ```text
/// [1] D. Knuth, "The Art of Computer Programming Vol 1. --
/// Fundamental Algorithms," 2nd Ed., Addison-Wesley, 1973
/// ```
///
/// # Author and Institution
///
/// ```text
/// J. Diaz del Rio (ODC Space)
/// W.L. Taber (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB 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)
/// ```
pub fn gcd(a: i32, b: i32) -> i32 {
let ret = GCD(a, b);
ret
}
//$Procedure GCD ( Greatest Common Divisor )
pub fn GCD(A: i32, B: i32) -> i32 {
let mut GCD: i32 = 0;
let mut REMNDR: i32 = 0;
let mut ABSA: i32 = 0;
let mut ABSB: i32 = 0;
let mut P: i32 = 0;
let mut Q: i32 = 0;
//
// Local variables
//
ABSA = i32::abs(A);
ABSB = i32::abs(B);
if (ABSA > ABSB) {
P = ABSA;
Q = ABSB;
} else {
P = ABSB;
Q = ABSA;
}
REMNDR = 1;
if (Q != 0) {
while (REMNDR != 0) {
GCD = Q;
REMNDR = (P - ((P / Q) * Q));
P = Q;
Q = REMNDR;
}
} else {
GCD = P;
}
GCD
}