Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cmath>
- using namespace std;
- int fast_exponentiation(int base, int pow) {
- unsigned int result; // variable to store intermediaries
- if (pow == 1) {
- return base;
- }
- else if (pow == 0) {
- return 1;
- }
- result = fast_exponentiation(base, floor(pow/2));
- // even power
- if (pow % 2 == 0) {
- return result * result;
- }
- // odd power
- else {
- return result * base * result;
- }
- }
- int main() {
- int num, answer, p;
- cout << "Enter the base: ";
- cin >> num;
- cout << "Enter power: ";
- cin >> p;
- answer = fast_exponentiation(num, p);
- cout << answer << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment