Program to find the N-th term of series 3, 5, 33, 35, 53…. | Set-2

Given a series of numbers composed only of digits 3 and 5. The first few numbers in the series are:

3, 5, 33, 35, 53, 55, …..

Examples:



Input: N = 2
Output: 5

Input: N = 5
Output: 53

For O(n) solution, refer Program to find N-th term of series 3, 5, 33, 35, 53….

. In this post, a O(log n) solution is discussed which is based on below pattern in numbers. The numbers can be seen.

                 ""
              /      \
            3         5
          /   \     /   \ 
        33    35   53    55
       / \   / \   / \  / \

The idea is to fill the required number from the end. We know can observe that the last digit is 3 if n is odd and the last digit is 5 if n is even. After filling the last digit, we move to the parent node in a tree. If n is odd, then the parent node corresponds to (n-1)/2. Else parent node corresponds to (n-2)/2.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find n-th number containing
// only 3 and 5.
#include <bits/stdc++.h>
using namespace std;
  
string findNthNo(int n)
{
    string res = "";
    while (n >= 1) {
        // If n is odd, append 3 and
        // move to parent
        if (n & 1) {
            res = res + "3";
            n = (n - 1) / 2;
        }
  
        // If n is even, append 5 and
        // move to parent
        else {
            res = res + "5";
            n = (n - 2) / 2;
        }
    }
  
    // Reverse res and return.
    reverse(res.begin(), res.end());
    return res;
}
  
// Driver code
int main()
{
    int n = 5;
    cout << findNthNo(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// java program to find n-th number
// containing only 3 and 5.
public class GFG {
  
    static String findNthNo(int n)
    {
        String res = "";
        while (n >= 1) {
  
            // If n is odd, append
            // 3 and move to parent
            if ((n & 1) == 1) {
                res = res + "3";
                n = (n - 1) / 2;
            }
  
            // If n is even, append
            // 5 and move to parent
            else {
                res = res + "5";
                n = (n - 2) / 2;
            }
        }
  
        // Reverse res and return.
        StringBuilder sb = new StringBuilder(res);
        sb.reverse();
        return new String(sb);
    }
  
    // Driver code
    public static void main(String args[])
    {
        int n = 5;
  
        System.out.print(findNthNo(n));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find 
# n-th number containing 
# only 3 and 5. 
def reverse(s): 
    if len(s) == 0
        return
    else
        return reverse(s[1:]) + s[0
            
def findNthNo(n): 
    res = ""; 
    while (n >= 1): 
            
        # If n is odd, append 
        # 3 and move to parent 
        if (n & 1): 
            res = res + "3"
            n = (int)((n - 1) / 2); 
                
            # If n is even, append 5 
            # and move to parent 
        else
            res = res + "5"
            n = (int)((n - 2) / 2); 
                
    # Reverse res 
    # and return. 
    return reverse(res); 
    
# Driver code 
n = 5
print(findNthNo(n)); 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find n-th number 
// containing only 3 and 5. 
using System;
  
class GFG 
// function to reverse a string
public static string Reverse(string s)
{
    char[] charArray = s.ToCharArray();
    Array.Reverse(charArray);
    return new string(charArray);
}
  
// function to find nth number
static string findNthNo(int n) 
    string res = ""
    while (n >= 1)
    
  
        // If n is odd, append 
        // 3 and move to parent 
        if ((n & 1) == 1) 
        
            res = res + "3"
            n = (n - 1) / 2; 
        
  
        // If n is even, append 
        // 5 and move to parent 
        else
        
            res = res + "5"
            n = (n - 2) / 2; 
        
    
  
    string sb = Reverse(res) ; 
    return sb ;
  
// Driver code 
static void Main()
    int n = 5; 
  
    Console.WriteLine(findNthNo(n)); 
}
  
// This code is contributed by ANKITRAI1

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP program to find n-th number 
// containing only 3 and 5.
  
function findNthNo($n)
{
    $res = "";
    while ($n >= 1) 
    {
        // If n is odd, append 3 
        // and move to parent
        if ($n & 1)
        {
            $res = $res + "3";
            $n = ($n - 1) / 2;
        }
  
        // If n is even, append 5 
        // and move to parent
        else 
        {
            $res = $res . "5";
            $n = ($n - 2) / 2;
        }
    }
  
    // Reverse res and return.
    $res = strrev($res);
    return $res;
}
  
// Driver code
$n = 5;
echo findNthNo($n);
  
// This code is contributed 
// by ChitraNayal
?>

chevron_right


Output:

53


My Personal Notes arrow_drop_up

pawanasipugmailcom

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.



Improved By : AnkitRai01, chitranayal