Math day, problem in modular power calculation :(

there is a conceptual error in your code
what you are asked is to compute (a^(n!))%p
but what you are doing is (a^(sum of n from 1 to n))%p
that’s why you are getting wrong answer

ohk fine, i have corrected it…but still there is mistake in power function

your power function is right all you have to do a little variation in your code instead of calling power(i,n,p) you have to call power(ans,n,p) and initialze ans with a


why we are doing this :-
we can write a^n!= (----((((a^1)^2)^3)^4)…)^n) which is (ans,n-1,p)^n now i think the solution make some sense

In case of any doubt feel free to ask :slight_smile:
if u got the answer mark your doubt as resolved

sir , meine same kar liya theek …power(ans,i,p) hi kiya hai…tabh bhi galt aara h

okay ! share your updated code , i will check.

your for loop should be from 1 to n instead of 2 to n or initialize your ans with a%p instead of a
refer above code i have shared

oh yes…a^1 has been calculated but %p has not done…understood now.Thanks… :slight_smile:

sir agr ham ans=a%n karde , then loop 2 se chalay , fir toh wa aara h…esa kyu?

kyun ki hame mod p se lena hai n se nhi

@Muskan-Gupta-598128740703036 first version of your code is perfect you just have to initialize your ans with ans=a%p just in case if n=1 you wont go to power function so its better to take this as ans. update this and you are good to go.