Given an integer N, the task is to find the Nth pure number.
A pure number has to satisfy three conditions:
1) It has even number of digits.
2) All digits are either 4 or 5.
3) And the number is a palindrome.
The Pure number series is: 44, 55, 4444, 4554, 5445, 5555, 444444, 445544, 454454, 455554 and so on.
Input: 5 Output: 5445 Explanation: 5445 is the 5th pure number in the series. Input: 19 Output: 45444454 Explanation: 45444454 is the 19th pure number in the series.
Approach: We will assume that 2 numbers make one single block. For each block, there is 2block number of pure numbers. For pure numbers with 1 block, there are 21 pure numbers, for numbers with 2 blocks, there are 22 numbers and so on.
- Pure numbers starting with 4, starts at position 2block – 1 for example, 4444 is at (22 -1 = 3) which means it is at third position in the series.
- Pure numbers starting with 5 starts at position 2block + 2(block-1) -1 for example, 5555 is at (2^2 + 2^1 -1 =5) which means it is at fifth position in the series.
A pure number at a block is essentially sandwiched between two 4’s or 5’s and is a combination of all previous block numbers. To understand it better let’s consider the example below:
- First pure number is 44 and second pure number is 55
- 4444 (“4″+ “44” + “4”) 44 from previous block
- 4554 (“4″+ “55” + “4”) 55 from previous block
- 5445 (“5″+ “44” + “5”) 44 from previous block
- 5555 (“5″+ “55” + “5”) 55 from previous block
This pattern repeats for all the numbers in the series.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal form game) | Set 2 (Game with Pure Strategy)
- Find minimum number to be divided to make a number a perfect square
- Find the largest number smaller than integer N with maximum number of set bits
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find a number which give minimum sum when XOR with every number of array of integers
- Given number of matches played, find number of teams in tournament
- 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
- Find count of digits in a number that divide the number
- Find the number of jumps to reach X in the number line from zero
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Find the smallest number whose digits multiply to a given number n
- Find the maximum number of composite summands of a number
- Find the number of ways to divide number into four parts such that a = c and b = d
- Find the total number of composite factor for a given number
- Find the largest good number in the divisors of given number N
- Find maximum number that can be formed using digits of a given number
- Find M-th number whose repeated sum of digits of a number is N
- Find Next number having distinct digits from the given number 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 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.