Last active
August 29, 2015 14:25
-
-
Save toanalien/c9a72bfdce4582504a21 to your computer and use it in GitHub Desktop.
100 - The 3n + 1 problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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