how to solve this problem using modulo and power
How to solve this problem using modulo and power
Hello @pragyachoudhary1111,
You can use recursion to solve this problem:
Suppose, you have to compute a^b and created a function named power(a,b),
-
if b is even:
a^(b/2)*a^(b/2)=a^(b/2+b/2)=a^b -
if b is odd:
a^(b/2)*a^(b/2)*a=a^b
this extra a accounts for odd number -
Use the above property to solve this problem
-
What is the base case?
if(b==0){ return 0;}
if(b==1){ return 1;} -
What is the recursive statement?
power(a,b/2);
Now, coming to the modulo part.
Why do we do this?
To normalize a value to a given range i.e. to prevent numbers to exceed a given range.
Property that you can use:
(ab)%c=((a%c)(b%c))%c;
Try to code.
If you face any issue, feel free to ask.
Hope, this would help.
Give a like, if you are satisfied.
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.