All possible numbers of N digits and base B without leading zeros
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’.
Examples:
Input: N = 2, B = 2 Output: 2 All possible numbers without leading zeros are 10 and 11. Input: N = 5, B = 8 Output: 28672
Approach:
- 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:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // function to count // all permutations void countPermutations(int N, int B) { // count of // all permutations int x = pow(B, N); // count of permutations // with leading zeros int y = pow(B, N - 1); // Return the permutations // without leading zeros cout << x - y << "\n"; } // Driver code int main() { int N = 6; int B = 4; countPermutations(N, B); return 0; } |
chevron_right
filter_none
Java
// Java implementation of the approach class GFG { // function to count // all permutations static void countPermutations(int N, int B) { // count of // all permutations int x = (int)Math.pow(B, N); // count of permutations // with leading zeros int y = (int)Math.pow(B, N - 1); // Return the permutations // without leading zeros System.out.println(x - y); } // Driver code public static void main(String[] args) { int N = 6; int B = 4; countPermutations(N, B); } } // This code is contributed by mits |
chevron_right
filter_none
Python3
# 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 countPermutations(N, B) # This code is contributed by Rituraj Jain |
chevron_right
filter_none
C#
// C# implementation of the approach using System; class GFG { // function to count // all permutations static void countPermutations(int N, int B) { // count of // all permutations int x = (int)Math.Pow(B, N); // count of permutations // with leading zeros int y = (int)Math.Pow(B, N - 1); // Return the permutations // without leading zeros Console.WriteLine(x - y); } // Driver code public static void Main() { int N = 6; int B = 4; countPermutations(N, B); } } // This code is contributed // by Akanksha Rai(Abby_akku) |
chevron_right
filter_none
PHP
<?php // PHP implementation of the approach // function to count all permutations function countPermutations($N, $B) { // count of all permutations $x = pow($B, $N); // count of permutations // with leading zeros $y = pow($B, $N - 1); // Return the permutations // without leading zeros echo ($x - $y), "\n"; } // Driver code $N = 6; $B = 4; countPermutations($N, $B); // This code is contributed // by Sach_Code` ?> |
chevron_right
filter_none
Output:
3072
Recommended Posts:
- Count numbers having N 0's and and M 1's with no leading zeros
- Count of N-bit binary numbers without leading zeros
- 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
- Check if the number is even or odd whose digits and base (radix) is given
- Write a program to add two numbers in base 14
- Count of N-digit numbers in base K with no two consecutive zeroes
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- 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 unique numbers that can be generated from N by adding one and removing trailing zeros
- 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



