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.
- First digit in product of an array of numbers
- Program for nth Catalan Number
- Convert from any base to decimal and vice versa
- Count total divisors of A or B in a given range
- Finding n-th number made of prime digits (2, 3, 5 and 7) only
- 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