Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Numerical AnalysisRSS Feeds

Problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs

Posted By: Easy Tutor     Category: C++ Programming     Views: 2355

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 i.e n <-- 3n + 1
4. if n is odd then i.e n <-- n/2
5. else
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 (including the 1). 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 i and j.

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.

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.

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).

Code for Problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs in C++ Programming

 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <conio.h>

 constlong cycle_length(long);
 constlong max_cycle_length(constlong,constlong);


 int main( )
    {
       clrscr( );

       fstream File("CP-19.txt",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<"\n Unable to open the input file."<<endl;
         cout<<"\n Press any key to exit.";

         getch( );
         exit(EXIT_FAILURE);
      }

       char Data[100]={NULL};

       do
      {
         strset(Data,NULL);

         File.getline(Data,80);

         if(strcmp(Data,NULL)==0)
        break;

         long start=0;
         long end=0;

         char* Ptr=NULL;

         Ptr=strtok(Data," ");
         start=atol(Ptr);

         Ptr=strtok(NULL,"\n");
         end=atol(Ptr);

         cout<<start<<" "<<end<<" "<<max_cycle_length(start,end)<<endl;
      }
       while(1);

       File.close( );

       getch( );
       return 0;
    }


 /*************************************************************************///-------------------------  max_cycle_length( )  -----------------------///*************************************************************************/constlong max_cycle_length(constlong start,constlong end)
    {
       long length=0;
       long max_length=0;

       for(long i=start;i<=end;i++)
      {
         length=cycle_length(i);

         if(length>max_length)
        max_length=length;
      }

       return max_length;
    }

 /*************************************************************************///--------------------------  cycle_length( )  --------------------------///*************************************************************************/constlong cycle_length(long n)
    {
       long length=1;

       while(n!=1)
      {
         if((n%2)==0)
        n=(n/2);

         else
        n=((3*n)+1);

         length++;
      }

       return length;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!