A self-descriptive number is an integer n in given base b is b digits long in which each digit at position p (the most significant digit being at position 0 and the least significant at position b – 1) counts how many times a digit p is in n.
For example in base 10, 6210001000 is a self descriptive number .
It is 10 digit number in base 10.
It has 6 at the position 0 and there are six 0s in 6210001000.
It has 2 at the position 1 and there are two 1s in 6210001000.
It has 1 at the position 2 and there is one 2s in 6210001000.
It has 0 at the position 3 and there are zero 3s in 6210001000.
It has 0 at the position 4 and there are zero 4s in 6210001000.
It has 0 at the position 5 and there are zero 5s in 6210001000.
It has 1 at the position 6 and there is one 6 in 6210001000.
It has 0 at the position 7 and there are zero 7s in 6210001000.
It has 0 at the position 8 and there are zero 8s in 6210001000.
It has 0 at the position 9 and there are zero 9s in 6210001000.
[Source : Wikipedia]
Here is a program to print all self-descriptive numbers below 100000000. In the below program we have just ignored one fact about the self-descriptive number that it should have as many number of digits as much the base is given.
Description of Program :
1 . Firstly all the digits get extracted from the outer loop and are stored in a variable b in each iteration.
2 . Then in the inner loop there is a count on how many times number i (this i is ith index of outer loop) is present in the string .
3 . Finally that count is compared with the digit present at the ith index stored in variable b.
1210 2020 21200 3211000
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count number of triplets with product equal to given number with duplicates allowed
- Find minimum number to be divided to make a number a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Given number of matches played, find number of teams in tournament
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Print a number strictly less than a given number such that all its digits are distinct.
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Count number of ways to divide a number in 4 parts
- Count Number of animals in a zoo from given number of head and legs
- Find the smallest number whose digits multiply to a given number n
- Count number of digits after decimal on dividing a number
- Build Lowest Number by Removing n digits from a given number
- Find the largest good number in the divisors of given number N
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.