Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
pow implementation
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//Title of this code #include <iostream> using namespace std; // http://stackoverflow.com/questions/5231096/time-complexity-of-power /* IDEA: x => x^2 and n => n/2 (x^2) ^ (n/2) / x^n \ x * (x^2) ^ ((n-1)/2) */ int power(int a, int b) { if (b < 0) return 0; int pow = 1; while ( b ) { if ( b & 1 ) { pow = pow * a; --b; } a = a*a; b >>= 1; // devide by 2 } return pow; } int mypow(int a, int b) { int ret = 1; for (int i = 0; i < b; ++i) ret *= a; return ret; } int main() { std::cout << power(2,10); }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.34 sec, absolute running time: 0.04 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0.39 sec
edit mode
|
history
|
discussion
1024