rlm@0: rlm@0: #include rlm@0: #include rlm@0: rlm@0: int main(int argc, char *argv[]){ rlm@0: FILE *fp; rlm@0: int n; rlm@0: if((fp=fopen(argv[1] ,"r")) == NULL) { rlm@0: printf("Cannot open file.\n"); rlm@0: exit(1);} rlm@0: fscanf(fp, "%d", &n); /* read from file */ rlm@0: int answer = search_collatz(n); rlm@0: printf("%d\n", answer); } rlm@0: rlm@0: long long collatz_next(long long n){ rlm@0: if (!(n % 2)){ rlm@0: return n/2;} rlm@0: else { rlm@0: return 1+(n*3);}} rlm@0: rlm@0: int collatz_length(int n){ rlm@0: long long t = n; rlm@0: int d = 1; rlm@0: while (t > 1){ rlm@0: t = collatz_next(t); rlm@0: d++;} rlm@0: return d;} rlm@0: rlm@0: int search_collatz(int n){ rlm@0: int max = 0; rlm@0: int max_val = 0; rlm@0: int temp_val; rlm@0: int i; rlm@0: for (i = 1; i max_val){ rlm@0: max = i; rlm@0: max_val = temp_val;}} rlm@0: return max;} rlm@0: