Open In App

Find the value of a number raised to its reverse

Last Updated : 27 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a number n and its reverse r. The task is to find the number obtained when the number is raised to the power of its own reverse. The answer can be very large, return the result modulo 109+7.

Examples:

 Input : n = 2, r = 2
Output: 4
Explanation: Number 2 raised to the power of its reverse 2 gives 4.

Input : n = 57, r = 75
Output: 262042770
Explanation: 5775 modulo 109+7 gives us the result as 262042770

[Naive Approach] - Multiply One by One - O(r) Time and O(1) Space

The idea is to traverse a loop from 1 to r (reverse) and multiply the result (initialized to 1) with n in each iteration.

Follow the below given steps:

  • Create a variable res and initialize it to 1 to store the final result.
  • Then, start a loop from 1 and it goes till r.
    • Multiply the variable res with n.
  • Return the result res with modulo of 1e9+7.

Below is given the implementation:

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007

int revPower(int n, int r) {

    // to store result
    int res = 1;

    // multiply res with n, r times
    for(int i = 0; i < r; i++) {
        res = (res * n) % mod;
    }
    return res;
}

signed main() {
    int n = 57, r = 75;
    cout << revPower(n, r);
    return 0;
}
Java
public class GfG {

    static final long mod = 1000000007;

    // to compute n raised to the power r under modulo
    public static long revPower(long n, long r) {

        // to store result
        long res = 1;

        // multiply res with n, r times
        for(long i = 0; i < r; i++) {
            res = (res * n) % mod;
        }
        return res;
    }

    public static void main(String[] args) {
        long n = 57, r = 75;
        System.out.print(revPower(n, r));
    }
}
Python
mod = 1000000007

def revPower(n, r):

    # to store result
    res = 1

    # multiply res with n, r times
    for i in range(r):
        res = (res * n) % mod
    return res

if __name__ == "__main__":
    n = 57; r = 75
    print(revPower(n, r))
C#
using System;

public class GfG {

    const long mod = 1000000007;

    // to compute n raised to the power r under modulo
    public static long revPower(long n, long r) {

        // to store result
        long res = 1;

        // multiply res with n, r times
        for(long i = 0; i < r; i++) {
            res = (res * n) % mod;
        }
        return res;
    }

    public static void Main(string[] args) {
        long n = 57, r = 75;
        Console.Write(revPower(n, r));
    }
}
JavaScript
const mod = 1000000007;

function revPower(n, r) {

    // to store result
    let res = 1;

    // multiply res with n, r times
    for (let i = 0; i < r; i++) {
        res = (res * n) % mod;
    }
    return res;
}

const n = 57, r = 75;
console.log(revPower(n, r));

Output
262042770

[Expected Approach] - Using Bit Manipulation - O(log r) Time and O(1) Space

The idea is to use binary exponentiation (also known as “exponentiation by squaring”) to compute the result in logarithmic time.

  • Every number can be written as the sum of powers of 2
  • Traverse through all the bits of r from LSB (Least Significant Bit) to MSB (Most Significant Bit) in O(log n) time. In each iteration, make the multiplier x as square of x.,
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define mod 1000000007

int revPower(int n, int r) {

    int res = 1;
    int x = n;

    while (r > 0) {

        // When reverse is odd
        if (r & 1) {
            res = (res * x) % mod;
        }

        x = (x * x) % mod;

        // Shifting right (r = r/2 )
        r >>= 1;
    }

    return res;
}

signed main() {
    int n = 57, r = 75;
    cout << revPower(n, r);
    return 0;
}
Java
class GfG {

    static final long mod = 1000000007;

    public static long revPower(long n, long r) {

        long res = 1;
        long x = n;

        while (r > 0) {

            // When reverse is odd
            if ((r & 1) != 0) {
                res = (res * x) % mod;
            }

            x = (x * x) % mod;

            // Shifting right (r = r/2 )
            r >>= 1;
        }

        return res;
    }

    public static void main(String[] args) {
        long n = 57, r = 75;
        System.out.print(revPower(n, r));
    }
}
Python
mod = 1000000007

def revPower(n, r):

    res = 1
    x = n

    while r > 0:

        # When reverse is odd
        if r & 1:
            res = (res * x) % mod

        x = (x * x) % mod

        # Shifting right (r = r/2 )
        r >>= 1

    return res

if __name__ == "__main__":
    n = 57; r = 75
    print(revPower(n, r))
C#
using System;

public class GfG {

    const long mod = 1000000007;

    public static long revPower(long n, long r) {

        long res = 1;
        long x = n;

        while (r > 0) {

            // When reverse is odd
            if ((r & 1) != 0) {
                res = (res * x) % mod;
            }

            x = (x * x) % mod;

            // Shifting right (r = r/2 )
            r >>= 1;
        }

        return res;
    }

    public static void Main(string[] args) {
        long n = 57, r = 75;
        Console.Write(revPower(n, r));
    }
}
JavaScript
const mod = 1000000007;

function revPower(n, r) {

    let res = 1;
    let x = n;

    while (r > 0) {

        // When reverse is odd
        if (r % 2 == 1) {
            res = (res * x) % mod;
        }

        x = (x * x) % mod;

        // Shifting right (r = r/2 )
        r = Math.floor(r / 2);
    }

    return res;
}

const n = 57, r = 75;
console.log(revPower(n, r));

Output
262042770

Similar Reads