Solved: 50 - Constexpr Frontier

This commit is contained in:
2026-08-24 19:27:20 +03:00
parent e907849c57
commit 986be65d61
2 changed files with 17 additions and 20 deletions
@@ -1,12 +1,23 @@
#pragma once
#include <algorithm>
#include <array>
#include <cstddef>
namespace cpp26 {
[[nodiscard]] auto compile_time_factorial(int n) -> long long;
[[nodiscard]] auto compile_time_sum(std::array<int, 5> values) -> int;
[[nodiscard]] auto is_sorted(std::array<int, 5> values) -> bool;
[[nodiscard]] constexpr auto compile_time_factorial(int n) -> long long {
auto result = 1LL;
for (auto i = 2; i <= n; ++i) {
result *= i;
}
return result;
}
[[nodiscard]] constexpr auto compile_time_sum(std::array<int, 5> values) -> int {
return std::ranges::fold_left(values, 0, std::plus{});
}
[[nodiscard]] constexpr auto is_sorted(const std::array<int, 5> &values) -> bool {
return std::ranges::is_sorted(values); // also a constexpr function
} // namespace cpp26
}
} // namespace cpp26