Program to find Nth term in the series 0, 0, 2, 1, 4, 2, 6, 3, 8,…
Given a number N. The task is to write a program to find the N-th term in the below series:
0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8,…..
Examples:
Input : N = 10 Output : 4 Input : N = 7 Output : 6
On observing carefully, you will find that the series is a mixture of 2 series:
- Terms at odd positions in the given series form the series of even numbers in increasing order starting from 0. Like, 0,2,4,6,..
- Terms at even positions in the given series are derived from the previous term using the formula (previousTerm/2). That is, the terms at even positions are half of their previous term.
Now, it is known that every odd positioned term forms an even series starting from 0 and every even positioned term is the half of the previous odd positioned term.
Therefore first check whether the input number N is even or odd. If it is odd, set N=(N/2) + 1(since there are Two series running parallelly) and find the Nth term by using formula 2*(N-1) ( N-1 because the series starts with 0).
Similarly, if N is even, set N = N/2, use the previous formula and divide the answer by 2.
Below is the implementation of above approach:
C++
// CPP program to find N-th term // in the series #include <iostream> #include <math.h> using namespace std; // Function to find N-th term // in the series void findNthTerm( int n) { // If n is even if (n % 2 == 0) { n = n / 2; n = 2 * (n - 1); cout << n / 2 << endl; } // If n is odd else { n = (n / 2) + 1; n = 2 * (n - 1); cout << n << endl; } } // Driver code int main() { int X = 10; findNthTerm(X); X = 7; findNthTerm(X); return 0; } |
Java
// Java program to find N-th term // in the series // Function to find N-th term // in the series class GFG { static void findNthTerm( int n) { // If n is even if (n % 2 == 0 ) { n = n / 2 ; n = 2 * (n - 1 ); System.out.println(n / 2 ); } // If n is odd else { n = (n / 2 ) + 1 ; n = 2 * (n - 1 ); System.out.println(n); } } // Driver code public static void main(String args[]) { int X = 10 ; findNthTerm(X); X = 7 ; findNthTerm(X); } } // This code is contributed by Subhadeep |
Python 3
# Python 3 program to find N-th term # in the series # Function to find N-th term # in the series def findNthTerm(n): # If n is even if (n % 2 = = 0 ): n = n / / 2 n = 2 * (n - 1 ) print ( n / / 2 ) # If n is odd else : n = (n / / 2 ) + 1 n = 2 * (n - 1 ) print (n) # Driver code if __name__ = = "__main__" : X = 10 findNthTerm(X); X = 7 ; findNthTerm(X) |
C#
// C# program to find N-th term // in the series using System; // Function to find N-th term // in the series class GFG { static void findNthTerm( int n) { // If n is even if (n % 2 == 0) { n = n / 2; n = 2 * (n - 1); Console.Write(n / 2); } // If n is odd else { n = (n / 2) + 1; n = 2 * (n - 1); Console.Write(n); } } // Driver code public static void Main() { int X = 10; findNthTerm(X); Console.Write( "\n" ); X = 7; findNthTerm(X); } } // This code is contributed // by Smitha |
PHP
<?php // PHP program to find N-th // term in the series // Function to find N-th // term in the series function findNthTerm( $n ) { // If $n is even if ( $n % 2 == 0) { $n = $n / 2; $n = 2 * ( $n - 1); echo $n / 2 . "\n" ; } // If $n is odd else { $n = (int)( $n / 2) + 1; $n = 2 * ( $n - 1); echo $n . "\n" ; } } // Driver code $X = 10; findNthTerm( $X ); $X = 7; findNthTerm( $X ); // This code is contributed // by ChitraNayal ?> |
Javascript
<script> // JavaScript program to find N-th term // in the series // Function to find N-th term // in the series function findNthTerm(n) { // If n is even if (n % 2 == 0) { n = Math.floor(n / 2); n = 2 * (n - 1); document.write(Math.floor(n / 2) + "<br>" ); } // If n is odd else { n = Math.floor(n / 2) + 1; n = 2 * (n - 1); document.write(n + "<br>" ); } } // Driver code let X = 10; findNthTerm(X); X = 7; findNthTerm(X); // This code is contributed by Surbhi Tyagi </script> |
4 6
Time Complexity: O(1), the code will run in a constant time.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Please Login to comment...