Given a number N and base b if N in base b representation starts with 1 print Yes else print No
Input : n = 6, b = 4 Output : Yes 6 can be written as in base 4 so answer is Yes as it starts with 1 Input : n = 24, b = 2 Output : Yes 24 can be written as in base 2 so answer is Yes as it starts with 1 Input : n = 24, b = 7 Output : No 24 can be written as in base 7 so answer is No as it starts with 3
When a number N is represented in base ‘b’ it gets converted to m+1 length sequence ….. which implies * + *…..+* = N
The smallest number in base b and starting with ‘1’ i.e. 100..00 and m+1 digits in base is
and largest number is 2*-1.So N should lie in this range.
<= N <= 2*-1
Now another thing to notice is that m cannot exceed floor((N)) because when we represent any number in base-2 it gets converted into a sequence of only 1s and 0s so the length of this sequence will always be greater than any other base representation and its length will be equal to floor((N))+1.
So to check for a given base 'b' if N starts with 1 or not we will traverse from m = 1 to m = floor((N)) and check if for any m N lies in the range <= N <= 2*-1 or not and accordingly print "Yes" or "No".
Yes Yes No Yes
This article is contributed by Ayush Jha. 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.
- Check if a String starts with any of the given prefixes in Java
- Inserting m into n such that m starts at bit j and ends at bit i.
- Why array index starts from zero ?
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Program to build DFA that starts and end with 'a' from input (a, b)
- Ways to split string such that each partition starts with distinct character
- Check if a number is divisible by all prime divisors of another number
- Count substrings that starts with character X and ends with character Y
- Check if a number with even number of digits is palindrome or not
- Program to check whether the given number is Buzz Number or not
- Program to check whether a number is Proth number or not
- Check if a number has same number of set and unset bits
- Find if a string starts and ends with another given string
- Check if a number is an Unusual Number or not
- Check if a number is a Trojan Number