mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-15 11:20:05 +08:00
formatting source-code for 153fb7b8a5
This commit is contained in:
@@ -23,8 +23,7 @@
|
||||
* algorithm implementation for \f$a^b\f$
|
||||
*/
|
||||
template <typename T>
|
||||
double fast_power_recursive(T a, T b)
|
||||
{
|
||||
double fast_power_recursive(T a, T b) {
|
||||
// negative power. a^b = 1 / (a^-b)
|
||||
if (b < 0)
|
||||
return 1.0 / fast_power_recursive(a, -b);
|
||||
@@ -48,15 +47,13 @@ double fast_power_recursive(T a, T b)
|
||||
It still calculates in \f$O(\log N)\f$
|
||||
*/
|
||||
template <typename T>
|
||||
double fast_power_linear(T a, T b)
|
||||
{
|
||||
double fast_power_linear(T a, T b) {
|
||||
// negative power. a^b = 1 / (a^-b)
|
||||
if (b < 0)
|
||||
return 1.0 / fast_power_linear(a, -b);
|
||||
|
||||
double result = 1;
|
||||
while (b)
|
||||
{
|
||||
while (b) {
|
||||
if (b & 1)
|
||||
result = result * a;
|
||||
a = a * a;
|
||||
@@ -68,14 +65,12 @@ double fast_power_linear(T a, T b)
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
std::srand(std::time(nullptr));
|
||||
std::ios_base::sync_with_stdio(false);
|
||||
|
||||
std::cout << "Testing..." << std::endl;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
for (int i = 0; i < 20; i++) {
|
||||
int a = std::rand() % 20 - 10;
|
||||
int b = std::rand() % 20 - 10;
|
||||
std::cout << std::endl << "Calculating " << a << "^" << b << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user