1
0

Exponentiation By Squaring.

This commit is contained in:
Captain ALM 2022-08-15 15:08:54 +01:00
parent c904f740b7
commit 9d2f0822c3
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1

View File

@ -1,21 +1,30 @@
#include <iostream>
#include <cstdint> // for std::int64_t
#include <cassert> // for assert
int getValue()
std::int64_t powint(std::int64_t base, int exp)
{
std::cout << "Enter an integer: ";
assert(exp >= 0 && "powint: exp parameter has negative value");
int x{};
std::cin >> x;
return x;
std::int64_t result{ 1 };
while (exp)
{
if (exp & 1) {
std::cout << "a : " << result << "\n";
result *= base;
}
std::cout << "b : " << exp << "\n";
exp >>= 1;
std::cout << "c : " << base << "\n";
base *= base;
}
return result;
}
int main()
{
int a{ getValue() };
int b{ getValue() };
int c{ getValue() };
std::cout << a + (b * c); // order of eval doesn't matter now
std::cout << powint(7, 12); // 7 to the 12th power
return 0;
}