← Back

Fast (Binary) Exponentiation

Compute x^n in O(log n) by squaring the base and reading n's bits.

1 / 1
// power returns x^n for n >= 0 in O(log n) multiplications.
func power(x, n int) int {

}
// power returns x^n for n >= 0 in O(log n) multiplications.
func power(x, n int) int {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
}
// power returns x^n for n >= 0 in O(log n) multiplications.
func power(x, n int) int {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    result := 1
}
// power returns x^n for n >= 0 in O(log n) multiplications.
func power(x, n int) int {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    result := 1

        // If the lowest bit of n is set, the current power of x belongs in the result.
}
// power returns x^n for n >= 0 in O(log n) multiplications.
func power(x, n int) int {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    result := 1

    for n > 0 {
        // If the lowest bit of n is set, the current power of x belongs in the result.
        if n&1 == 1 {
            result *= x
        }
    }
}
// power returns x^n for n >= 0 in O(log n) multiplications.
func power(x, n int) int {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    result := 1

    for n > 0 {
        // If the lowest bit of n is set, the current power of x belongs in the result.
        if n&1 == 1 {
            result *= x
        }

        // Move to the next bit: square the base and shift n right by one.
    }
}
// power returns x^n for n >= 0 in O(log n) multiplications.
func power(x, n int) int {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    result := 1

    for n > 0 {
        // If the lowest bit of n is set, the current power of x belongs in the result.
        if n&1 == 1 {
            result *= x
        }

        // Move to the next bit: square the base and shift n right by one.
        x *= x
        n >>= 1
    }
}
// power returns x^n for n >= 0 in O(log n) multiplications.
func power(x, n int) int {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    result := 1

    for n > 0 {
        // If the lowest bit of n is set, the current power of x belongs in the result.
        if n&1 == 1 {
            result *= x
        }

        // Move to the next bit: square the base and shift n right by one.
        x *= x
        n >>= 1
    }

    // For modular exponentiation, take % mod after each *= (result and x).
}
// power returns x^n for n >= 0 in O(log n) multiplications.
func power(x, n int) int {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    result := 1

    for n > 0 {
        // If the lowest bit of n is set, the current power of x belongs in the result.
        if n&1 == 1 {
            result *= x
        }

        // Move to the next bit: square the base and shift n right by one.
        x *= x
        n >>= 1
    }

    // For modular exponentiation, take % mod after each *= (result and x).
    return result
}
# power returns x^n for n >= 0 in O(log n) multiplications.
def power(x, n):
    pass
# power returns x^n for n >= 0 in O(log n) multiplications.
def power(x, n):
    # x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    pass
# power returns x^n for n >= 0 in O(log n) multiplications.
def power(x, n):
    # x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    result = 1
# power returns x^n for n >= 0 in O(log n) multiplications.
def power(x, n):
    # x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    result = 1

        # If the lowest bit of n is set, the current power of x belongs in the result.
# power returns x^n for n >= 0 in O(log n) multiplications.
def power(x, n):
    # x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    result = 1

    while n > 0:
        # If the lowest bit of n is set, the current power of x belongs in the result.
        if n & 1 == 1:
            result *= x
# power returns x^n for n >= 0 in O(log n) multiplications.
def power(x, n):
    # x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    result = 1

    while n > 0:
        # If the lowest bit of n is set, the current power of x belongs in the result.
        if n & 1 == 1:
            result *= x

        # Move to the next bit: square the base and shift n right by one.
# power returns x^n for n >= 0 in O(log n) multiplications.
def power(x, n):
    # x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    result = 1

    while n > 0:
        # If the lowest bit of n is set, the current power of x belongs in the result.
        if n & 1 == 1:
            result *= x

        # Move to the next bit: square the base and shift n right by one.
        x *= x
        n >>= 1
# power returns x^n for n >= 0 in O(log n) multiplications.
def power(x, n):
    # x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    result = 1

    while n > 0:
        # If the lowest bit of n is set, the current power of x belongs in the result.
        if n & 1 == 1:
            result *= x

        # Move to the next bit: square the base and shift n right by one.
        x *= x
        n >>= 1

    # For modular exponentiation, take % mod after each *= (result and x).
# power returns x^n for n >= 0 in O(log n) multiplications.
def power(x, n):
    # x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    result = 1

    while n > 0:
        # If the lowest bit of n is set, the current power of x belongs in the result.
        if n & 1 == 1:
            result *= x

        # Move to the next bit: square the base and shift n right by one.
        x *= x
        n >>= 1

    # For modular exponentiation, take % mod after each *= (result and x).
    return result
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x, n) {

}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x, n) {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x, n) {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
  let result = 1;
}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x, n) {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
  let result = 1;

    // If the lowest bit of n is set, the current power of x belongs in the result.
}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x, n) {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
  let result = 1;

  while (n > 0) {
    // If the lowest bit of n is set, the current power of x belongs in the result.
    if ((n & 1) === 1) {
      result *= x;
    }
  }
}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x, n) {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
  let result = 1;

  while (n > 0) {
    // If the lowest bit of n is set, the current power of x belongs in the result.
    if ((n & 1) === 1) {
      result *= x;
    }

    // Move to the next bit: square the base and shift n right by one.
  }
}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x, n) {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
  let result = 1;

  while (n > 0) {
    // If the lowest bit of n is set, the current power of x belongs in the result.
    if ((n & 1) === 1) {
      result *= x;
    }

    // Move to the next bit: square the base and shift n right by one.
    x *= x;
    n >>= 1;
  }
}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x, n) {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
  let result = 1;

  while (n > 0) {
    // If the lowest bit of n is set, the current power of x belongs in the result.
    if ((n & 1) === 1) {
      result *= x;
    }

    // Move to the next bit: square the base and shift n right by one.
    x *= x;
    n >>= 1;
  }

  // For modular exponentiation, take % mod after each *= (result and x).
}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x, n) {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
  let result = 1;

  while (n > 0) {
    // If the lowest bit of n is set, the current power of x belongs in the result.
    if ((n & 1) === 1) {
      result *= x;
    }

    // Move to the next bit: square the base and shift n right by one.
    x *= x;
    n >>= 1;
  }

  // For modular exponentiation, take % mod after each *= (result and x).
  return result;
}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x: number, n: number): number {

}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x: number, n: number): number {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x: number, n: number): number {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
  let result = 1;
}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x: number, n: number): number {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
  let result = 1;

    // If the lowest bit of n is set, the current power of x belongs in the result.
}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x: number, n: number): number {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
  let result = 1;

  while (n > 0) {
    // If the lowest bit of n is set, the current power of x belongs in the result.
    if ((n & 1) === 1) {
      result *= x;
    }
  }
}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x: number, n: number): number {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
  let result = 1;

  while (n > 0) {
    // If the lowest bit of n is set, the current power of x belongs in the result.
    if ((n & 1) === 1) {
      result *= x;
    }

    // Move to the next bit: square the base and shift n right by one.
  }
}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x: number, n: number): number {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
  let result = 1;

  while (n > 0) {
    // If the lowest bit of n is set, the current power of x belongs in the result.
    if ((n & 1) === 1) {
      result *= x;
    }

    // Move to the next bit: square the base and shift n right by one.
    x *= x;
    n >>= 1;
  }
}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x: number, n: number): number {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
  let result = 1;

  while (n > 0) {
    // If the lowest bit of n is set, the current power of x belongs in the result.
    if ((n & 1) === 1) {
      result *= x;
    }

    // Move to the next bit: square the base and shift n right by one.
    x *= x;
    n >>= 1;
  }

  // For modular exponentiation, take % mod after each *= (result and x).
}
// power returns x^n for n >= 0 in O(log n) multiplications.
function power(x: number, n: number): number {
  // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
  let result = 1;

  while (n > 0) {
    // If the lowest bit of n is set, the current power of x belongs in the result.
    if ((n & 1) === 1) {
      result *= x;
    }

    // Move to the next bit: square the base and shift n right by one.
    x *= x;
    n >>= 1;
  }

  // For modular exponentiation, take % mod after each *= (result and x).
  return result;
}
class Solution {
    // power returns x^n for n >= 0 in O(log n) multiplications.
    public long power(long x, int n) {

    }
}
class Solution {
    // power returns x^n for n >= 0 in O(log n) multiplications.
    public long power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    }
}
class Solution {
    // power returns x^n for n >= 0 in O(log n) multiplications.
    public long power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long result = 1;
    }
}
class Solution {
    // power returns x^n for n >= 0 in O(log n) multiplications.
    public long power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long result = 1;

            // If the lowest bit of n is set, the current power of x belongs in the result.
    }
}
class Solution {
    // power returns x^n for n >= 0 in O(log n) multiplications.
    public long power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long result = 1;

        while (n > 0) {
            // If the lowest bit of n is set, the current power of x belongs in the result.
            if ((n & 1) == 1) {
                result *= x;
            }
        }
    }
}
class Solution {
    // power returns x^n for n >= 0 in O(log n) multiplications.
    public long power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long result = 1;

        while (n > 0) {
            // If the lowest bit of n is set, the current power of x belongs in the result.
            if ((n & 1) == 1) {
                result *= x;
            }

            // Move to the next bit: square the base and shift n right by one.
        }
    }
}
class Solution {
    // power returns x^n for n >= 0 in O(log n) multiplications.
    public long power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long result = 1;

        while (n > 0) {
            // If the lowest bit of n is set, the current power of x belongs in the result.
            if ((n & 1) == 1) {
                result *= x;
            }

            // Move to the next bit: square the base and shift n right by one.
            x *= x;
            n >>= 1;
        }
    }
}
class Solution {
    // power returns x^n for n >= 0 in O(log n) multiplications.
    public long power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long result = 1;

        while (n > 0) {
            // If the lowest bit of n is set, the current power of x belongs in the result.
            if ((n & 1) == 1) {
                result *= x;
            }

            // Move to the next bit: square the base and shift n right by one.
            x *= x;
            n >>= 1;
        }

        // For modular exponentiation, take % mod after each *= (result and x).
    }
}
class Solution {
    // power returns x^n for n >= 0 in O(log n) multiplications.
    public long power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long result = 1;

        while (n > 0) {
            // If the lowest bit of n is set, the current power of x belongs in the result.
            if ((n & 1) == 1) {
                result *= x;
            }

            // Move to the next bit: square the base and shift n right by one.
            x *= x;
            n >>= 1;
        }

        // For modular exponentiation, take % mod after each *= (result and x).
        return result;
    }
}
public class Solution {
    // Power returns x^n for n >= 0 in O(log n) multiplications.
    public long Power(long x, int n) {

    }
}
public class Solution {
    // Power returns x^n for n >= 0 in O(log n) multiplications.
    public long Power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    }
}
public class Solution {
    // Power returns x^n for n >= 0 in O(log n) multiplications.
    public long Power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long result = 1;
    }
}
public class Solution {
    // Power returns x^n for n >= 0 in O(log n) multiplications.
    public long Power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long result = 1;

            // If the lowest bit of n is set, the current power of x belongs in the result.
    }
}
public class Solution {
    // Power returns x^n for n >= 0 in O(log n) multiplications.
    public long Power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long result = 1;

        while (n > 0) {
            // If the lowest bit of n is set, the current power of x belongs in the result.
            if ((n & 1) == 1) {
                result *= x;
            }
        }
    }
}
public class Solution {
    // Power returns x^n for n >= 0 in O(log n) multiplications.
    public long Power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long result = 1;

        while (n > 0) {
            // If the lowest bit of n is set, the current power of x belongs in the result.
            if ((n & 1) == 1) {
                result *= x;
            }

            // Move to the next bit: square the base and shift n right by one.
        }
    }
}
public class Solution {
    // Power returns x^n for n >= 0 in O(log n) multiplications.
    public long Power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long result = 1;

        while (n > 0) {
            // If the lowest bit of n is set, the current power of x belongs in the result.
            if ((n & 1) == 1) {
                result *= x;
            }

            // Move to the next bit: square the base and shift n right by one.
            x *= x;
            n >>= 1;
        }
    }
}
public class Solution {
    // Power returns x^n for n >= 0 in O(log n) multiplications.
    public long Power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long result = 1;

        while (n > 0) {
            // If the lowest bit of n is set, the current power of x belongs in the result.
            if ((n & 1) == 1) {
                result *= x;
            }

            // Move to the next bit: square the base and shift n right by one.
            x *= x;
            n >>= 1;
        }

        // For modular exponentiation, take % mod after each *= (result and x).
    }
}
public class Solution {
    // Power returns x^n for n >= 0 in O(log n) multiplications.
    public long Power(long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long result = 1;

        while (n > 0) {
            // If the lowest bit of n is set, the current power of x belongs in the result.
            if ((n & 1) == 1) {
                result *= x;
            }

            // Move to the next bit: square the base and shift n right by one.
            x *= x;
            n >>= 1;
        }

        // For modular exponentiation, take % mod after each *= (result and x).
        return result;
    }
}
// power returns x^n for n >= 0 in O(log n) multiplications.
long long power(long long x, int n) {

}
// power returns x^n for n >= 0 in O(log n) multiplications.
long long power(long long x, int n) {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
}
// power returns x^n for n >= 0 in O(log n) multiplications.
long long power(long long x, int n) {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    long long result = 1;
}
// power returns x^n for n >= 0 in O(log n) multiplications.
long long power(long long x, int n) {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    long long result = 1;

        // If the lowest bit of n is set, the current power of x belongs in the result.
}
// power returns x^n for n >= 0 in O(log n) multiplications.
long long power(long long x, int n) {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    long long result = 1;

    while (n > 0) {
        // If the lowest bit of n is set, the current power of x belongs in the result.
        if ((n & 1) == 1) {
            result *= x;
        }
    }
}
// power returns x^n for n >= 0 in O(log n) multiplications.
long long power(long long x, int n) {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    long long result = 1;

    while (n > 0) {
        // If the lowest bit of n is set, the current power of x belongs in the result.
        if ((n & 1) == 1) {
            result *= x;
        }

        // Move to the next bit: square the base and shift n right by one.
    }
}
// power returns x^n for n >= 0 in O(log n) multiplications.
long long power(long long x, int n) {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    long long result = 1;

    while (n > 0) {
        // If the lowest bit of n is set, the current power of x belongs in the result.
        if ((n & 1) == 1) {
            result *= x;
        }

        // Move to the next bit: square the base and shift n right by one.
        x *= x;
        n >>= 1;
    }
}
// power returns x^n for n >= 0 in O(log n) multiplications.
long long power(long long x, int n) {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    long long result = 1;

    while (n > 0) {
        // If the lowest bit of n is set, the current power of x belongs in the result.
        if ((n & 1) == 1) {
            result *= x;
        }

        // Move to the next bit: square the base and shift n right by one.
        x *= x;
        n >>= 1;
    }

    // For modular exponentiation, take % mod after each *= (result and x).
}
// power returns x^n for n >= 0 in O(log n) multiplications.
long long power(long long x, int n) {
    // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    long long result = 1;

    while (n > 0) {
        // If the lowest bit of n is set, the current power of x belongs in the result.
        if ((n & 1) == 1) {
            result *= x;
        }

        // Move to the next bit: square the base and shift n right by one.
        x *= x;
        n >>= 1;
    }

    // For modular exponentiation, take % mod after each *= (result and x).
    return result;
}
class Solution {
public:
    // power returns x^n for n >= 0 in O(log n) multiplications.
    long long power(long long x, int n) {

    }
};
class Solution {
public:
    // power returns x^n for n >= 0 in O(log n) multiplications.
    long long power(long long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
    }
};
class Solution {
public:
    // power returns x^n for n >= 0 in O(log n) multiplications.
    long long power(long long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long long result = 1;
    }
};
class Solution {
public:
    // power returns x^n for n >= 0 in O(log n) multiplications.
    long long power(long long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long long result = 1;

            // If the lowest bit of n is set, the current power of x belongs in the result.
    }
};
class Solution {
public:
    // power returns x^n for n >= 0 in O(log n) multiplications.
    long long power(long long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long long result = 1;

        while (n > 0) {
            // If the lowest bit of n is set, the current power of x belongs in the result.
            if ((n & 1) == 1) {
                result *= x;
            }
        }
    }
};
class Solution {
public:
    // power returns x^n for n >= 0 in O(log n) multiplications.
    long long power(long long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long long result = 1;

        while (n > 0) {
            // If the lowest bit of n is set, the current power of x belongs in the result.
            if ((n & 1) == 1) {
                result *= x;
            }

            // Move to the next bit: square the base and shift n right by one.
        }
    }
};
class Solution {
public:
    // power returns x^n for n >= 0 in O(log n) multiplications.
    long long power(long long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long long result = 1;

        while (n > 0) {
            // If the lowest bit of n is set, the current power of x belongs in the result.
            if ((n & 1) == 1) {
                result *= x;
            }

            // Move to the next bit: square the base and shift n right by one.
            x *= x;
            n >>= 1;
        }
    }
};
class Solution {
public:
    // power returns x^n for n >= 0 in O(log n) multiplications.
    long long power(long long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long long result = 1;

        while (n > 0) {
            // If the lowest bit of n is set, the current power of x belongs in the result.
            if ((n & 1) == 1) {
                result *= x;
            }

            // Move to the next bit: square the base and shift n right by one.
            x *= x;
            n >>= 1;
        }

        // For modular exponentiation, take % mod after each *= (result and x).
    }
};
class Solution {
public:
    // power returns x^n for n >= 0 in O(log n) multiplications.
    long long power(long long x, int n) {
        // x^n = product of x^(2^b) for each set bit b of n. Start the product at 1.
        long long result = 1;

        while (n > 0) {
            // If the lowest bit of n is set, the current power of x belongs in the result.
            if ((n & 1) == 1) {
                result *= x;
            }

            // Move to the next bit: square the base and shift n right by one.
            x *= x;
            n >>= 1;
        }

        // For modular exponentiation, take % mod after each *= (result and x).
        return result;
    }
};

Tap the left half to go back, the right half to go forward.

Arrow keys or space to navigate · Esc to exit.