
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Check if a number can be expressed as x^y (x raised to power y) in C++
Here we will check whether we can represent a number as power, like xy or not. Suppose a number 125 is present. This can be represented as 53. Another number 91 cannot be represented as power of some integer value.
Algorithm
isRepresentPower(num): Begin if num = 1, then return true for i := 2, i2 <= num, increase i by 1, do val := log(a)/log(i) if val – int(val) < 0.0000000001, then return true done return false End
Example
#include<iostream> #include<cmath> using namespace std; bool isRepresentPower(int num) { if (num == 1) return true; for (int i = 2; i * i <= num; i++) { double val = log(num) / log(i); if ((val - (int)val) < 0.00000001) return true; } return false; } int main() { int n = 125; cout << (isRepresentPower(n) ? "Can be represented" : "Cannot be represented"); }
Output
Can be represented
- Related Articles
- Check if a number can be expressed as 2^x + 2^y in C++
- Check if a number can be expressed as power in C++
- Find value of y mod (2 raised to power x) in C++
- Check if a number can be expressed as a^b in C++
- Check if a number can be expressed as a^b in Python
- Check if the following is true.\( (x+y)^{2}=(x-y)^{2}+4 x y \)
- Check if a number can be expressed as sum two abundant numbers in C++
- Check if a number can be expressed as a sum of consecutive numbers in C++
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Verify : (i) \( x^{3}+y^{3}=(x+y)\left(x^{2}-x y+y^{2}\right) \)(ii) \( x^{3}-y^{3}=(x-y)\left(x^{2}+x y+y^{2}\right) \)
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- If $x+y=19$ and $x-y=7$, then $xy=?$
- Factorize:$4(x - y)^2 - 12(x -y) (x + y) + 9(x + y)^2$
- If \( 3^{x}=5^{y}=(75)^{z} \), show that \( z=\frac{x y}{2 x+y} \).
- Factorize:\( x^{2}+y-x y-x \)

Advertisements