371 - Ackermann Functions
#include <stdio.h>
int main()
{
long L,H,sum,max,track,V,c,temp;
while(scanf("%ld %ld", &L, &H) && L && H)
{
max=-99999999;
sum=0;
if(L > H)
{
temp=L;
L=H;
H=temp;
} // End of if
for(long i=L;i<=H;i++)
{
track=i;
c=i;
while(1)
{
if(c%2==0)
{
c=c/2;
sum +=1;
} // End of if
else
{
c = c*3+1;
sum +=1;
} // Endo of else
if(c==1) break;
//printf("%ld ",c);
}// End of while
if(sum > max)
{
max=sum;
V = track;
} // End of max
//printf("sum = %ld\nV=%ld",sum,V);
sum = 0;
//printf("\n");
} // End of for
printf("Between %ld and %ld, %ld generates the longest sequence of %ld values.\n",L,H,V,max);
} // End of while
} // End of main
int main()
{
long L,H,sum,max,track,V,c,temp;
while(scanf("%ld %ld", &L, &H) && L && H)
{
max=-99999999;
sum=0;
if(L > H)
{
temp=L;
L=H;
H=temp;
} // End of if
for(long i=L;i<=H;i++)
{
track=i;
c=i;
while(1)
{
if(c%2==0)
{
c=c/2;
sum +=1;
} // End of if
else
{
c = c*3+1;
sum +=1;
} // Endo of else
if(c==1) break;
//printf("%ld ",c);
}// End of while
if(sum > max)
{
max=sum;
V = track;
} // End of max
//printf("sum = %ld\nV=%ld",sum,V);
sum = 0;
//printf("\n");
} // End of for
printf("Between %ld and %ld, %ld generates the longest sequence of %ld values.\n",L,H,V,max);
} // End of while
} // End of main
Comments
Post a Comment