Created
July 12, 2021 09:55
-
-
Save zakki/c3ce191390eb2a6b93c758b24bf3a2d3 to your computer and use it in GitHub Desktop.
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 <set> | |
#include <iostream> | |
template<typename T> size_t collatz(T x) { | |
std::set<T> xs; | |
size_t n = 0; | |
for (;; n++) { | |
if (x == 1) | |
break; | |
if (xs.find(x) != xs.end()) { | |
std::cout << "LOOP" << std::endl; | |
exit(1); | |
return n; | |
} | |
xs.insert(x); | |
if (x % 2 == 0) | |
x = x /2; | |
else | |
x = x * 3 + 1; | |
} | |
return n; | |
} | |
int main(int argc, char** argv) { | |
for (size_t i = 1;; i++) | |
std::cout << i << ":" << collatz<int32_t>(i) << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment