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++
#include <bits/stdc++.h>
using namespace std;
string findNthNo( int n)
{
string res = "" ;
while (n >= 1) {
if (n & 1) {
res = res + "3" ;
n = (n - 1) / 2;
}
else {
res = res + "5" ;
n = (n - 2) / 2;
}
}
reverse(res.begin(), res.end());
return res;
}
int main()
{
int n = 5;
cout << findNthNo(n);
return 0;
}
|
Java
public class GFG {
static String findNthNo( int n)
{
String res = "" ;
while (n >= 1 ) {
if ((n & 1 ) == 1 ) {
res = res + "3" ;
n = (n - 1 ) / 2 ;
}
else {
res = res + "5" ;
n = (n - 2 ) / 2 ;
}
}
StringBuilder sb = new StringBuilder(res);
sb.reverse();
return new String(sb);
}
public static void main(String args[])
{
int n = 5 ;
System.out.print(findNthNo(n));
}
}
|
Python3
def reverse(s):
if len (s) = = 0 :
return s
else :
return reverse(s[ 1 :]) + s[ 0 ]
def findNthNo(n):
res = "";
while (n > = 1 ):
if (n & 1 ):
res = res + "3" ;
n = ( int )((n - 1 ) / 2 );
else :
res = res + "5" ;
n = ( int )((n - 2 ) / 2 );
return reverse(res);
n = 5 ;
print (findNthNo(n));
|
C#
using System;
class GFG
{
public static string Reverse( string s)
{
char [] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string (charArray);
}
static string findNthNo( int n)
{
string res = "" ;
while (n >= 1)
{
if ((n & 1) == 1)
{
res = res + "3" ;
n = (n - 1) / 2;
}
else
{
res = res + "5" ;
n = (n - 2) / 2;
}
}
string sb = Reverse(res) ;
return sb ;
}
static void Main()
{
int n = 5;
Console.WriteLine(findNthNo(n));
}
}
|
PHP
<?php
function findNthNo( $n )
{
$res = "" ;
while ( $n >= 1)
{
if ( $n & 1)
{
$res = $res + "3" ;
$n = ( $n - 1) / 2;
}
else
{
$res = $res . "5" ;
$n = ( $n - 2) / 2;
}
}
$res = strrev ( $res );
return $res ;
}
$n = 5;
echo findNthNo( $n );
?>
|
Javascript
<script>
function reverseString(str) {
return str.split( "" ).reverse().join( "" );
}
function findNthNo( n) {
let res = "" ;
while (n >= 1) {
if ((n & 1) == 1) {
res = res + "3" ;
n = (n - 1) / 2;
}
else {
res = res + "5" ;
n = (n - 2) / 2;
}
}
sb = (res);
sb =reverseString(sb);
return (sb);
}
let n = 5;
document.write(findNthNo(n));
</script>
|
Time Complexity: O(log n)
Auxiliary Space: O(1)
Last Updated :
30 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...