Skip to content

Instantly share code, notes, and snippets.

@toanalien
Last active August 29, 2015 14:25
Show Gist options
  • Save toanalien/c9a72bfdce4582504a21 to your computer and use it in GitHub Desktop.
Save toanalien/c9a72bfdce4582504a21 to your computer and use it in GitHub Desktop.
100 - The 3n + 1 problem
#include <stdio.h>
int main()
{
unsigned long int x,z,i,j , n, max;
while (scanf("%lu %lu", &x, &z) == 2)
{
if (x > z)
{
i = z;
j = x;
}
else
{
i = x;
j = z;
}
max = 0;
for (unsigned long int k = i; k <= j; k++)
{
unsigned long int temp = k;
n = 1;
while (temp != 1)
{
n++;
if (temp % 2 == 0)
temp /= 2;
else
temp = 3 * temp + 1;
}
if (n > max)
max = n;
}
printf("%lu %lu %lu\n", x, z, max);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment