Skip to main content

UVa 100 - The 3n+1 problem solution

UVa 100- The 3n+1 problem


Description: 


Problems in Computer Science are often classified as belonging to a certain class of problems (e.g.,
NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose
classification is not known for all possible inputs.

Consider the following algorithm:
  1. input n
  2. print n
  3. if n = 1 then STOP
  4. if n is odd then n ←− 3n + 1
  5. else n ←− n/2
  6. GOTO 2
 Given the input 22, the following sequence of numbers will be printed
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input
value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has
been verified, however, for all integers n such that 0 < n < 1, 000, 000 (and, in fact, for many more
numbers than this.)

Given an input n, it is possible to determine the number of numbers printed before and including
the 1 is printed. For a given n this is called the cycle-length of n. In the example above, the cycle
length of 22 is 16.

For any two numbers i and j you are to determine the maximum cycle length over all numbers
between and including both i and j.

Input

The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers
will be less than 10,000 and greater than 0. You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j. You can assume that no operation overflows a 32-bit integer.

Output

For each pair of input integers i and j you should output i, j, and the maximum cycle length for
integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).

Sample Input

1 10
100 200
201 210
900 1000

Sample Output

1 10 20
100 200 125
201 210 89
900 1000 174 


Solution

#include<stdio.h>
int cycle(int a)
{
    int i=0;
    while(1)
    {
             i++;

        if(a==1)
            break;
        if(a%2!=0)
            a=(3*a)+1;
        else
            a=a/2;
    }
    return i;

}
int main()
{
    int a,b,c,temp,i;
    while(scanf("%d%d",&a,&b)==2)
    {
        int max=0;
        printf("%d %d ",a,b);
        if(a>b)
        {
            temp=a;
            a=b;
            b=temp;
        }
        for(i=a;i<=b;i++)
        {
            c=cycle(i);
            if(c>=max)
                max=c;
        }
        printf("%d\n",max);
    }
    return 0;
}



See More Solution:

Comments

Populer Article

UVa 10082 - WERTYU Solution

UVa  10082 - WERTYU Description:   A common typing error is to place the  hands on the keyboard one row to the  right of the correct position. So ‘Q’ is  typed as ‘W’ and ‘J’ is typed as ‘K’ and  so on. You are to decode a message typed in this manner. Input Input consists of several lines of text. Each line may contain digits, spaces, upper case letters (except  Q, A, Z), or punctuation shown above [except back-quote (‘)]. Keys labelled with words [Tab, BackSp,  Control, etc.] are not represented in the input. Output You are to replace each letter or punction symbol by the one immediately to its left on the ‘QWERTY’  keyboard shown above. Spaces in the input should be echoed in the output. Sample Input O S, GOMR YPFSU/ Sample Output I AM FINE TODAY.  Solution #include<stdio.h> int main () { char str[10000], key[]={'`','1','2','3','4','5','6','7','8','9','0', '-','=','...

UVa 10062 - Tell me the frequency Solution

UVa  10062 - Tell me the frequency Description:  Given a line of text you will have to find out the frequencies of the ASCII characters present in it. The  given lines will contain none of the first 32 or last 128 ASCII characters. Of course lines may end with  \n and \r but always keep those out of consideration. Input Several lines of text are given as input. Each line of text is considered as a single input. Maximum  length of each line is 1000. Output Print the ASCII value of the ASCII characters which are present and their frequency according to the  given format below. A blank line should separate each set of output. Print the ASCII characters in the  ascending order of their frequencies. If two characters are present the same time print the information  of the ASCII character with higher ASCII value first. Input is terminated by end of file. Sample Input AAABBC 122333 Sample Output 67 1 66 2 65 3 49 1 50 2 51 3 Solution #include<stdio.h...

UVa 10079 - Pizza Cutting Solution

UVa 10079 - Pizza Cutting Description:  When someone calls Ivan lazy, he claims that it is his intelligence that helps him to be so. If his intelligence allows him to do something at less physical effort, why should he exert more? He also claims that he always uses his brain and tries to do some work at less effort; this is not his laziness, rather this is his intellectual smartness. Once Ivan was asked to cut a pizza into seven pieces to distribute it among his friends. (Size of the pieces may not be the same. In fact, his piece will be larger than the others.) He thought a bit, and came to the conclusion that he can cut it into seven pieces by only three straight cuts through the pizza with a pizza knife. Accordingly, he cut the pizza in the following way (guess which one is Ivan’s piece): One of his friends, who never believed in Ivan’s smartness, was startled at this intelligence. He thought, if Ivan can do it, why can’t my computer? So he tried to do a similar (but not ex...