You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
444 B
21 lines
444 B
#pragma once
|
|
|
|
#include <type_traits>
|
|
|
|
namespace algorithm
|
|
{
|
|
template <typename T, typename F>
|
|
T fast_power(T a, F n)
|
|
{
|
|
static_assert(std::is_arithmetic_v<T>, "Radix must be arithmetic for fast power");
|
|
static_assert(std::is_integral_v<F>, "Power must be integral for fast power");
|
|
|
|
T result{1};
|
|
while (n > 0) {
|
|
if (n & 1) result *= a;
|
|
a *= a;
|
|
n >>= 1;
|
|
}
|
|
return result;
|
|
}
|
|
} // namespace algorithm
|