Skip to content

Instantly share code, notes, and snippets.

@david-batranu
Created October 24, 2013 21:33
Show Gist options
  • Save david-batranu/7145427 to your computer and use it in GitHub Desktop.
Save david-batranu/7145427 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
int main(){
unsigned int maxRecord = 0;
unsigned int upperBound = 1000000;
unsigned int chainLen[upperBound + 1];
size_t x;
int max_x;
for(x = 1; x < upperBound; ++x) {
unsigned int n = x;
unsigned int max = 0;
while(n != 1) {
if(n < upperBound && chainLen[n] != 0 && chainLen[n] + max < maxRecord) {break;}
if(n % 2 == 0) {n /= 2;}
else {n = (n * 3 + 1);}
max++;
}
chainLen[x] = max;
if(max >= maxRecord) {
maxRecord = max;
max_x = x;
}
}
fprintf(stdout, "%u\n", max_x);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment