Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Check if a number can be expressed as power | Set 2 (Using Log)
Next article icon

Check if a number can be expressed as power | Set 2 (Using Log)

Last Updated : 14 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Check if a number can be expressed as x^y (x raised to power y) 
Given a positive integer n, find if it can be expressed as x^y where y > 1 and x > 0. x and y both are integers.
Examples : 
 

Input:  n = 8 Output: true 8 can be expressed as 2^3  Input:  n = 49 Output: true 49 can be expressed as 7^2  Input:  n = 48 Output: false 48 can't be expressed as x^y


 


We have discussed two different approaches in below post.
Check if a number can be expressed as x^y (x raised to power y).
The idea is find Log n in different bases from 2 to square root of n. If Log n for a base becomes integer then result is true, else result is false. 
 

C++
// CPP program to find if a number // can be expressed as x raised to // power y. #include <bits/stdc++.h> using namespace std;  bool isPower(unsigned int n) {     // Find Log n in different bases     // and check if the value is an     // integer     for (int x=2; x<=sqrt(n); x++) {         float f = log(n) / log(x);         if ((f - (int)f) == 0.0)              return true;             }     return false; }  // Driver code int main() {     for (int i = 2; i < 100; i++)         if (isPower(i))             cout << i << "  ";     return 0; } 
Java
// Java program to find if a number // can be expressed as x raised to // power y. class GFG {          static boolean isPower(int n)     {         // Find Log n in different         // bases and check if the          // value is an integer         for (int x = 2; x <=                 (int)Math.sqrt(n); x++)          {             float f = (float)Math.log(n) /                        (float) Math.log(x);                                    if ((f - (int)f) == 0.0)                  return true;              }         return false;     }          // Driver code      public static void main(String args[])     {         for (int i = 2; i < 100; i++)             if (isPower(i))                 System.out.print( i + " ");     } }  // This code is contributed by Sam007 
Python3
# Python3 program to find if a number # can be expressed as x raised to # power y. import math  def isPower(n):      # Find Log n in different      # bases and check if the      # value is an integer     for x in range(2,int(math.sqrt(n)) + 1):               f = math.log(n) / math.log(x);         if ((f - int(f)) == 0.0):              return True;               return False;  # Driver code for i in range(2, 100):     if (isPower(i)):         print(i, end = " ");  # This code is contributed by mits 
C#
// C# program to find if a number // can be expressed as x raised to // power y. using System;  class GFG  {     static bool isPower(int n)     {         // Find Log n in different         // bases and check if the          // value is an integer         for (int x = 2;                   x <= (int)Math.Sqrt(n); x++)          {             float f = (float)Math.Log(n) /                        (float) Math.Log(x);             if ((f - (int)f) == 0.0)                  return true;              }         return false;     }     // Driver Code     public static void Main()     {         for (int i = 2; i < 100; i++)             if (isPower(i))                 Console.Write( i + " ");     }  }  // This code is contributed by Sam007 
PHP
<?php // PHP program to find if a number // can be expressed as x raised to // power y.  function isPower($n) {     // Find Log n in different      // bases and check if the      // value is an integer     for ($x = 2; $x <= sqrt($n); $x++)      {         $f = log($n) / log($x);         if (($f - (int)$f) == 0.0)              return true;          }     return false; }  // Driver code for ($i = 2; $i < 100; $i++)     if (isPower((int)$i))         echo $i." ";  // This code is contributed by Sam007 ?> 
JavaScript
<script>  // javascript program to find if a number // can be expressed as x raised to // power y.  function isPower(n) {         // Find Log n in different         // bases and check if the         // value is an integer         for (x = 2; x <= parseInt( Math.sqrt(n)); x++)          {             var f =  Math.log(n) /  Math.log(x);              if ((f - parseInt( f)) == 0.0)                 return true;         }         return false;     }      // Driver code              for (i = 2; i < 100; i++)             if (isPower(i))                 document.write(i + " ");  // This code contributed by Rajput-Ji  </script> 

Output: 
4  8  9  16  25  27  32  36  49  64  81

 

Time Complexity : O(sqrt(N))

Auxiliary Space : O(1) ,as we are not using any extra space


Next Article
Check if a number can be expressed as power | Set 2 (Using Log)

C

coder_11
Improve
Article Tags :
  • Mathematical
  • DSA
Practice Tags :
  • Mathematical

Similar Reads

    Check if a number can be expressed as a^b | Set 2
    You have given a number n. Check if a number can be represented in the form of pow(a, b) (a^b). Examples: Input : 4 Output : Yes 22 = 4 Input : 12 Output : No We have discussed two approaches in Check if a number can be expressed as x^y (x raised to power y). In this post, a more efficient solution
    3 min read
    Check if a number can be expressed as x^y (x raised to power y)
    Given a positive integer n, find if it can be expressed as xy where y > 1 and x > 0. x and y both are integers. Examples : Input: n = 8 Output: true 8 can be expressed as 23 Input: n = 49 Output: true 49 can be expressed as 72 Input: n = 48 Output: false 48 can't be expressed as xyRecommended
    11 min read
    Check if a number can be expressed as 2^x + 2^y
    Given a number n, we need to check if it can be expressed as 2x + 2y or not . Here x and y can be equal.Examples : Input : 24 Output : Yes Explanation: 24 can be expressed as 24 + 23 Input : 13 output : No Explanation: It is not possible to express 13 as sum of two powers of 2. If we take few exampl
    9 min read
    Check if a number N can be expressed as the sum of powers of X or not
    Given two positive numbers N and X, the task is to check if the given number N can be expressed as the sum of distinct powers of X. If found to be true, then print "Yes", Otherwise, print "No". Examples: Input: N = 10, X = 3Output: YesExplanation:The given value of N(= 10) can be written as (1 + 9)
    5 min read
    Check if a number can be expressed as sum of two Perfect powers
    Given a positive integer n, the task is to check whether n can be expressed in the form of ax + by where x and y > 1 and a and b >= 0. If n can be expressed in the given form then print "Yes", otherwise print "No".Examples:Input: n = 5Output: YesExplanation: 5 can be expressed as 22 + 12 Input
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences