Modular exponentiation

in this question if we find power first and then take modulus it dosent work .why

because power could be number whose value exceed maximum number that can be stored in long long int

Take modulo while calculating the power function (custom)
Dont use power function

  1. Use logn time exponentiation with taking modulo each time in the recursion/while loop

say if you want to calculate 10^100, if you dont perform modulo inside your custom power function it will still overflow.

1 Like