Given two numbers as strings, find if one is a power of other


Assume we have a data set of some numbers as a string and denoted as str[]. Now the task is to multiply the two large numbers which represents a string here. We need to find out from those two numbers as strings, find if one is a power of other.

Here is a general example of the process −

Input for the process us:
a = "374747", b = "52627712618930723"
Output : YES THE VALUE IS HERE
Explanation of the process:
374747^3 = 52627712618930723
Input for the process is :
a = "2", b = "4099"
Output : NO THE VALUE IS NOT HERE

Algorithm to find a power of other from two numbers as strings

In this possible algorithm, we are going to perform to check if one number is power of other from the given two numbers as string in a C++ environment. With this algorithm, we will build some C++ syntax to learn about the problem statement in an efficient manner.

  • Step 1 − Start the process.

  • Step 2 − Declare the input output stream.

  • Step 3 − Import the built-in classes and declared functions.

  • Step 4 − Declare a string function to find the value.

  • Step 5 − If first one is greater than string two, then return the true.

  • Step 6 − Declare a value for multiplied string.

  • Step 7 − Multiply the numbers.

  • Step 8 − Declare a for loop to iterate the method.

  • Step 9 − If the digit exceeds the value of number 9, add the cumulative carry to the previous digit.

  • Step 10 − If all values are zero, then return the value zero.

  • Step 11 − Start to remove the value of zero.

  • Step 12 − Remove the extra zeros.

  • Step 13 − Remove the leading the value of zero.

  • Step 14 − Return the power the value.

  • Step 15 − Declare the string one and two and define the value is available or not.

  • Step 16 − Return the value and terminate the process.

Syntax to find a power of other from two numbers as strings

int num;
   cout << "ASKING FOR THE INPUT STATEMENT: ";
   cin >> num;
   if((num != 0) && ((num &(num - 1)) == 0))
      cout << num << " IS A POWER OF THE NUMBER." << endl;
   else
      cout << num << " NOT A POWER OF THE NUMBER." << endl;
int checkPowerofTwo(int x){
   if (x == 0)
   return 0;
   while( x != 1){
      if(x % 2 != 0)
      return 0;
      x /= 2;
   }
return 1;

In this possible syntax, we are going to perform to check if one number is power of other from the given two numbers as string in a C++ environment. With these possible syntax, we will build some C++ example codes to explain the problem statement in a broad manner.

Approaches to Follow

  • Approach 1 − C++ program to check if one number is power of other by using string multiply(string one, string two), isPower(), logarithm function and compare method

  • Approach 2 − C++ program to check if one number is power of other by using binary search with loop changing, CheckPowerofNumber() and traversing method

Approach 1: Using String Multiply(string one, String two), IsPower(), Logarithm Function and Compare Method

Use of String Multiply(string one, String two) Method

In this possible approach, we are going to apply using string multiply(string one, string two) method to find if the one is a power of other form the given two numbers as a string.

last = powers[0] = y;
for (i = 1; last < x; i++){
   last *= last;
   powers[i] = last;
}
while (x >= y) {
	unsigned long top = powers[--i];
	if (x >= top) {
		unsigned long x1 = x / top;
		if (x1 * top != x) return 0;
		x = x1;
	}
}
return (x == 1);
sort(A, A+n);
for (int i=0; i<n; i++){
	for (int j=i+1; j<n; j++){
		int x = 0;
		while ((A[i]*pow(k, x)) <= A[j]){
			if ((A[i]*pow(k, x)) == A[j]){
				ans++;
				break;
			}
			x++;
		}
	}
}
return ans;

Example

//C++ program to check if one number is power of other by using string multiply(string one, string two)
#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);
   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');
   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 (i == size)
   return "0";
   string temp;
   while (i < size){
      temp += (result[i] + '0');
      i++;
   }
   return temp;
}
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){
   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;
   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 = "1610", s2 = "52627712618930723";
   cout << (isPower(s1, s2) ? "YES\n" : "NO\n");
   s1 = "19972001", s2 = "2022";
   cout << (isPower(s1, s2) ? "YES\n" : "NO\n");
   return 0;
}

Output

NO
NO

Use of IsPower() Method

In this possible approach, we are going to apply isPower() method to find if the one is a power of other form the given two numbers as a string.

Example

//C++ program to check if one number is power of other by using the isPower() method
#include <bits/stdc++.h>
using namespace std;
bool isPower(int x, long int y){
   if (x == 1)
   return (y == 1);
   long int pow = 1;
   while (pow < y)
   pow *= x;
   return (pow == y);
}
int main(){
   cout << isPower(16, 07) << endl;
   cout << isPower(10, 1) << endl;
   cout << isPower(97, 10) << endl;
   cout << isPower(2, 30) << endl;
   return 0;
}

Output

0
1
0
0

Use of Logarithm Function and Compare Method

In this possible approach, we are going to apply the logarithm function to calculate value and use the compare method to find if the one is a power of other form the given two numbers as a string.

Example

//C++ program to check if one number is power of other by using the logarithm function to calculate value and use the compare method also
#include <iostream>
#include <math.h>
using namespace std;
bool isPower(int x, int y){
   double res1 = log(y) / log(x);
   double res2 = log(y) / log(x);
   return (res1 == res2);
}
int main(){
   cout << isPower(16, 07) << endl;
   return 0;
}

Output

1

Approach 2: Using Binary Search With Loop Changing, CheckPowerofNumber() and Traversing Method

Use of the Binary Search With Loop Changing Method

In this possible approach, we are going to apply the binary search tree with loop chaining method to find if the one is a power of other form the given two numbers as a string.

double d = Math.log(Math.abs(x)) / Math.log(Math.abs(y));
if ((x > 0 && y > 0) || (x < 0 && y < 0)) {
if (d == (int) d) {
   return true;
} else {
   return false;
}
} else if (x > 0 && y < 0) {
   if ((int) d % 2 == 0) {
      return true;
   } else {
      return false;
   }
} else {
return false;

Example

//C++ program to check if one number is power of other by using the loop changing exponent with binary search
#include <iostream>
#include <cmath>
using namespace std;
bool isIntegerPower(int x, int y){
   int low = 0, high;
   int exp = 1;
   int val = y;
   while(1){
      val = pow((double)y, exp);
      if(val == x)
      return true;
      else if(val > x)
      break;
      low = exp;
      exp = exp * 2;
      high = exp;
   }
   int mid = (low + high)/2;
   while(low < high){
      val = pow((double)y, mid);
      if(val > x){
         high = mid-1;
      } else if(val == x){
         return true;
      } else if(val < x){
         low = mid+1;
      }
      mid = (low + high)/2;
   }
   return false;
}
int main(){
   cout<<isIntegerPower(1607,2);
}

Output

0

Use of the CheckPowerofNumber() Method

In this possible approach, we are going to apply the check power of a number method to find if the one is a power of other form the given two numbers as a string.

Example

//C++ program to check if one number is power of other by using the function is checkPowerofTwo() or not
#include <iostream>
#include <cmath>
using namespace std;
int checkPowerofTwo(int n);
int main(){
   int num;
   printf("Enter the number you want to test today: ");
   scanf("%d", &num);
   if (checkPowerofTwo(num) == 1)
   printf("\n%d - this number is a power of 2\n", num);
   else
   printf("\n%d - this number is not a power of 2\n", num);
   return 0;
}
int checkPowerofTwo(int x){
   if (x == 0)
   return 0;
   while( x != 1){
      if(x % 2 != 0)
      return 0;
      x /= 2;
   }
   return 1;
}

Output

Enter the number you want to test today: 1607
1607 - this number is not a power of 2
Enter the number you want to test today: 64
64 - this number is a power of 2

Use of the Traversing Method

In this possible approach, we are going to deploy the traversing method to find if the one is a power of other form the given two numbers as a string.

Example

//C++ program to check if one number is power of other by using traversing method
#include <bits/stdc++.h>
using namespace std;
int countPairs(int A[], int n, int k){
   int ans = 0;
   sort(A, A + n);
   for (int i = 0; i < n; i++){
      for (int j = i + 1; j < n; j++){
         int x = 0;
         while ((A[i] * pow(k, x)) <= A[j]){
            if ((A[i] * pow(k, x)) == A[j]){
               ans++;
               break;
            }
            x++;
         }
      }
   }
   return ans;
}
int main() {
   int A[] = {3, 8, 9, 12, 16, 10,7, 1997, 2001, 18, 4, 24, 2, 6};
   int n = sizeof(A) / sizeof(A[0]);
   int k = 3;
   cout << countPairs(A, n, k);
   return 0;
}

Output

6

Conclusion

Today in this article we have learned about how to implement the process to construct and apply the various methods to check if one number is power of other from the given two numbers as strings in a C++ environment. With the above mentioned logics, syntax and algoritm; we have tried to build some C++ codes to solve the problem statement in an efficient manner.

Updated on: 27-Dec-2023

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements