Given a number and no. of digits to represent the number, find if given number can be represented in given no. of digits in any base from 2 to 32.
Input: 8 4 Output: Yes Possible in base 2 as 8 in base 2 is 1000 Input: 8 2 Output: Yes Possible in base 3 as 8 in base 3 is 22 Input: 8 3 Output: No Not possible in any base
We strongly recommend you to minimize your browser and try this yourself first.
The idea is to check all bases one by one starting from base 2 to base 32. How do we check for a given base? Following are simple steps.
1) IF number is smaller than base and digit is 1, then return true.
2) Else if digit is more than 1 and number is more than base, then remove the last digit from num by doing num/base, reduce the number of digits and recur.
3) Else return false
Below is the implementation of above idea.
This article is contributed by Mehboob Elahi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Count digits in given number N which divide N
- Print first k digits of 1/n where n is a positive integer
- Program to check if a given number is Lucky (all digits are different)
- Find nth term of the Dragon Curve Sequence
- Convert from any base to decimal and vice versa
- Number of Permutations such that no Three Terms forms Increasing Subsequence
- Check if the first and last digit of the smallest number forms a prime
- Print all substring of a number without any conversion
- Complement of a number with any base b
- Check if Decimal representation of an Octal number is divisible by 7