Given two numbers as strings, find if one is a power of other
Given two large numbers as strings, find if one is the power of another. For Example :
Examples:
Input : a = "374747", b = "52627712618930723" Output : YES Explanation : 374747^3 = 52627712618930723 Input : a = "2", b = "4099" Output : NO
Prerequisite : Multiply two large numbers represented as string
The approach is simple. First, find smaller of the two strings and then multiply it with itself until it becomes equal to or greater than the larger string. If they are equal, return true, else return false.
Below is the implementation of above approach
C++
// CPP program to check if one number is // power of other #include <bits/stdc++.h> using namespace std; bool isGreaterThanEqualTo(string s1, string s2) { if (s1.size() > s2.size()) return true ; return (s1 == s2); } string multiply(string s1, string s2) { int n = s1.size(); int m = s2.size(); vector< int > result(n + m, 0); // Multiply the numbers. It multiplies // each digit of second string to each // digit of first and stores the result. for ( int i = n - 1; i >= 0; i--) for ( int j = m - 1; j >= 0; j--) result[i + j + 1] += (s1[i] - '0' ) * (s2[j] - '0' ); // If the digit exceeds 9, add the // cumulative carry to previous digit. int size = result.size(); for ( int i = size - 1; i > 0; i--) { if (result[i] >= 10) { result[i - 1] += result[i] / 10; result[i] = result[i] % 10; } } int i = 0; while (i < size && result[i] == 0) i++; // if all zeroes, return "0". if (i == size) return "0" ; string temp; // Remove starting zeroes. while (i < size) { temp += (result[i] + '0' ); i++; } return temp; } // Removes Extra zeroes from front of a string. string removeLeadingZeores(string s) { int n = s.size(); int i = 0; while (i < n && s[i] == '0' ) i++; if (i == n) return "0" ; string temp; while (i < n) temp += s[i++]; return temp; } bool isPower(string s1, string s2) { // Make sure there are no leading zeroes // in the string. s1 = removeLeadingZeores(s1); s2 = removeLeadingZeores(s2); if (s1 == "0" || s2 == "0" ) return false ; if (s1 == "1" && s2 == "1" ) return true ; if (s1 == "1" || s2 == "1" ) return true ; // Making sure that s1 is smaller. // If it is greater, we recur we // reversed parameters. if (s1.size() > s2.size()) return isPower(s2, s1); string temp = s1; while (!isGreaterThanEqualTo(s1, s2)) s1 = multiply(s1, temp); return s1 == s2; } int main() { string s1 = "374747" , s2 = "52627712618930723" ; cout << (isPower(s1, s2) ? "YES\n" : "NO\n" ); s1 = "4099" , s2 = "2" ; cout << (isPower(s1, s2) ? "YES\n" : "NO\n" ); return 0; } |
Java
// Java program to check if one number is // power of other import java.util.*; class new_file { static boolean isGreaterThanEqual(String s1, String s2) { if (s1.length() > s2.length()) return true ; if (s1.compareTo(s2) == 0 ) return true ; return false ; } static String multiply(String s1, String s2) { int n = s1.length(); int m = s2.length(); int [] result = new int [n + m]; // Multiply the numbers. It multiplies // each digit of second string to each // digit of first and stores the result. for ( int i = n - 1 ; i >= 0 ; i--) for ( int j = m - 1 ; j >= 0 ; j--) result[i + j + 1 ] += (s1.charAt(i) - '0' ) * (s2.charAt(j) - '0' ); // If the digit exceeds 9, add the // cumulative carry to previous digit. int size = result.length; for ( int i = size - 1 ; i > 0 ; i--) { if (result[i] >= 10 ) { result[i - 1 ] += result[i] / 10 ; result[i] = result[i] % 10 ; } } int i = 0 ; while (i < size && result[i] == 0 ) i++; // if all zeroes, return "0". if (i == size) return "0" ; String temp = "" ; // Remove starting zeroes. while (i < size) { temp += Integer.toString(result[i]); i++; } return temp; } // Removes Extra zeroes from front of a string. static String removeLeadingZeores(String s) { int n = s.length(); int i = 0 ; while (i < n && s.charAt(i) == '0' ) i++; if (i == n) return "0" ; String temp = "" ; while (i < n) { temp += s.charAt(i); i++; } return temp; } static boolean isPower(String s1, String s2) { // Make sure there are no leading zeroes // in the string. s1 = removeLeadingZeores(s1); s2 = removeLeadingZeores(s2); if (s1 == "0" || s2 == "0" ) return false ; if (s1 == "1" && s2 == "1" ) return true ; if (s1 == "1" || s2 == "1" ) return true ; // Making sure that s1 is smaller. // If it is greater, we recur we // reversed parameters. if (s1.length() > s2.length()) return isPower(s2, s1); String temp = new String(s1); while (!isGreaterThanEqual(s1, s2)) s1 = multiply(s1, temp); if (s1.compareTo(s2) == 0 ) return true ; return false ; } // Driver Code public static void main(String[] args) { String s1 = "374747" , s2 = "52627712618930723" ; if (isPower(s1, s2)) System.out.println( "Yes" ); else System.out.println( "No" ); s1 = "4099" ; s2 = "2" ; if (isPower(s1, s2)) System.out.println( "Yes" ); else System.out.println( "No" ); } } // This code is contributed by // sanjeev2552 |
Python3
# Python3 program to check if one # number is a power of other def isGreaterThanEqualTo(s1, s2): if len (s1) > len (s2): return True return s1 = = s2 def multiply(s1, s2): n, m = len (s1), len (s2) result = [ 0 ] * (n + m) # Multiply the numbers. It multiplies # each digit of second string to each # digit of first and stores the result. for i in range (n - 1 , - 1 , - 1 ): for j in range (m - 1 , - 1 , - 1 ): result[i + j + 1 ] + = ( int (s1[i]) * int (s2[j])) # If the digit exceeds 9, add the # cumulative carry to previous digit. size = len (result) for i in range (size - 1 , 0 , - 1 ): if result[i] > = 10 : result[i - 1 ] + = result[i] / / 10 result[i] = result[i] % 10 i = 0 while i < size and result[i] = = 0 : i + = 1 # If all zeroes, return "0". if i = = size: return "0" temp = "" # Remove starting zeroes. while i < size: temp + = str (result[i]) i + = 1 return temp # Removes Extra zeroes from front of a string. def removeLeadingZeores(s): n, i = len (s), 0 while i < n and s[i] = = '0' : i + = 1 if i = = n: return "0" temp = "" while i < n: temp + = s[i] i + = 1 return temp def isPower(s1, s2): # Make sure there are no leading # zeroes in the string. s1 = removeLeadingZeores(s1) s2 = removeLeadingZeores(s2) if s1 = = "0" or s2 = = "0" : return False if s1 = = "1" and s2 = = "1" : return True if s1 = = "1" and s2 = = "1" : return True # Making sure that s1 is smaller. # If it is greater, we recur we # reversed parameters. if len (s1) > len (s2): return isPower(s2, s1) temp = s1 while not isGreaterThanEqualTo(s1, s2): s1 = multiply(s1, temp) return s1 = = s2 # Driver Code if __name__ = = "__main__" : s1, s2 = "374747" , "52627712618930723" if isPower(s1, s2): print ( "YES" ) else : print ( "NO" ) s1, s2 = "4099" , "2" if isPower(s1, s2): print ( "YES" ) else : print ( "NO" ) # This code is contributed by Rituraj Jain |
OUTPUT
YES NO
Recommended Posts:
- Modulo power for large numbers represented as strings
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Find the numbers of strings that can be formed after processing Q queries
- Find power of power under mod of a prime
- Minimum number of sub-strings of a string such that all are power of 5
- Sum of fourth power of first n even natural numbers
- Writing power function for large numbers
- Find whether a given integer is a power of 3 or not
- Find sub-string with given power
- Find value of y mod (2 raised to power x)
- Find whether a given number is a power of 4 or not
- Program to find whether a no is power of two
- Find the super power of a given Number
- Find multiple of x closest to or a ^ b (a raised to power b)
- Find unit digit of x raised to power y
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.