This program checks whether a number n can be expressed as power of k and if yes, then to what power should k be raised to make it n. Following example will clarify :
Input : n = 16, k = 2 Output : yes : 4 Explanation : Answer is yes because 16 can be expressed as power of 2. Input : n = 27, k = 3 Output : yes : 3 Explanation : Answer is yes as 27 can be expressed as power of 3. Input : n = 20, k = 5 Output : No Explanation : Answer is No as 20 cannot be expressed as power of 5.
We have discussed two methods in below post
:Check if a number is a power of another number
In this post, a new Base Changing method is discussed.
In Base Changing Method, we simply change the base of number n to k and check if the first digit of Changed number is 1 and remaining all are zero.
Example for this : Let’s take n = 16 and k = 2.
Change 16 to base 2. i.e. (10000)2. Since first digit is 1 and remaining are zero. Hence 16 can be expressed as power of 2. Count the length of (10000)2 and subtract 1 from it, that’ll be the number to which 2 must be raised to make 16. In this case 5 – 1 = 4.
Another example : Let’s take n = 20 and k = 3.
20 in base 3 is (202)3. Since there are two non-zero digit, hence 20 cannot be expressed as power of 3.
This article is contributed by Shubham Rana. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the Nth digit from right in base B of the given number in Decimal base
- Given a number N in decimal base, find the sum of digits in any base B
- Add two integers of different base and represent sum in smaller base of the two
- Check if a number is in given base or not
- Check whether a number has consecutive 0's in the given base or not
- Check if a number N can be expressed in base B
- Check if N contains all digits as K in base B
- Convert a number into negative base representation
- Number System and Base Conversions
- Complement of a number with any base b
- Largest value of x such that axx is N-digit number of base b
- Ternary number system or Base 3 numbers
- Count of primes after converting given binary number in base between L to R
- Find the Largest N digit perfect square number in Base B
- Find most significant bit of a number X in base Y
- Largest N digit number in Base B
- Convert from any base to decimal and vice versa
- Convert all substrings of length 'k' from base 'b' to decimal
- Double Base Palindrome
- Find the total count of numbers up to N digits in a given base B