User talk:Fullstop/Sandbox/T2

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
#include <stdio.h>
int main(void) /* empirically test Collatz' 3n+1 conjecture */
{
   unsigned long min, max;

   while (fscanf(stdin, "%lu %lu\n", &min, &max) == 2) {
      unsigned long mm = min, max_cycle_len = 0;
   
      while (mm <= max) {
          unsigned long cycle_len = 0;
          unsigned long n = mm++;

          for (;;) {
             cycle_len++;
  
             if (n == 1)
                break;
             if (n & 1)
                n += (n << 1) | 1;
             else
                n >>= 1;
          }
          if (cycle_len > max_cycle_len)
             max_cycle_len = cycle_len;
      }
      printf("%lu %lu %lu\n", min, max, max_cycle_len);
   }
   return 0;
}