N-th term in the series 1, 11, 55, 239, 991,….
Last Updated :
23 Mar, 2023
Given a number N. The task is to write a program to find the N-th term in the series:
1, 11, 55, 239, 991, …
Examples:
Input: N = 3
Output: 55
Input: N = 4
Output: 239
Approach-1:
On writing down the binary representation of the given numbers, a pattern can be observed.
1 = 1
11 = 1011
55 = 110111
239 = 11101111
.
.
.
Hence for N = 1, the answer will always be one. For N-th term the binary string will be (n-1)*1 + (0) + (n)*1 which is converted to decimal value to get the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int binaryToDecimal(string n)
{
string num = n;
int dec_value = 0;
int base = 1;
int len = num.length();
for ( int i = len - 1; i >= 0; i--) {
if (num[i] == '1' )
dec_value += base;
base = base * 2;
}
return dec_value;
}
int numberSequence( int n)
{
if (n == 1)
return 1;
string s = "" ;
for ( int i = 1; i < n; i++)
s += '1' ;
s += '0' ;
for ( int i = 1; i <= n; i++)
s += '1' ;
int num = binaryToDecimal(s);
return num;
}
int main()
{
int n = 4;
cout << numberSequence(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int binaryToDecimal(String n)
{
String num = n;
int dec_value = 0 ;
int base = 1 ;
int len = num.length();
for ( int i = len - 1 ; i >= 0 ; i--)
{
if (num.charAt(i) == '1' )
dec_value += base;
base = base * 2 ;
}
return dec_value;
}
static int numberSequence( int n)
{
if (n == 1 )
return 1 ;
String s = "" ;
for ( int i = 1 ; i < n; i++)
s += '1' ;
s += '0' ;
for ( int i = 1 ; i <= n; i++)
s += '1' ;
int num = binaryToDecimal(s);
return num;
}
public static void main(String args[])
{
int n = 4 ;
System.out.println(numberSequence(n));
}
}
|
Python 3
def binaryToDecimal(n):
num = n
dec_value = 0
base = 1
l = len (num)
for i in range (l - 1 , - 1 , - 1 ):
if (num[i] = = '1' ):
dec_value + = base
base = base * 2
return dec_value
def numberSequence(n):
if (n = = 1 ):
return 1
s = ""
for i in range ( 1 , n):
s + = '1'
s + = '0'
for i in range ( 1 ,n + 1 ):
s + = '1'
num = binaryToDecimal(s)
return num
if __name__ = = "__main__" :
n = 4
print (numberSequence(n))
|
C#
using System;
class GFG
{
static int binaryToDecimal(String n)
{
String num = n;
int dec_value = 0;
int base_ = 1;
int len = num.Length;
for ( int i = len - 1; i >= 0; i--)
{
if (num[i] == '1' )
dec_value += base_;
base_ = base_ * 2;
}
return dec_value;
}
static int numberSequence( int n)
{
if (n == 1)
return 1;
String s = "" ;
for ( int i = 1; i < n; i++)
s += '1' ;
s += '0' ;
for ( int i = 1; i <= n; i++)
s += '1' ;
int num = binaryToDecimal(s);
return num;
}
public static void Main()
{
int n = 4;
Console.WriteLine(numberSequence(n));
}
}
|
PHP
<?php
function binaryToDecimal( $n )
{
$num = $n ;
$dec_value = 0;
$base = 1;
$len = strlen ( $num );
for ( $i = $len - 1; $i >= 0; $i --)
{
if ( $num [ $i ] == '1' )
$dec_value += $base ;
$base = $base * 2;
}
return $dec_value ;
}
function numberSequence( $n )
{
if ( $n == 1)
return 1;
$s = "" ;
for ( $i = 1; $i < $n ; $i ++)
$s .= '1' ;
$s .= '0' ;
for ( $i = 1; $i <= $n ; $i ++)
$s .= '1' ;
$num = binaryToDecimal( $s );
return $num ;
}
$n = 4;
echo numberSequence( $n );
?>
|
Javascript
<script>
function binaryToDecimal(n)
{
let num = n;
let dec_value = 0;
let base = 1;
let len = num.length;
for (let i = len - 1; i >= 0; i--) {
if (num[i] == '1' )
dec_value += base;
base = base * 2;
}
return dec_value;
}
function numberSequence(n)
{
if (n == 1)
return 1;
let s = "" ;
for (let i = 1; i < n; i++)
s += '1 ';
// add 0
s += ' 0 ';
// add n 1' s at end
for (let i = 1; i <= n; i++)
s += '1' ;
let num = binaryToDecimal(s);
return num;
}
let n = 4;
document.write(numberSequence(n));
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(N), as we are using extra space for string.
Approach-2:
The series has a general formulae of 4N-2N-1 which is used to get the N-th term in series.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numberSequence( int n)
{
int num = pow (4, n) - pow (2, n) - 1;
return num;
}
int main()
{
int n = 4;
cout << numberSequence(n);
return 0;
}
|
Java
class GFG
{
static int numberSequence( int n)
{
int num = ( int )(Math.pow( 4 , n) -
Math.pow( 2 , n)) - 1 ;
return num;
}
public static void main(String args[])
{
int n = 4 ;
System.out.println(numberSequence(n));
}
}
|
Python 3
def numberSequence(n) :
num = pow ( 4 , n) - pow ( 2 , n) - 1
return num
if __name__ = = "__main__" :
n = 4
print (numberSequence(n))
|
C#
using System;
class GFG
{
static int numberSequence( int n)
{
int num = ( int )(Math.Pow(4, n) -
Math.Pow(2, n)) - 1;
return num;
}
public static void Main()
{
int n = 4;
Console.WriteLine(numberSequence(n));
}
}
|
PHP
<?php
function numberSequence( $n )
{
$num = pow(4, $n ) -
pow(2, $n ) - 1;
return $num ;
}
$n = 4;
echo numberSequence( $n );
?>
|
Javascript
<script>
function numberSequence(n)
{
let num = Math.pow(4, n) - Math.pow(2, n) - 1;
return num;
}
let n = 4;
document.write(numberSequence(n));
</script>
|
Time Complexity: O(logN), as we are using pow function which will cost logN time.
Auxiliary Space: O(1), as we are not using any extra space.
Approach-3:
As we can see the pattern in approach 1, here also we use that pattern to calculate N-th term. But instead of string to store, we use bitwise operator(“<<” and “|”) and built-in pow() function to calculate N-th term of the sequence.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numberSequence( int n)
{
if (n == 1)
return 1;
int q = pow (2, n) - 1;
int p = pow (2, (n - 1)) - 1;
p = p << (n + 1);
p = p | q;
return p;
}
int main()
{
int n = 4;
cout << numberSequence(n);
return 0;
}
|
Java
import java.math.BigInteger;
class GFG {
public static void main(String[] args)
{
int n = 4 ;
System.out.println(numberSequence(n));
}
public static int numberSequence( int n)
{
if (n == 1 ) {
return 1 ;
}
int q = ( int )Math.pow( 2 , n) - 1 ;
int p = ( int )Math.pow( 2 , n - 1 ) - 1 ;
p = p << (n + 1 );
p = p | q;
return p;
}
}
|
Python3
import math
def numberSequence(n: int ) - > int :
if n = = 1 :
return 1
q = pow ( 2 , n) - 1
p = pow ( 2 , (n - 1 )) - 1
p = p << (n + 1 )
p = p | q
return p
n = 4
print (numberSequence(n))
|
C#
using System;
class Program {
static int numberSequence( int n)
{
if (n == 1)
return 1;
int q = ( int )Math.Pow(2, n) - 1;
int p = ( int )Math.Pow(2, (n - 1)) - 1;
p = p << (n + 1);
p = p | q;
return p;
}
static void Main( string [] args)
{
int n = 4;
Console.WriteLine(numberSequence(n));
}
}
|
Javascript
function numberSequence(n) {
if (n === 1) {
return 1;
}
let q = Math.pow(2, n) - 1;
let p = Math.pow(2, n - 1) - 1;
p = p << (n + 1);
p = p | q;
return p;
}
let n = 4;
console.log(numberSequence(n));
|
Time Complexity: O(logN) [For pow function]
Auxiliary Space: O(1)
Approach-4:
As we can see the pattern in approach 2, here do the same but instead of using pow() function, here we use the math.log2() in combination with the operator “**” to calculate N-th term of the sequence.
Below is the implementation of the above approach:
C++
#include <cmath>
#include <iostream>
using namespace std;
int numberSequence( int n)
{
int num = pow (2, n * ( int )(log2(4)))
- pow (2, n * ( int )(log2(2))) - 1;
return num;
}
int main()
{
int n = 4;
cout << numberSequence(n) << endl;
return 0;
}
|
Java
import java.lang.Math;
public class GFG {
public static int numberSequence( int n)
{
int num
= ( int )Math.pow(
2 , n * ( int )(Math.log( 4 ) / Math.log( 2 )))
- ( int )Math.pow(
2 , n * ( int )(Math.log( 2 ) / Math.log( 2 )))
- 1 ;
return num;
}
public static void main(String[] args)
{
int n = 4 ;
System.out.println(numberSequence(n));
}
}
|
Python3
import math
def numberSequence(n):
num = 2 * * (n * int (math.log2( 4 ))) - 2 * * (n * int (math.log2( 2 ))) - 1
return num
if __name__ = = "__main__" :
n = 4
print (numberSequence(n))
|
C#
using System;
class GFG {
static int NumberSequence( int n)
{
int num
= ( int )(Math.Pow(2, n * ( int )(Math.Log(4, 2))))
- ( int )(Math.Pow(2,
n * ( int )(Math.Log(2, 2))))
- 1;
return num;
}
static void Main( string [] args)
{
int n = 4;
Console.WriteLine(NumberSequence(n));
}
}
|
Javascript
function numberSequence(n) {
let num = Math.pow(2, n * Math.log2(4)) - Math.pow(2, n * Math.log2(2)) - 1;
return num;
}
const n = 4;
console.log(numberSequence(n));
|
Time Complexity: O(Log(exponent))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...