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...
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...
#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); }
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method receives the following arrangement: [1,2,3,4,5] The invert method returns the array [5,4,3,2,1] Example 2: the print method receives the following array: [5,4,3,2,1] The print method prints: 5,4,3,2,1 (data separated by commas). TIP: for the print method use System.out.print () without the "ln".
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
Consider the following program written in C syntax: void swap(int a, int b) { int temp;...
Consider the following program written in C syntax: void swap(int a, int b) { int temp; temp = a; a = b; b = temp;} void main() { int value = 2, list[5] = {1, 3, 5, 7, 9}; swap(value, list[0]); swap(list[0], list[1]); swap(value, list[value]); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three calls to swap? Passed by value Passed by reference Passed...
why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int...
why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; vector<int>left(n1); vector<int>right(n2); for(int i = 0; i < n1; i++) { left[i] = data[p + i]; } for(int j = 0; j < n2; j++) { right[j] = data[q+j+1]; } int i = 0; int j = 0; for(int k = p; k <= r; k++) { if(left[i]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT