Back to list of problems
The 3n + 1 problem
100.c
/* Trivial */ #include <stdio.h> int algorithm(int n) { int length=1; while(n!=1) { if (n%2) { n = 3*n+1; } else { n = n/2; } length++; } return length; } #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MAX(a,b) ((a) > (b) ? (a) : (b)) int main() { int i,j; while(scanf("%d %d", &i, &j)==2) { int n; int maximum=0; for(n=MIN(i,j); n<=MAX(i,j); n++) { int a = algorithm(n); maximum = (maximum>a) ? maximum : a; } printf("%d %d %d\n", i, j, maximum); } exit(0); }