TCS Write a Program Find the nth term of the series. 1,1,2,3,4,9,8,27,16,81,32,243,….

Problem

Question. Find the nth term of the series.

1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243,64, 729, 128, 2187 ….

This series is a mixture of 2 series – all the odd terms in this series form a geometric series and all the even terms form yet another geometric series. Write a program to find the Nth term in the series.

  • The value N in a positive integer that should be read from STDIN.
  • The Nth term that is calculated by the program should be written to STDOUT.
  • Other than value of n th term,no other character / string or message should be written to STDOUT.
  • For example , if N=16, the 16th term in the series is 2187, so only value 2187 should be printed to STDOUT.

You can assume that N will not exceed 30.

Link to this Question

Test Case 1

  • Input- 16
  • Expected Output – 2187

Test Case 2

  • Input- 13
  • Expected Output – 64

(TCS Ninja – Dec 2018 Slot 2)

Explanation

1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243,64, 729, 128, 2187 can represented as :

  • 2(0), 3(0),2(1), 3(1),2(2), 3(2),2(3), 3(3),2(4), 3(4),2(5), 3(5),2(6), 3(6) ….

There are two consecutive sub GP’s at even and odd positions

  • (GP-1) At Odd Positions (Powers of 2) – 1, 2, 4, 8, 16, 32, 64, 128
  • (GP-2) At Even Positions (Powers of 3) – 1, 3, 9, 27, 81, 243, 729, 2187

Clearly, for calculating Nth position value

  • If N is Even, Find (N/2) position in sub GP – 2
  • If N is Odd, Find (N/2 + 1) position in sub GP – 1

42 comments on “TCS Write a Program Find the nth term of the series. 1,1,2,3,4,9,8,27,16,81,32,243,….”


  • shushank

    n=int(input(“Enter number : “))
    if n%2==0:
    a1=n//2
    a1=a1-1
    c=1*(3**a1)
    print(c)
    else:
    a1=n//2
    c=1*(2**a1)
    print(c)


  • Mansi

    a=1
    b=1

    for i in range(17):
    if i%2==0:
    if(i==16):
    print(a)
    else:
    print(a,end=”,”)
    a=a*2
    else:
    if(i==16):
    print(a)
    else:
    print(b,end=”,”)
    b=b*3


  • Aditya

    package CodingChallenge;

    import java.util.Scanner;

    public class SeriesPrint {

    public static void main(String[] args) {
    int num1 = 1, num2 = 1;
    Scanner sc = new Scanner(System.in); //Taking Input
    int n = sc.nextInt();
    int str[] = new int[n]; //Creating Array

    for (int i = 2; i < str.length; i++) {
    if (i % 2 == 0) {
    num1=num1*2;
    str[i]=num1;
    }
    else {
    num2=num2*3;
    str[i]=num2;
    }
    }
    System.out.println(str[n-1]);

    }

    } //This Code is Quite Complicated but 100% write


  • Aditi

    a=1
    b=1
    n=int(input())
    for i in range (2,n+1):
    if i%2==0:
    a*=2
    else:
    b*=3

    if n%2==0:
    print(b)
    else:
    print(a)


  • Yuvraj

    import math
    lst = []
    n = int(input())
    for i in range(math.ceil(n/2)):
    lst.append(pow(2,i))
    lst.append(pow(3,i))
    if n % 2 == 0:
    print(lst[-1])
    else:
    print(lst[-2])


  • Saurabh Aherkar

    n = int(input())
    a = b = 1
    for i in range(1, n+1):
    if i%2 != 0:
    a *= 2
    else:
    b *= 3
    if n%2 != 0:
    print(‘{}th term of gp is {}’.format(n, a/2))
    else:
    print(‘{}th term of gp is {}’.format(n, a/2))


  • sheetal

    #include
    int main(){
    int i,k,c=1,v=1;
    int a[10000];
    scanf(“%d”,&k);
    for(i=1;i<=k;i++){
    a[i]=c;
    c=c*2;
    a[++i]=v;
    v=v*3;
    }
    printf(" \n%d",a[k]);
    return 0;
    }


  • Sumanth

    #include
    #include
    int main()
    {
    int n,x;
    printf(“Enter the number: “);
    scanf(“%d”,&n);
    x = (n%2==0) ? pow(3,(n-1)/2) : pow(2,(n-1)/2);
    printf(“%d”,x);
    return 0;
    }
    by srinivasa vasamshetty


  • Sumanth

    public static void main(String[] args) {
    int n=4;
    System.out.println(n%2==0?(int)Math.pow(3,(n-1)/2):(int)Math.pow(2,(n-1)/2));
    }


  • Danish

    #include
    #include
    using namespace std;
    int main()
    {
    int n;
    cout<>n;
    int j=0,k=0;
    for(int i=0;i<n;i++)
    {
    if(i%2 ==0)
    {
    cout<<pow(2,j)<<" ";
    j++;
    }
    if(i%2!=0)
    {
    cout<<pow(3,k)<<" ";
    k++;
    }
    }
    }