Skip to content

Instantly share code, notes, and snippets.

@zakki
Created July 12, 2021 09:55
Show Gist options
  • Save zakki/c3ce191390eb2a6b93c758b24bf3a2d3 to your computer and use it in GitHub Desktop.
Save zakki/c3ce191390eb2a6b93c758b24bf3a2d3 to your computer and use it in GitHub Desktop.
#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