Skip to content

Instantly share code, notes, and snippets.

@Ch-sriram
Created May 29, 2020 01:41
Show Gist options
  • Save Ch-sriram/5359bea35cce5a2d1265547a59ccb81c to your computer and use it in GitHub Desktop.
Save Ch-sriram/5359bea35cce5a2d1265547a59ccb81c to your computer and use it in GitHub Desktop.
Given Number a & N, compute a^N. [Naive Solution: O(N)].
// 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