Skip to content

Instantly share code, notes, and snippets.

@moretti
Created August 2, 2013 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moretti/6141287 to your computer and use it in GitHub Desktop.
Save moretti/6141287 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int collatz_len(unsigned long n) {
int c = 0;
while (n != 1) {
if (n % 2 == 1) {
n = 3 * n + 1;
} else {
n /= 2;
}
c++;
}
return c;
}
int main() {
int res = 0, resCount = 0;
for (int n = 1; n <= 1000000; n++) {
int c = collatz_len(n);
if (resCount < c) {
resCount = c;
res = n;
}
}
printf("%d %d\n", res, resCount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment