-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactorial.hpp
83 lines (59 loc) · 1.82 KB
/
factorial.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
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
// 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/Factorial
#ifndef FORFUN_FACTORIAL_HPP_
#define FORFUN_FACTORIAL_HPP_
#include <algorithm>
#include <cassert>
#include <concepts>
#include <functional>
#include <ranges>
#include <type_traits>
#include "common.hpp"
namespace forfun::factorial {
namespace iterative {
/// @note Providing a negative argument for @p n results in undefined behavior.
/// @note For large values of @p n, the result may overflow the return type.
[[nodiscard]] constexpr auto factorial(std::integral auto const n) noexcept
-> decltype(n)
{
using T = std::remove_const_t<decltype(n)>;
assert(n >= T{0});
T result{1};
for (auto i{n}; i > T{1}; --i)
{
result *= i;
}
return result;
}
} // namespace iterative
namespace recursive {
/// @note Providing a negative argument for @p n results in undefined behavior.
template <common::concepts::addition_unpromoted T>
[[nodiscard]] constexpr auto factorial(T const n) noexcept -> T
{
assert(n >= T{0});
if (n <= T{1})
{
return T{1};
}
return n * factorial(n - T{1});
}
} // namespace recursive
namespace stl_functional {
/// @note Providing a negative argument for @p n results in undefined behavior.
/// @note For large values of @p n, the result may overflow the return type.
[[nodiscard]] constexpr auto factorial(std::integral auto const n) noexcept
{
using T = decltype(n);
assert(n >= T{0});
return std::ranges::fold_left(
std::views::iota(T{1}) | std::views::take(n), T{1}, std::multiplies<>()
);
}
} // namespace stl_functional
} // namespace forfun::factorial
#endif // FORFUN_FACTORIAL_HPP_