PSCF v1.1
gcd.h
1#ifndef UTIL_GCD_H
2#define UTIL_GCD_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <stdlib.h>
12
13namespace Util
14{
15
30 inline int gcd(int a, int b)
31 {
32 a = abs(a);
33 b = abs(b);
34 if (a == 0) return b;
35 if (b == 0) return a;
36 int c;
37 // If b > a, swap such that b < a
38 if (b > a) {
39 c = a;
40 a = b;
41 b = c;
42 }
43 // Euclid's algorithm, for a > b > 0
44 while (b !=0) {
45 c = a % b;
46 a = b;
47 b = c;
48 }
49 return a;
50 }
51
52}
53#endif
int gcd(int a, int b)
Compute greatest common divisor (gcd) of two integers.
Definition: gcd.h:30
Utility classes for scientific computation.
Definition: accumulators.mod:1