Advertisement
apl-mhd

np+1

Sep 1st, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int cycleLength(int n){
  4.  
  5.     int i = 0;
  6.  
  7.     while(1){
  8.  
  9.            // printf(" %d ", n);
  10.  
  11.         i++;
  12.  
  13.         if( n == 1)
  14.             break;
  15.  
  16.         if( n % 2 == 1)
  17.             n = (n *3) +1;
  18.        
  19.  
  20.         else
  21.  
  22.             n /=2;
  23.     }
  24.  
  25.  
  26.  
  27. return i;
  28. }
  29.  
  30.  
  31. int maxCycleLength(int a, int b){
  32.  
  33.         int i, max = 0;
  34.  
  35.    for(i = a; i <= b; i++){
  36.  
  37.         if( max < cycleLength(i))
  38.             max = cycleLength(i);
  39.  
  40.        // return max;
  41.    }
  42.  
  43.    return max;
  44.  
  45. }
  46.  
  47. int main()
  48. {
  49.  
  50.     int a, b, temp;
  51.    /* scanf("%d%d",&a, &b);
  52.     if(a > b){
  53.         temp =  a;
  54.         a = b;
  55.         b = temp;
  56.  
  57.     }*/
  58.  
  59.     printf("%d\n", maxCycleLength(1, 10));
  60.     printf("%d\n", maxCycleLength(201, 210));
  61.     printf("%d\n", maxCycleLength(900, 1000));
  62.  
  63.  
  64.  
  65.    return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement