Search
 
SCRIPT & CODE EXAMPLE
 

CPP

power function in O(log(n)) time c++

#include <bits/stdc++.h>

using namespace std;

long long power(long long num, long long n){
 	if(n == 0)return 1;
  	long long tmp = power(num, n / 2);
  	tmp = tmp * tmp;
  	if(n % 2 == 0)return tmp;
  	return tmp * num;
}

int main() {
    cout << power(3,4); // outputs 81 since 3^4 is 81
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: print to console c++ 
Cpp :: hello world c++ visual studio 
Cpp :: should i learn c or c++ 
Cpp :: qt qchar to char 
Cpp :: have unique vector after sorting vector 
Cpp :: error: ‘memset’ was not declared in this scope in cpp 
Cpp :: how to run code in devcpp 
Cpp :: c++ pass argument to singleton 
Cpp :: cpp executing without console 
Cpp :: can you verify adsense no ssl certificate 
Cpp :: c++ execution time 
Cpp :: c++ while loop decrement 
Cpp :: expected number of trials to get n consecutive heads 
Cpp :: climits in cpp 
Cpp :: meter espacios en cadena c 
Cpp :: border radius layout android xml 
Cpp :: initialize 2d vector of ints c++ 
Cpp :: how to get a word from file c++ 
Cpp :: recursive binary search 
Cpp :: cpp rand 
Cpp :: c++ find largest number in array 
Cpp :: c++ vector iterator 
Cpp :: create a dictionary cpp 
Cpp :: getch c++ library 
Cpp :: Could not load the Visual C++ component "VCBuild.exe". To fix this, 1) install the .NET Framework 2.0 SDK, 2) install Microsoft Visual Studio 2005 or 3) add the location of the component to the system path if it is installed elsewhere. 
Cpp :: elixir update map 
Cpp :: syntax c++ 
Cpp :: how to round to nearest whole number unity 
Cpp :: c++ cin operator 
Cpp :: read text from file c++ 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =