You are not logged in. Please login at to post your questions!


a to the power b form

How to check if a number n is of the form a^b.

problem link

asked 06 Dec '17, 15:25

pandey_96's gravatar image

accept rate: 0%

Simple idea is to precompute a set containing all powers >= 2nd power of all numbers from 2 to 1e5. This will solve problem for input values upto 1e10 because minimum value of b is 2.

Now, for values of a > 1e5, we realize that 4th power of a will always be > 1e16, but in input, all N <= 1e16.

So, we will alsot take square root and cube root, and check if sqrtsqrt == N || cbrtcbrt*cbrt == N || set.contains(N)..

If any of above is true, answer is true.

Hope u find this helpful..


answered 06 Dec '17, 15:53

taran_1407's gravatar image

accept rate: 24%

edited 06 Dec '17, 15:53

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 06 Dec '17, 15:25

question was seen: 233 times

last updated: 06 Dec '17, 15:53