694 - The Collatz Sequence

#include <stdio.h>

int main()
    {
    long L,H,sum,c,k=1;
    while(scanf("%ld %ld", &L, &H) && L && H)
         {
         if(L == -1 && L==-1) break;
         else
         sum=0;
         c=L;
             while(c!=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 > H)
                   break;
                  }// End of while

                  if(c==1)
                  printf("Case %ld: A = %ld, limit = %ld, number of terms = %ld",k,L,H,sum+1);
                  else
                  printf("Case %ld: A = %ld, limit = %ld, number of terms = %ld",k,L,H,sum);
            k +=1;
            sum = 0;
            printf("\n");
         } // End of while
    } // End of main

Comments

Popular posts from this blog

371 - Ackermann Functions

Create many folder with one click