Created
October 13, 2024 06:53
-
-
Save nus/4ca9f1d49b5e6396c6cf9fe548c858d1 to your computer and use it in GitHub Desktop.
Calcuration the Collatz conjecture during compilation time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdint> | |
// Calcuration the Collatz conjecture during compilation time. | |
template<bool EVEN, uint64_t N> | |
struct InnerCollatz { | |
static const uint64_t next = N / 2; | |
static const uint64_t value = InnerCollatz<next % 2 == 0, next>::value; | |
}; | |
template<uint64_t N> | |
struct InnerCollatz<false, N> { | |
static const uint64_t next = (N * 3) + 1; | |
static const uint64_t value = InnerCollatz<next % 2 == 0, next>::value; | |
}; | |
template<> | |
struct InnerCollatz<false, 1> { | |
static const uint64_t value = 1; | |
}; | |
template<uint64_t N> | |
struct Collatz { | |
static const uint64_t value = InnerCollatz<N % 2 == 0, N>::value; | |
}; | |
int main() { | |
static_assert(Collatz<1>::value == 1); | |
static_assert(Collatz<123456789>::value == 1); | |
std::cout << Collatz<123456789>::value << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment