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 <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::int64_t result{ 1 };
|
||||||
std::cin >> x;
|
while (exp)
|
||||||
return x;
|
{
|
||||||
|
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 main()
|
||||||
{
|
{
|
||||||
int a{ getValue() };
|
std::cout << powint(7, 12); // 7 to the 12th power
|
||||||
int b{ getValue() };
|
|
||||||
int c{ getValue() };
|
|
||||||
|
|
||||||
std::cout << a + (b * c); // order of eval doesn't matter now
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user