I am given three operation on integer:
A - add 3 to number
B - doubles the number
C - swaps two last digits of number
I am supposed to write algorithm that checks if i can make k prime number using operations A,B,C in n steps. At the end i have to print the sequence of operations that i used to make k prime number.
Lets assume we have function:
bool ifprime(int n);
The function ifprime returns true when the number is prime and return false when it is not.
The code:
bool is_possible(int k, int n, int a)
{
if(ifprime(k))
{
return true;
}
if(n==0)
{
return false;
}
switch(a)
{
case 1:
k = A(k); // perform operation A
break;
case 2:
k=B(k); //perform operation B
break;
case 3:
k=C(k); //perform operation C
break;
}
return is_possible(k,n-1,1)||is_possible(k,n-1,2)||is_possible(k,n-1,3);
}
My problem is that i do not know how to remember the correct path and then print it.
trueprint a message. once debugged replace "print a message" with appropriate output stepis_possible()is about to returntrue, including cases when it returns a value from the recursive call to itself, log the message.