logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
instagramtwitterfacebookyoutubelinkedinmailgithubrss
Copyright © 2026 All rights reserved.
•
VIKI
Built with Vikramaditya Singh
Built with
VIKI - A posts created with Next.js and Tailwind.css
About
arrays
Arrays
Boyer-Moore Majority Vote Algorithm
Kadane’s Algorithm
Triple Reverse algorithm aka Reversal Algorithm
graph
Cycle detection
Graph
Topological Sort
Tree Traversal
heap
Binary Heap
Binomial Heap
Heap
linked-list
Cycle Detection
Linked List
Sorting
miscellaneous
Finding the majority element
Rotate array by K steps
numerical-algorithms
Finding If Number is Prime
GCD of two numbers
Performing Exponentiation
Prime Numbers
Randomising Data
partitioning
3. Max Consecutive Ones
Hoare'S Partition Scheme
Lomuto Partition Scheme
queue
Deques
Priority Queue
Queue
search
The Binary Search Master Guide
The Rotated Sorted Array Master Guide
sort
Bubble Sort
Counting Sort
Sorting
Insertion Sort
Merge Sort
Pigeon Hole Sort
Quick Sort
Selection Sort
stack
Monotonic Stack
Monotonic Stack
Stack
Reversing an Array
Stack
Tower of Hanoi
tree
Binary Tree Traversal
Centre of gravity
Diameter of Tree
Shortest Path
Algorithmic Pattern›numerical algorithms

Performing Exponentiation

Method is based on the fact that can quickly calculate powers of a number that are themselves powers of 2.

A8=A4∗A4A4=A2∗A2A^8 = A^4*A^4 \\ A^4 = A^2*A^2A8=A4∗A4A4=A2∗A2

# Fast Modulo Multiplication

For two numbers base and exp, to compute baseexp\text{base}^{\text{exp}}baseexp under Modulo 109+710^9+7109+7

static long exponentiation(long base, long exp) {
    long t = 1L;
    while (exp > 0) {
 
        // for cases where exponent
        // is not an even value
        if (exp % 2 != 0)
            t = (t * base) % N;
 
        base = (base * base) % N;
        exp /= 2;
    }
    return t % N;
}
  • Time Complexity = O(log⁡exp)\text{O}(\log{exp})O(logexp)
  • Space Complexity = O(1)\text{O}(1)O(1)

# Bit-Manipulation Method

The basis for the fast exponentiation algorithm is to build bigger and bigger powers of A and use the binary digits of the exponent to decide which of those should be multiplied into the final result.

The steps of the algorithm are as follows

  1. Initialize a result variable to 1, and a base variable to the given base value.
  2. Iterate over the bits of the binary representation of the exponent, from right to left.
    1. For each bit, square the current value of the base.
    2. If the current bit is 1, multiply the result variable by the current value of the base.
    3. Continue the iteration until all bits of the exponent have been processed.

3. Return the result variable modulo the given modulus value.

One limitation of this algorithm is that values raised to large powers grow extremely large.

Reducing each number with the modulus makes each step slightly slower, but can calculate values of practically unlimited size.

\

PreviousGCD of two numbersNextPrime Numbers
CONTENTS
Fast Modulo MultiplicationBit-Manipulation Method