-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcd.hpp
39 lines (27 loc) · 784 Bytes
/
gcd.hpp
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
// Copyright (c) Omar Boukli-Hacene. All rights reserved.
// Distributed under an MIT-style license that can be
// found in the LICENSE file.
// SPDX-License-Identifier: MIT
/// Problem source:
/// https://en.wikipedia.org/wiki/Greatest_common_divisor
#ifndef FORFUN_GCD_HPP_
#define FORFUN_GCD_HPP_
#include <cstdlib>
namespace forfun::gcd::euclid::recursive {
namespace detail {
[[nodiscard]] constexpr auto gcd_imp(int const m, int const n) noexcept -> int
{
if (n == 0)
{
return m;
}
return gcd_imp(n, m % n);
}
} // namespace detail
[[nodiscard]] /*constexpr*/ inline auto gcd(int const m, int const n) noexcept
-> int
{
return std::abs(detail::gcd_imp(m, n));
}
} // namespace forfun::gcd::euclid::recursive
#endif // FORFUN_GCD_HPP_