Created
May 29, 2020 01:41
-
-
Save Ch-sriram/5359bea35cce5a2d1265547a59ccb81c to your computer and use it in GitHub Desktop.
Given Number a & N, compute a^N. [Naive Solution: O(N)].
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Naive Solution: O(N) Time Complexity | |
const long long M = (long long) 1e9+7; // a very big prime number | |
long long pow(a, N) { | |
if (N == 0) return 1; | |
long long ans = 1; | |
for(int i = 0; i < N; ++i) | |
ans = ((ans % M) * a) % M; | |
return ans % M; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment