Given number of digits ‘N’ and base ‘B’, the task is to count all the ‘N’ digit numbers without leading zeros that are in base ‘B’.
Input: N = 2, B = 2 Output: 2 All possible numbers without leading zeros are 10 and 11. Input: N = 5, B = 8 Output: 28672
- If the base is ‘B’ then every digit of the number can take any value within the range [0, B-1].
- So, B ‘N’ digit numbers are possible with base ‘B’ (including the numbers with leading zeros).
- And, if we fix the first digit as ‘0’ then the rest of the ‘N-1’ digits can form a total of B numbers.
- So, total number of ‘N’ digit numbers with base ‘B’ possible without leading zeros are B – B.
Below is the implementation of above Approach:
# Python3 implementation of the approach
# function to count all permutations
def countPermutations(N, B):
# count of all permutations
x = B ** N
# count of permutations
# with leading zeros
y = B ** (N – 1)
# Return the permutations
# without leading zeros
print(x – y)
# Driver code
if __name__ == “__main__”:
N, B = 6, 4
# This code is contributed by Rituraj Jain
- Given a number N in decimal base, find number of its digits in any base (base b)
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Check if a given number can be represented in given a no. of digits in any base
- Write a program to add two numbers in base 14
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count numbers with same first and last digits
- Compute sum of digits in all numbers from 1 to n
- Print numbers with digits 0 and 1 only such that their sum is N
- Sum of all numbers that can be formed with permutations of n digits
- Numbers having Unique (or Distinct) digits
- Print all numbers less than N with at-most 2 unique digits
- Counting numbers of n digits that are monotone
- Number of digits in the product of two numbers