Question

In: Computer Science

void phase05(){ int n = atoi(next_input()); int m = atoi(next_input()); int t = (hash % 30)...

void phase05(){

int n = atoi(next_input());

int m = atoi(next_input());

int t = (hash % 30) + 21;

int s = 0;

while(n>1){

if(n & 1){

n = (n<<2) - n + 1;

}

else {

n = n >> 1;

}

if(s == t && m == t){

return;

}

s++;

}

failure("Seems you forgatz the essence of Collatz");

}

Hash value: 2002296385

what input values should n, m have in order for the function phase05 to work?

Solutions

Expert Solution

#include <iostream>
using namespace std;
#define hash 2002296385
void phase05(){

int n;
cin>>n;
int m ;
cin>>m;
int t = (hash % 30) + 21; // Line 9
cout<<t<<endl;
int s = 0;
while(n>1) // Line 12
    {
    if(n & 1) // Line 14
    {    n = (n<<2) - n + 1;  }  // Line 15
    else
    {   n = n >> 1;    } //Line 17
    if(s == t && m == t) //Line 18
            {  
                cout<<"program worked";
                return; 
                
            }
    
    s++;
    
    }
    
    cout<<"Seems you forgatz the essence of Collatz"; // error line 
    
    }
int main() {
    
    phase05();

    return 0;
}

This program is based on Collatz conjecture, which generates Collatz sequence for any natural number, the Collatz sequence eventually reach 1 for all positive integer, Main crux of choosing n in this program is that, it's Collatz sequence should have 46 or more terms before it reaches to 1.

Line 9 of Code snippet sets the value of t = 46

Program works if it returns before executing the error line, so the value of m should be 46 ( In order to satisfy the second condition of the if Statement (m == t)

Now to satisfy the first condition, we have to make s = 46 and every time while loop runs it increases the value of s by 1 so we have to make while loop run 46 times and then it will go inside the if condition in line 18 and program returns, s depends on the value of n

  • n >1 (to enter in while loop)

Line 14 checks if n is odd,

  • if n is odd then, the execution will go inside the if condition and the value of n will be updated

Line 17 checks if n is even,

  • if the value of n is even then it will go inside else condition and then n will get updated to

So the value of m should be 46 and the value of n can be any natural number which has more than 46 terms in its Collatz sequence before it reaches to 1. some value of n can be { 27, 31,46,47,54,55,62,63,71,73,82,83,91,94,95, 97 etc}


Related Solutions

Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)...
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)       {        for(int k=0;k<j;k++)        cout<<A[k]<<" + ";        cout<<rem<<"\n";        return;       }     for(int i=0;i<=rem;i++)    {          if(i<=rem)          A[j]=i;          printperm(A,n-1,rem-i,j+1);    } }
   private static void merge(int arr[], int l, int m, int r) {        //...
   private static void merge(int arr[], int l, int m, int r) {        // Find sizes of two subarrays to be merged        int n1 = m - l + 1;        int n2 = r - m;        /* Create temp arrays */        int L[] = new int[n1];        int R[] = new int[n2];        /* Copy data to temp arrays */        for (int i = 0; i...
Given the root C++ code: void sort() {    const int N = 10;    int...
Given the root C++ code: void sort() {    const int N = 10;    int x[N];    for(int i = 0; i < N; i++)    {        x[i] = 1 + gRandom-> Rndm() * 10;        cout<<x[i]<<" "; }    cout<<endl;    int t;       for(int i = 0; i < N; i++)    {    for(int j = i+1; j < N; j++)    {        if(x[j] < x[i])        {   ...
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int x, int y, int z); void add (int arr [10], int first, int last); void print (int arr [10]); int main () {     int my_arr [10];         cout << "The original array is:\n";     print (my_arr);         start (my_arr);     cout << "\n\nThe array after start is:\n";     print (my_arr);         move (my_arr, 2, 4, 6);     cout << "\n\nThe...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {  ...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {    cout<<"? ";    if (n > 0)      mystery (n - 1); }
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT