Find nth Term of the Series 1 2 2 4 4 4 4 8 8 8 8 8 8 8 8 …


Given a number n, the task is to find the nth term of the Series

1 2 2 4 4 4 4 8 8 8 8 8 8 8 8 …

Example:

Input: n = 9
Output: 8

Input: n = 1025
Output: 1024

Naive Approach:

  1. Run a loop from i = 0 to n
  2. Inside loop increment i by i+k
  3. Inside loop increment k by 2*k
  4. Run above loop while i is less than n
  5. Return k/2 as the result

Complexity: log(n)

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to find Nth term
  
#include <bits/stdc++.h>
using namespace std;
  
// Function that will return nth term
int getValue(int n)
{
    int i = 0, k = 1;
  
    while (i < n) {
        i = i + k;
        k = k * 2;
    }
  
    return k / 2;
}
  
// Driver Code
int main(void)
{
  
    // Get n
    int n = 9;
  
    // Get the value
    cout << getValue(n) << endl;
  
    // Get n
    n = 1025;
  
    // Get the value
    cout << getValue(n) << endl;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find Nth term
  
class GFG
{
      
    // Function that will return nth term
    static int getValue(int n)
    {
        int i = 0, k = 1;
      
        while (i < n) {
            i = i + k;
            k = k * 2;
        }
      
        return k / 2;
    }
      
    // Driver Code
    public static void main(String []args)
    {
      
        // Get n
        int n = 9;
      
        // Get the value
        System.out.println(getValue(n));
          
        // Get n
        n = 1025;
      
        // Get the value
        System.out.println(getValue(n));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to find Nth term 
  
# Function that will return nth term 
def getValue(n): 
  
    i = 0
    k = 1
  
    while (i < n):
        i = i + k; 
        k = k * 2
  
    return int(k / 2); 
  
# Driver Code 
  
# Get n 
n = 9
  
# Get the value 
print(getValue(n)); 
  
# Get n 
n = 1025
  
# Get the value 
print(getValue(n)); 
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find Nth term
  
using System;
class GFG
{
      
    // Function that will return nth term
    static int getValue(int n)
    {
        int i = 0, k = 1;
      
        while (i < n) {
            i = i + k;
            k = k * 2;
        }
      
        return k / 2;
    }
      
    // Driver Code
    public static void Main()
    {
      
        // Get n
        int n = 9;
      
        // Get the value
        Console.WriteLine(getValue(n));
          
        // Get n
        n = 1025;
      
        // Get the value
        Console.WriteLine(getValue(n));
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to find Nth term 
  
// Function that will return nth term 
function getValue($n
    $i = 0; $k = 1; 
  
    while ($i < $n)
    
        $i = $i + $k
        $k = $k * 2; 
    
  
    return (int)$k / 2; 
  
// Driver Code 
  
// Get n 
$n = 9; 
  
// Get the value 
echo getValue($n),"\n"
  
// Get n 
$n = 1025; 
  
// Get the value 
echo getValue($n),"\n"
  
// This code is contributed by ajit
?>

chevron_right


Output:

8
1024

Efficient Approach: This Problem can be solved in O(1) time complexity.
Let nth term of the sequence be equal to 2m

  1+2+4+8+.... +2^{m-1} < n \\  2^m -1 <n \\  2^m < n+1 \\  m < log_2 (n+1)\\

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to find Nth term
  
#include <bits/stdc++.h>
using namespace std;
  
// Function that will return nth term
int getValue(int n)
{
    // Find log of n+1 on base 2
    int result = (floor)(log(n + 1) / log(2));
  
    return pow(2, result);
}
  
// Driver Code
int main(void)
{
    // Get n
    int n = 9;
  
    // Get the value
    cout << getValue(n) << endl;
  
    // Get n
    n = 1025;
  
    // Get the value
    cout << getValue(n) << endl;

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find Nth term
  
import java.lang.*;
import java.lang.Math;
import java.io.*;
  
class GFG {
    // Function that will return nth term
static double getValue(double n)
{
    // Find log of n+1 on base 2
 double result =(Math.floor(Math.log(n + 1) / Math.log(2)));
  
    return Math.pow(2, result);
}
  
// Driver Code
    public static void main (String[] args) {
    // Get n
    double n = 9;
    // Get the value
    System.out.println (getValue(n));
    // Get n
    n = 1025;
    // Get the value
        System.out.println (getValue(n));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to find Nth term
import math
  
# Function that will return nth term
def getValue(n):
  
    # Find log of n+1 on base 2
    result = int(math.floor(math.log(n + 1) / 
                            math.log(2)))
    return int(math.pow(2, result))
  
# Driver code
n = 9
print(getValue(n))
  
n = 1025
print(getValue(n))
  
# This code is contributed 
# by Shrikant13 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find Nth term
using System;
  
class GFG
{
    // Function that will return nth term
    static double getValue(double n)
    {
        // Find log of n+1 on base 2
        double result =(Math.Floor(Math.Log(n + 1) / Math.Log(2)));
        return Math.Pow(2, result);
    }
  
    // Driver Code 
    public static void Main ()
    {
        // Get n
        double n = 9;
          
        // Get the value
        Console.WriteLine(getValue(n));
          
        // Get n
        n = 1025;
          
        // Get the value
        Console.WriteLine (getValue(n));
    }
}
  
// This code is contributed by SoM15242

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to find Nth term
// Function that will return nth term
function getValue($n)
{
    // Find log of n+1 on base 2
    $result = (int)(log($n + 1) / log(2));
  
    return pow(2, $result);
}
  
// Driver Code
  
// Get n
$n = 9;
  
// Get the value
echo getValue($n), "\n";
  
// Get n
$n = 1025;
  
// Get the value
echo getValue($n), "\n";
  
// This code is contributed by ajit
?>

chevron_right


Output:

8
1024


My Personal Notes arrow_drop_up

All powers are within you You can do anything and everything Believe in that

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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.