🤖
@qwertyvipul | code
  • Code
  • DSA
    • Disjoint Set
    • Segment Tree
    • Bit Operations
    • Binary Exponential
    • Kadane's Algorithm
    • Modulus Multiplicative Inverse
  • Quick Notes
    • Design Patterns
    • System Design
    • React.js
  • LeetCode With JavaScript
Powered by GitBook
On this page
  1. DSA

Binary Exponential

def binpowRecursive(a, b):
    if b == 0: return 1

    res = binpow(a, b // 2)
    
    if b & 1: return res * res * a

    return res * res

# Test
print(binpowRecursive(10, 3), binpow(10, 3) == 10 ** 3)
print(binpowRecursive(8, 3), binpow(8, 3) == 8 ** 3)
def binpowIterative(a, b):
    res = 1
    while b > 0:
        if b & 1: res = res * a
        a = a * a
        b >>= 1
    return res

# Test
print(binpowIterative(10, 3), binpow(10, 3) == 10 ** 3)
print(binpowIterative(8, 3), binpow(8, 3) == 8 ** 3)
PreviousBit OperationsNextKadane's Algorithm

Last updated 1 year ago