Related Articles
Find nth Term of the Series 1 2 2 4 4 4 4 8 8 8 8 8 8 8 8 …
• Last Updated : 13 Apr, 2021

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++

 `// CPP Program to find Nth term` `#include ``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;``}`

## Java

 `// 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));``    ``}``}`

## Python3

 `# 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`

## C#

 `// 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));``    ``}``}`

## PHP

 ``

## Javascript

 ``
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

Below is the implementation of the above approach:

## C++

 `// CPP Program to find Nth term` `#include ``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;``}`

## Java

 `// 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));``    ``}``}`

## Python3

 `# 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`

## C#

 `// 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`

## PHP

 ``

## Javascript

 ``
Output:
```8
1024```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up