In: Computer Science
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?
#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
Line 14 checks if n is odd,
Line 17 checks if n is even,
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}