solver_common.hpp 745 Bytes
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
#ifndef CK_SOLVER_COMMON_HPP
#define CK_SOLVER_COMMON_HPP
3

Chao Liu's avatar
Chao Liu committed
4
5
namespace ck {
namespace driver {
6

7
8
// greatest common divisor, aka highest common factor
inline int gcd(int x, int y)
9
{
10
    if(x < 0)
11
    {
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
        return gcd(-x, y);
    }
    else if(y < 0)
    {
        return gcd(x, -y);
    }
    else if(x == y || x == 0)
    {
        return y;
    }
    else if(y == 0)
    {
        return x;
    }
    else if(x > y)
    {
        return gcd(x % y, y);
    }
30
31
    else
    {
32
33
34
        return gcd(x, y % x);
    }
}
35

36
37
38
39
40
41
42
template <typename X,
          typename... Ys,
          typename std::enable_if<sizeof...(Ys) >= 2, bool>::type = false>
auto gcd(X x, Ys... ys)
{
    return gcd(x, gcd(ys...));
}
43

Chao Liu's avatar
Chao Liu committed
44
45
} // namespace driver
} // namespace ck
46
#endif