Open In App

Find the value of a number raised to its reverse

Last Updated : 09 May, 2023
Improve
Improve
Like Article
Like
Save
Share
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 which gives 4 as a result after performing modulo 109+7

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

Naive Approach:

The easiest way to solve this problem could be to traverse a loop from 1 to R(reverse) and multiply our answer with N  in each iteration.

Follow the steps mentioned below to implement the idea:

  • Create a variable (say ans) and initialize it to 1 to store the final result.
  • Then, start a loop from 1 and it goes till R.
    • Multiply the variable ans with N.
  • Return the result ans with modulo of 1e9+7.

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return ans with modulo
int PowerOfNum(int N, int R)
{
    long long ans = 1, mod = 1e9 + 7;
    for (int i = 1; i <= R; i++) {
        ans *= N;
        ans %= mod;
    }
    return ans;
}
 
// Driver code
int main()
{
    int N = 57, R = 75;
 
    // Function call
    cout << PowerOfNum(N, R);
 
    return 0;
}


Java




// Java code addition
import java.io.*;
 
class GFG {
 
  // Function to return ans with modulo
  public static long PowerOfNum(int N, int R)
  {
    long ans = 1;
    long mod = 1000000000 + 7;
    for (int i = 1; i <= R; i++) {
      ans *= N;
      ans %= mod;
    }
    return ans;
  }
 
  public static void main(String[] args)
  {
    int N = 57, R = 75;
 
    // Function call
    System.out.println(PowerOfNum(N, R));
  }
}
 
// This code is contributed by lokesh


Python3




# Python3 code to implement the approach
 
# Function to return ans with modulo
def PowerOfNum(N, R):
    ans = 1
    mod = 1e9 + 7
    for i in range(1,R+1):
        ans *= N
        ans %= mod
    return ans
 
# Driver code
N = 57
R = 75
 
# Function call
print(int(PowerOfNum(N, R)))
 
# This code is contributed by akashish__


C#




using System;
 
public class GFG{
 
  // Function to return ans with modulo
  public static long PowerOfNum(int N, int R)
  {
    long ans = 1;
    long mod = 1000000000 + 7;
    for (int i = 1; i <= R; i++) {
      ans *= N;
      ans %= mod;
    }
    return ans;
  }
 
 
  static public void Main ()
  {
    int N = 57, R = 75;
 
    // Function call
    Console.WriteLine(PowerOfNum(N, R));
  }
}
 
// This code is contributed by akashish__


Javascript




// JS code implementation
  // Function to return ans with modulo
function PowerOfNum(N,R)
  {
    let ans = 1;
    let mod = 1000000000 + 7;
    for (let i = 1; i <= R; i++) {
      ans *= N;
      ans %= mod;
    }
    return ans;
  }
 
 
    let N = 57, R = 75;
 
    // Function call
    console.log(PowerOfNum(N, R));
 
// This code is contributed by ksam24000


Output

262042770

Time Complexity: O(R)
Auxiliary Space: O(1)

Find the value of a number raised to its reverse using Recursion

We can use the same approach as above but instead of an iterative loop, we can use recursion for the purpose.

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
 
long long power(long long x, long long n)
{
    // If x^0 return 1
    if (n == 0)
        return 1;
 
    // If we need to find of 0^y
    if (x == 0)
        return 0;
 
    // For all other cases
    return (x * power(x, n - 1)) % mod;
}
 
// Driver Code
int main()
{
    long long x = 57;
    long long n = 75;
 
    // Function call
    cout << (power(x, n));
}


C




#include <stdio.h>
 
// Function to calculate power (x^n) modulo MOD
long long power(long long x, long long n) {
    if (n == 0)
        return 1; // x^0 = 1
     
    if (x == 0)
        return 0; // 0^y = 0, where y is any positive integer
     
  // Recursive calculation of power modulo MOD
    return (x * power(x, n - 1)) % 1000000007;
}
 
int main() {
    long long x = 57;
    long long n = 75;
     
    printf("%lld\n", power(x, n)); // Print the result of power(x, n)
     
    return 0;
}


Java




public class Main {
    static final long MOD = 1000000007;
 
    // Function to calculate power (x^n) modulo MOD
    static long power(long x, long n) {
        if (n == 0)
            return 1; // x^0 = 1
         
        if (x == 0)
            return 0; // 0^y = 0, where y is any positive integer
         
       // Recursive calculation of power modulo MOD
        return (x * power(x, n - 1)) % MOD;
    }
 
    public static void main(String[] args) {
        long x = 57;
        long n = 75;
         
        System.out.println(power(x, n)); // Print the result of power(x, n)
    }
}


Python3




def power(x, n):
    if n == 0:
        return 1  # x^0 = 1
     
    if x == 0:
        return 0  # 0^y = 0, where y is any positive integer
     
    # Recursive calculation of power modulo MOD
    return (x * power(x, n - 1)) % 1000000007 
 
x = 57
n = 75
print(power(x, n))  # Print the result of power(x, n)


C#




using System;
 
public class Program {
    const long MOD = 1000000007;
 
    // Function to calculate power (x^n) modulo MOD
    static long Power(long x, long n) {
        if (n == 0)
            return 1; // x^0 = 1
         
        if (x == 0)
            return 0; // 0^y = 0, where y is any positive integer
       
        // Recursive calculation of power modulo MOD
        return (x * Power(x, n - 1)) % MOD;
    }
 
    public static void Main(string[] args) {
        long x = 57;
        long n = 75;
         
        Console.WriteLine(Power(x, n)); // Print the result of power(x, n)
    }
}


Javascript




function power(x, n) {
    if (n == 0)
        return 1; // x^0 = 1
     
    if (x == 0)
        return 0; // 0^y = 0, where y is any positive integer
     
    // Recursive calculation of power modulo MOD
    return (x * power(x, n - 1)) % 1000000007;
}
 
var x = 57;
var n = 75;
console.log(power(x, n)); // Print the result of power(x, n)


Output

262042770

Time Complexity: O(R)
Auxiliary Space: O(R)

Find the value of a number raised to its reverse using Bit Manipulation

The efficient way of solving this problem could be bit manipulation, just break the problem into small parts and solve them here the concept of binary exponentiation method will be used.

  • Every number can be written as the sum of powers of 2
  • Traverse through all the bits of a number from LSB (Least Significant Bit) to MSB (Most Significant Bit) in O(log N) time.

Follow the steps mentioned below to implement the idea:

  •  First, create a variable (say ans) and initialize it to 1 to store the result.
  •  Then, check if the given reverse number is odd or not.
    • If yes, then multiply the answer with pow ans = (ans * pow)%mod.
    • Then, multiply the pow with pow i.e., pow = (pow*pow).
    • Start shifting right by R = R/2.
  • Finally, return the ans as result.

Below is the implementation of the above approach

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the power
long long power(int num, int rev)
{
    long long ans = 1;
    long long mod = 1e9 + 7;
    long long pow = num * 1LL;
 
    while (rev > 0) {
        // When reverse is odd
        if (rev & 1) {
            ans = (ans * pow) % mod;
        }
        pow = (pow * pow) % mod;
        // Shifting right (rev = rev/2 )
        rev >>= 1;
    }
    return ans;
}
 
// Driver Code
int main()
{
    int N = 57, R = 75;
 
    // Function call
    cout << power(N, R) << endl;
 
    return 0;
}


Java




// Java code to implement the approach
 
import java.io.*;
 
class GFG {
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 57, R = 75;
 
        // Function call
        System.out.println(power(N, R));
    }
 
    // Function to find the power
    static long power(int num, int rev)
    {
        long ans = 1;
        long mod = 1000000007, pow = num * 1L;
        while (rev > 0) {
 
            // When rev is odd
            if (rev % 2 == 1) {
                ans = (ans * pow) % mod;
            }
            pow = (pow * pow) % mod;
 
            // Shifting right (rev = rev/2 )
            rev >>= 1;
        }
        return ans;
    }
}


Python3




# Python code to implement the above approach
def power(num, rev):
 
    ans = 1
    mod = 1000000007
    pow = num * 1
     
    while (rev > 0) :
 
        # When rev is odd
        if (rev % 2 == 1) :
            ans = (ans * pow) % mod
             
        pow = (pow * pow) % mod
 
        # Shifting right (rev = rev/2 )
        rev >>= 1
             
    return ans
 
if __name__ == "__main__":
 
    N = 57
    R = 75
 
    # Function call
    print(power(N, R))
     
    # This code is contributed by sanjoy_62.


C#




// C# code to implement the approach
 
using System;
 
public class GFG {
 
    static public void Main()
    {
 
        // Code
        int N = 57, R = 75;
 
        // Function call
        Console.WriteLine(power(N, R));
    }
 
    // Function to find the power
    static long power(int num, int rev)
    {
        long ans = 1;
        long mod = 1000000007, pow = num * 1L;
        while (rev > 0) {
 
            // When rev is odd
            if (rev % 2 == 1) {
                ans = (ans * pow) % mod;
            }
            pow = (pow * pow) % mod;
 
            // Shifting right (rev = rev/2 )
            rev >>= 1;
        }
        return ans;
    }
}
 
// This code is contributed by lokeshmvs21.


Javascript




// JavaScript code to implement the approach
const mod = 1000000007;
 
// Function to find the power
function power(num, rev) {
    let ans = 1;
    let pow = num * 1; // Use the "n" suffix to specify that //pow should be a bigint
    while (rev > 0) {
        // When rev is odd
        if (rev % 2 == 1) {
            ans = (ans * pow) % mod;
        }
        pow = (pow * pow) % mod;
 
        // Shifting right (rev = rev/2 )
        rev >>= 1;
    }
    return ans;
}
 
// Driver code
const N = 57, R = 75;
 
// Function call
console.log(power(N, R));
 
// This code is contributed by ksam24000


Output

262042770

Time Complexity: O(log R)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads