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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number of factors of very large number N modulo M where M is any prime number
- Minimum number of distinct powers of 2 required to express a given binary number
- Find the largest number smaller than integer N with maximum number of set bits
- 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
- Maximum number formed from array with K number of adjacent swaps allowed
- Count number of trailing zeros in Binary representation of a number using Bitset
- Find smallest number formed by inverting digits of given number N
- Largest number dividing maximum number of elements in the array
- Number of times the largest perfect square number can be subtracted from N
- Given number of matches played, find number of teams in tournament
- Smallest number dividing minimum number of elements in the array | Set 2
- Smallest number dividing minimum number of elements in the Array
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Number of distinct ways to represent a number as sum of K unique primes
- Minimum divisor of a number to make the number perfect cube
- Find smallest possible Number from a given large Number with same count of digits
- Find the minimum number to be added to N to make it a prime number
- Previous perfect square and cube number smaller than number N
- Number of possible permutations when absolute difference between number of elements to the right and left are given
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.