Given the following function int Ulamint num if num
Given the following function
int Ulam(int num)
{
if (num <2)
return 1;
else if (num %2 ==0)
return Ulam(num/2);
else
return Ulam(3* num +1);
}
a.What problems come up in verifiying this function?
b.How many recursive calls are made by the following initial calls?
cout << Ulam(7) <<endl;
cout << Ulam(8) <<endl;
cout << Ulam(15) << endl;
Solution
cout << Ulam(7) <<endl;
Answer: 17 recursive calls are made
After 17 recursive calls, num value will become 1 so if condition will return true and stop the recursive calls and return the value.
cout << Ulam(8) <<endl;
Answer: 4 recursive calls are made
After 4 recursive calls, num value will become 1 so if condition will return true and stop the recursive calls and return the value.
cout << Ulam(15) <<endl;
Answer: 18 recursive calls are made
After 18 recursive calls, num value will become 1 so if condition will return true and stop the recursive calls and return the value.
a.What problems come up in verifiying this function?
Sometimes it will get infinite loop or stach over flow issues when we dont handle the conditions properly in recursive calls. In our code, i dont see any such problems. it will work for sure for all scenarios.
