Find the Nth Pure number

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.

Examples:

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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<bits/stdc++.h>
using namespace std;
  
// CPP program to find
// the Nth pure num
  
// Function to check if it
// is a power of 2 or not
bool isPowerOfTwo(int N)
{
    double number = log(N)/log(2);
    int checker = int(number);
    return number - checker == 0;
}
  
// if a number belongs to 4 series
// it should lie between 2^blocks -1 to
// 2^blocks + 2^(blocks-1) -1
bool isSeriesFour(int N, int digits)
{
    int upperBound = int(pow(2, digits)+pow(2, digits - 1)-1);
    int lowerBound = int(pow(2, digits)-1);
    return (N >= lowerBound) && (N < upperBound);
}
  
// Method to find pure number
string getPureNumber(int N)
{
    string numbers[N + 1];
  
    numbers[0] = "";
  
    int blocks = 0;
    int displacement = 0;
  
    // Iterate from 1 to N
    for (int i = 1; i < N + 1; i++) {
  
        // Check if number is power of two
        if (isPowerOfTwo(i + 1)) {
            blocks = blocks + 1;
        }
  
        if (isSeriesFour(i, blocks)) {
            displacement
                = int(pow(2, blocks - 1));
  
            // Distance to previous
            // block numbers
            numbers[i] = "4" + numbers[i - displacement] + "4";
        }
  
        else {
  
            displacement = int(pow(2, blocks));
  
            // Distance to previous
            // block numbers
            numbers[i] = "5" + numbers[i - displacement] + "5";
        }
    }
  
    return numbers[N];
}
  
// Driver Code
int main()
{
    int N = 5;
  
    string pure = getPureNumber(N);
  
    cout << pure << endl;
}
  
// This code is contributed by Surendra_Gangwar

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find
// the Nth pure number
  
import java.io.*;
  
class PureNumbers {
  
    // Function to check if it
    // is a power of 2 or not
    public boolean isPowerOfTwo(int N)
    {
        double number
            = Math.log(N) / Math.log(2);
        int checker = (int)number;
        return number - checker == 0;
    }
  
    // if a number belongs to 4 series
    // it should lie between 2^blocks -1 to
    // 2^blocks + 2^(blocks-1) -1
    public boolean isSeriesFour(
        int N, int digits)
    {
        int upperBound
            = (int)(Math.pow(2, digits)
                    + Math.pow(2, digits - 1)
                    - 1);
        int lowerBound
            = (int)(Math.pow(2, digits)
                    - 1);
        return (N >= lowerBound)
            && (N < upperBound);
    }
  
    // Method to find pure number
    public String getPureNumber(int N)
    {
        String[] numbers
            = new String[N + 1];
  
        numbers[0] = "";
  
        int blocks = 0;
        int displacement = 0;
  
        // Iterate from 1 to N
        for (int i = 1; i < N + 1; i++) {
  
            // Check if number is power of two
            if (isPowerOfTwo(i + 1)) {
                blocks = blocks + 1;
            }
  
            if (isSeriesFour(i, blocks)) {
                displacement
                    = (int)Math.pow(
                        2, blocks - 1);
  
                // Distance to previous
                // block numbers
                numbers[i]
                    = "4"
                      + numbers[i - displacement]
                      + "4";
            }
            else {
  
                displacement
                    = (int)Math.pow(
                        2, blocks);
  
                // Distance to previous
                // block numbers
                numbers[i]
                    = "5"
                      + numbers[i - displacement]
                      + "5";
            }
        }
  
        return numbers[N];
    }
  
    // Driver Code
    public static void main(String[] args)
        throws Exception
    {
        int N = 5;
  
        // Create an object of the class
        PureNumbers ob = new PureNumbers();
  
        // Function call to find the
        // Nth pure number
        String pure = ob.getPureNumber(N);
  
        System.out.println(pure);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find
// the Nth pure number
using System;
   
class PureNumbers {
   
    // Function to check if it
    // is a power of 2 or not
    public bool isPowerOfTwo(int N)
    {
        double number
            = Math.Log(N) / Math.Log(2);
        int checker = (int)number;
        return number - checker == 0;
    }
   
    // if a number belongs to 4 series
    // it should lie between 2^blocks -1 to
    // 2^blocks + 2^(blocks-1) -1
    public bool isSeriesFour(
        int N, int digits)
    {
        int upperBound
            = (int)(Math.Pow(2, digits)
                    + Math.Pow(2, digits - 1)
                    - 1);
        int lowerBound
            = (int)(Math.Pow(2, digits)
                    - 1);
        return (N >= lowerBound)
            && (N < upperBound);
    }
   
    // Method to find pure number
    public string getPureNumber(int N)
    {
        string[] numbers
            = new string[N + 1];
   
        numbers[0] = "";
   
        int blocks = 0;
        int displacement = 0;
   
        // Iterate from 1 to N
        for (int i = 1; i < N + 1; i++) {
   
            // Check if number is power of two
            if (isPowerOfTwo(i + 1)) {
                blocks = blocks + 1;
            }
   
            if (isSeriesFour(i, blocks)) {
                displacement
                    = (int)Math.Pow(
                        2, blocks - 1);
   
                // Distance to previous
                // block numbers
                numbers[i]
                    = "4"
                      + numbers[i - displacement]
                      + "4";
            }
            else {
   
                displacement
                    = (int)Math.Pow(
                        2, blocks);
   
                // Distance to previous
                // block numbers
                numbers[i]
                    = "5"
                      + numbers[i - displacement]
                      + "5";
            }
        }
   
        return numbers[N];
    }
   
    // Driver Code
    public static void Main()
    {
        int N = 5;
   
        // Create an object of the class
        PureNumbers ob = new PureNumbers();
   
        // Function call to find the
        // Nth pure number
        string pure = ob.getPureNumber(N);
   
        Console.Write(pure);
    }
}
  
// This code is contributed by chitranayal

chevron_right


Output:

5445

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.