Exponentiation By Squaring.
This commit is contained in:
parent
c904f740b7
commit
9d2f0822c3
29
main.cpp
29
main.cpp
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user