Open in App
Not now

# Find the value of a number raised to its reverse

• Difficulty Level : Basic
• Last Updated : 17 Jan, 2023

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 ` `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)

#### Efficient Approach: 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 ` `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)

My Personal Notes arrow_drop_up
Related Articles