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);    } }
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as a parameter, put all prime numbers less than n into an array, and print out the array and the sum of the numbers in that array. You can use the isPrime function above to save time.
   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;...
Topic: Template template void arrayContents(const T *arr, int countSize); int main() { const int ACOUNT =...
Topic: Template template void arrayContents(const T *arr, int countSize); int main() { const int ACOUNT = 5; const int BCOUNT = 7; const int CCOUNT = 6; int a[ACOUNT] = {1, 2, 3, 4, 5}; double b[BCOUNT] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; char c[CCOUNT] = "HELLO"; cout <<"Array A contents: \n"; arrayContents(a, ACOUNT); cout <<"Array B contents: \n"; arrayContents(b, BCOUNT); cout <<"Array C contents: \n"; arrayContents(c, CCOUNT); return 0; } template void arrayContents(const T *arr, int countSize)...
#include<iostream> using namespace std; void calcSumAndDiff(int ,int, int &, int &); void calcSumAndDiff(int n1,int n2, int...
#include<iostream> using namespace std; void calcSumAndDiff(int ,int, int &, int &); void calcSumAndDiff(int n1,int n2, int &sum, int &diff){ sum = n1 + n2; diff = n1 - n2; } int main() { int n1,n2,sum,diff; n1=30;n2=10; calcSumAndDiff(n1,n2,sum,diff); cout<<"Sum is :"<<sum<<endl; cout<<"Diff is:"<<diff<<endl; system("pause"); }
Write the pseudocodes for these programs: a) #include <iostream> using namespace std; void print(int arr[],int n){...
Write the pseudocodes for these programs: a) #include <iostream> using namespace std; void print(int arr[],int n){ if(n==0) return; cout<<arr[n-1]<<" "; print(arr,n-1); } int main() { int n; cout<<"Enter the size of the array:"; cin>>n; int arr[n]; int i; cout<<"Enter the elements of the array:"<<endl; for(i=0;i<n;i++){ cin >> arr[i]; } cout<<"Displaying the elements of the array in reverse order:"<<endl; print(arr,n); return 0; } b) #include<iostream> #include<fstream> using namespace std; //student structure struct student { int id; float gpa; }; //node structure...
Analyze the following codes by computing their running time, T(n). 1) int function ( int n)...
Analyze the following codes by computing their running time, T(n). 1) int function ( int n) { int sum; for (int i = 0; i < n; i++) ​ sum = sum + (i * n); return sum; } 2) int function(int n) { ​int sum; ​ ​for (int i = 0; i < n; i++) ​​if (n < 1000) ​​​sum++; ​​else ​​​sum += function(n); } 3) int function(n) { ​int s = 0; ​for (int i = 1; i...
*/ #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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT