This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
Idiom #32 Integer exponentiation by squaring
Create function exp which calculates (fast) the value x power n.
x and n are non-negative integers.
- C
- Clojure
- C++
- C#
- C#
- D
- Dart
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Haskell
- JS
- JS
- Java
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- Pascal
- Pascal
- Perl
- Perl
- Python
- Ruby
- Ruby
- Rust
- Rust
- Scala
- Smalltalk
unsigned int exp(unsigned int x,unsigned int n)
{
if(n==0)
{
return 1;
}
if(n==1)
{
return x;
}
if(!(n%2))
{
return exp(x*x,n/2);
}
return x*exp(x*x,(n-1)/2);
}
int pow(int x, int p)
{
if (p == 0) return 1;
if (p == 1) return x;
int tmp = pow(x, p/2);
if (p%2 == 0) return tmp * tmp;
else return x * tmp * tmp;
}
long exp(long x, long n){
if (n == 0)
return 1;
if (n == 1)
return x;
if (n % 2 == 0)
return exp(x * x, n / 2);
else
return x * exp(x * x, (n - 1) / 2);
}
int exp(int x, int n) {
if (n == 0) return 1;
if (n == 1) return x;
var r = exp(x * x, n ~/ 2);
if (n % 2 == 1) r *= x;
return r;
}
int exp(int x, int n) {
var r = 1;
while (true) {
if (n.isOdd) r *= x;
n ~/= 2;
if (n == 0) break;
x *= x;
}
return r;
}
The idiomatic way to do exponentiation by squaring is a loop, not a recursive function.
This one puts the break before the x*=x to avoid an extra unnecessary multiplication at the end.
This one puts the break before the x*=x to avoid an extra unnecessary multiplication at the end.
-module (int_power).
-export ([exp/2]).
exp (X, N) -> exp (X, N, 1).
exp (_X, 0, Y) -> Y;
exp (X, N, Y) when N rem 2 =:= 0 ->
exp (X * X, N div 2, Y);
exp (X, N, Y) ->
exp (X * X, N div 2, X * Y).
Right-to-left exponentiation by squaring. Knuth TAOCP Volume 2, 3rd edition, p 462. This cleans up Algorithm A of section 4.6.3.
func exp(x, n int) int {
switch {
case n == 0:
return 1
case n == 1:
return x
case n%2 == 0:
return exp(x*x, n/2)
default:
return x * exp(x*x, (n-1)/2)
}
}
Warning: type int quickly overflows
function exp(x, n) {
if (n===0) return 1;
if (n===1) return x;
return n%2 ? x * exp(x*x, (n-1)/2)
: exp(x*x, n/2);
}
int exp(int x, int n){
if(n==0)
return 1;
if(n==1)
return x;
if(n%2==0)
return exp(x*x, n/2);
else
return x * exp(x*x, (n-1)/2);
}
Warning :
- type int quickly overflows
- type int quickly overflows
(loop
for (power rem) = (list p 0) then (multiple-value-list (floor power 2))
for multiple = 1 then (if (zerop rem) multiple (* multiple base))
for base = x then (* base base)
when (= power 0) return 1
when (= power 1) return (* multiple base)))
All of the odd values are multiplied into multiple while base is squared with each iteration.
Since specification is "non-negative" handle the case of the power being 0 separately
Since specification is "non-negative" handle the case of the power being 0 separately
function custom_exp($x, $n)
{
if ($n === 0) {
return 1;
}
if ($n === 1) {
return $x;
}
if (!($n % 2)) {
return custom_exp($x * $x, $n / 2);
}
return $x * custom_exp($x * $x, ($n - 1) / 2);
}
echo custom_exp(5, 10);
A custom exp function using recursion. Note: an exp function already exists in PHP for powers of the constant e. See documentation for pow function and "**" operator.
fn exp(x: u64, n: u64) -> u64 {
match n {
0 => 1,
1 => x,
i if i % 2 == 0 => exp(x * x, n / 2),
_ => x * exp(x * x, (n - 1) / 2),
}
}