please explain the logic to make ab from (-a) and (-b) i have done for all 3 cases by adding or subtracting a,b times from a in case of negative b a is subtracted from a (-b-1) times and in case of addition we will add a to a b times also the logic for the second problem
Recursion time to fly
Hello @Learning_bunny,
Below is the step by step approach:
- Check if one or both of the numbers are negative.
- If the number passed in the second parameter is negative swap the parameters and call the function again.
- If both of the parameters are negative call the function again and pass the absolute values of the numbers as parameters.
- If n<m call the function with swapped parameters for less execution time of the function.
- As long as m is not 0 keep on calling the function with subcase n, m-1 and return n + multrecur(n, m-1).
Hope, this would help.
Give a like if you are satisfied.
please explain the second and forth step again
Hello @Learning_bunny,
the first parameter is n and second is m
And you have to find n*m
-
The second point instructs to swap the n and m if m was negative.
This is done to make the second variable positive.
Reason:
The number of recursive calls depends upon the value of m. -
The fourth point instructs to swap n and m if n<m.
To reduce the number of recursive calls
still dont get the 4th point, how replacing n and m reduce complex
Hello @Learning_bunny,
As i have explained in my previous reply:
The number of recursive calls depends upon the value of m i.e. the second parameter.
Reason:
In the recursive call:
return n + multrecur(n, m-1).
You are reducing the value of second parameter until m becomes 0 i.e. the base statement.
So, lesser the value of m lesser the number of recursions.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.