Skip to content

Instantly share code, notes, and snippets.

@housemeow
Last active August 29, 2015 13:57
Show Gist options
  • Save housemeow/9681811 to your computer and use it in GitHub Desktop.
Save housemeow/9681811 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
void input(int *i, int *j);
int solveIJ3NPlus1MaxCycleLengthProblem(int i, int j);
int solve3NPLus1Problem(int n);
int main()
{
while(1){
int i, j;
input(&i, &j);
int maxCycleLength = solveIJ3NPlus1MaxCycleLengthProblem(i, j);
printf("maxCycleLength = %d\n", maxCycleLength);
system("pause");
}
return 0;
}
void input(int *i, int *j)
{
scanf("%d%d", i, j);
if(*j < *i){
int temp = *i;
*i = *j;
*j = temp;
}
}
int solve3NPLus1Problem(int n)
{
long long int longLongIntN = n;
int cycleLength = 1;
while(1){
if(longLongIntN==1){
return cycleLength;
}else if(longLongIntN%2==1){
longLongIntN = longLongIntN * 3 + 1;
}else
{
longLongIntN = longLongIntN / 2;
}
cycleLength++;
}
}
int solveIJ3NPlus1MaxCycleLengthProblem(int i, int j)
{
int n;
int maxCycleLength = 1;
for(n=i;n<=j;n++){
int cycleLength = solve3NPLus1Problem(n);
if(maxCycleLength < cycleLength)
{
maxCycleLength = cycleLength;
}
}
return maxCycleLength;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment