Skip to content

Instantly share code, notes, and snippets.

@nandor
Created February 19, 2016 22:06
Show Gist options
  • Save nandor/80cb1781155de22dedc5 to your computer and use it in GitHub Desktop.
Save nandor/80cb1781155de22dedc5 to your computer and use it in GitHub Desktop.
Power
/**
Computes the nth power of x in a rather funny way.
*/
template<size_t N> inline uint64_t power(int x) {
const auto half = power<N >> 1>(x);
const auto half2 = half * half;
return (N & 1) ? (half2 * x) : half2;
}
template<> inline uint64_t power<0>(int x) { return 1; }
template<> inline uint64_t power<1>(int x) { return x; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment