The 3n + 1 problem

1 view
Skip to first unread message

bahador saket

unread,
Nov 23, 2009, 7:07:07 AM11/23/09
to FIT Cafeteria
Dear all,
can you please help me to understand the meaning of this question??

Question :::

Consider the following algorithm to generate a sequence of numbers.
Start with an integer n. If n is even, divide by 2. If n is odd,
multiply by 3 and add 1. Repeat this process with the new value of n,
terminating when n = 1. For example, the following sequence of numbers
will be generated for n = 22:
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
It is conjectured (but not yet proven) that this algorithm will
terminate at n = 1 for every integer n. Still, the conjecture holds
for all integers up to at least 1, 000, 000.

For an input n, the cycle-length of n is the number of numbers
generated up to and including the 1. In the example above, the cycle
length of 22 is 16. Given any two numbers i and j, you are to
determine the maximum cycle length over all numbers between i and j,
including both endpoints.

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 1,000,000
and greater than 0.

Output
For each pair of input integers i and j, output i, j in the same order
in which they appeared in the input and then the maximum cycle length
for integers between and including i and j. These three numbers should
be separated by one space, with all three numbers on one line and with
one line of output for each line of input.

Sample Input

1 10
100 200
201 210
900 1000

Sample Output

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


what's the i and j really?
friends, do not solve the problem, clarify the problem for me please.

Best Regards,
Bahador Saket

Ognjen Regoje

unread,
Nov 23, 2009, 7:22:23 AM11/23/09
to fit-ca...@googlegroups.com
Hello,

example:
If i = 20
and j = 25

then you have to run the algorithm calculating the cycle length for all the numbers between 20 and 25: 20, 21, 22, 23, 24 and 25: then get the biggest number that the algorithm has calculated and output it as the third integer.

Regards
Ognjen



bahador saket

unread,
Nov 23, 2009, 8:34:34 AM11/23/09
to fit-ca...@googlegroups.com
ٍِِDear Mr/Mis Ognjen

thanks for your answer....
but i wrote the program to what you said and i had some problems because when i run my program my output is not look like the output of problem but in my opinion it work correctly.
because between 1 to 10 we have 9 number that we can caculate .
2 ----> 1
3-----> 10 5 16 8 4 2 1
4-----> 2 1
5-----> 16 8 4 2 1
6----->3 10 5 16 8 4 2 1
7----->22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
8----->4 2 1
9-----> 28 14 7 22 11 34 17 52 26 13 40  20 10 5 16 8 4 2 1

i wrote this program, it can show the program you said but it output is not like the question output.

my program:

 #include<iostream>
using namespace std;
int main(){
    int i,j,n;
    cin>>i>>j;
    int temp;
    int tool,max;
    tool=j-i;
    int array[tool];
     for(int s=0;i<=j;i++,s++)
     {
             int counter=1;
             int temp=i;
            // cout<<temp<<"    ";          only for testing
             while(temp>1)
             {
                if(temp%2==0)
                  temp=temp/2;
                else
                  temp=temp*3+1;
                counter++;
                //cout<<" "<<temp;            only for testing
             }
             //cout <<endl;                       only for testing
            array[s]=counter;
     }
     max=array[0];
     for(int j=0;j<tool;j++)
     {
             if(array[j+1]>array[j])
             {
                     max=array[j+1];
             }
     }
    cout<<" "<<i<< " "<<j<<" "<<max;
    system("pause");
    return 0;
}


Best Regards,
Bahador Saket

Ognjen Regoje

unread,
Nov 23, 2009, 9:01:35 AM11/23/09
to fit-ca...@googlegroups.com
Hey,

its Mr. Ognjen btw :D

Sorry, I do not have time to explain how everything works but with a bit of modification your program works. Look through mine and take see the differences. Hope you get it. Here it is.

//-----------------------------------------------------------------------------------------------------

 #include<iostream>
using namespace std;
int main(){
    int i,j;

    cin>>i>>j;
    int temp;
    int tool;
    int max = 0;
    tool=j-i;
    int array[tool];

     for(int s=i;s<=j;s++)
     {
             int counter=1;
             int temp=s;
             cout<<temp<<"    ";

             while(temp>1)
             {
                if(temp%2==0)
                  temp=temp/2;
                else
                  temp=temp*3+1;
                counter++;
                cout<<" "<<temp;
             }
             cout << endl;
             if (max < counter) max = counter;
            //array[s-i]=counter;
     }

     /*max2=array[0];

     for(int j=0;j<tool;j++)
     {
             if(array[j+1]>array[j])
             {
                     max=array[j+1];
             }
     }*/

    cout<<i<< " "<<j<<" "<<max;
    system("pause");
    return 0;
}
//-----------------------------------------------------------------------------------------------------

Regards
Ognjen

bahador saket

unread,
Nov 23, 2009, 10:33:29 AM11/23/09
to fit-ca...@googlegroups.com
Dear Mr. Ognjen

tnx for your helps, i find the problem of my program.
    
     max2=array[0];
     for(int j=0;j<tool;j++)
     {
             if(array[j+1]>array[j])
             {
                     max=array[j+1];
             }
     }
my problem was in this scope , i should revise my if , i should write 

max=0;

     for(int j=0;j<tool;j++)
     {
             if(array[j]>max)
             {
                     max=array[j];
             }
     }

because the lenght of array is limited.

Best Regards,
Bahador Saket
Reply all
Reply to author
Forward
0 new messages