SimiLie
Loading...
Searching...
No Matches
binomial_coefficient.hpp
1// SPDX-FileCopyrightText: 2024 Baptiste Legouix
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#include <ddc/ddc.hpp>
7
8namespace sil {
9
10namespace misc {
11
12// From https://stackoverflow.com/a/44719219
13constexpr inline std::size_t binomial_coefficient(std::size_t n, std::size_t k) noexcept
14{
15 return (k > n) ? 0 : // out of range
16 (k == 0 || k == n) ? 1
17 : // edge
18 (k == 1 || k == n - 1) ? n
19 : // first
20 binomial_coefficient(n - 1, k - 1) * n / k; // recursive
21}
22
23} // namespace misc
24
25} // namespace sil
constexpr std::size_t binomial_coefficient(std::size_t n, std::size_t k) noexcept
The top-level namespace of SimiLie.
Definition csr.hpp:14