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. 

Consider the following 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.

Please comment the code in other languages below – 

#include <stdio.h>
#include<math.h>

int main() {
//code
int n;
scanf(“%d”, &n);
if(n % 2 == 1)
{
int a = 1;
int r = 2;
int term_in_series = (n+1)/2;
int res = pow(2, term_in_series – 1);
printf(“%d “, res);
}
else
{
int a = 1;
int r = 3;
int term_in_series = n/2;
int res = pow(3, term_in_series – 1);
printf(“%d “, res);
}

return 0;
}

Please send code in other langauges

16 comments on “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. ”


  • goutham

    #python
    num=int(input(“enter the number: “))
    a=0
    b=0
    print(a,b,end=”,”)
    for i in range(2,num+1):
    if i%2==0:
    a=a+7
    print(a,end=”,”)
    else:
    b=b+6
    print(b,end=”,”)


  • Nithish

    ******************************************JAVA******************************************
    import java.util.*;
    import java.math.*;
    public class Main
    {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int res = 0;
    if(N>30 || N<=0) {
    System.out.println("Invalid Input");
    }
    else {
    if(N%2 == 0) {
    res = (int) Math.pow(3, (N/2)-1);
    }
    else {
    res = (int) Math.pow(2, ((N+1)/2)-1);
    }
    System.out.print(N + "th term of the series is: ");
    System.out.println(res);
    }
    }
    }


  • sai

    #python
    n = int(input())
    if n%2 == 0:
    x = n//2 – 1
    print(3**x)
    else:
    print(2**(n//2))


  • vikas

    #python
    term=int(input())
    if term%2==0:
    term=term//2
    print((1/3)*(3**term))
    else:
    term=(term//2)+1
    print((1/2)*(2**term))


  • Easy

    #include
    #include
    int main()
    {
    //1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243, 64, 729, 128, 2187
    int n;
    scanf(“%d”,&n);
    printf(“%.f”,(n%2!=0)?pow(2,n/2):pow(3,n/2-1));
    }


  • Bimal

    This is right code for python user
    def find_the_nth_series(num):
    a= num
    if (num%2==0):
    num = num//2
    print(str(a)+’th term of the series is {}:’.format(3 ** (num-1)))
    else:
    num = (num//2)+1
    print(str(a)+’th term of the series is {}:’.format( 2** (num-1)))


  • Nayan

    *********************************************code in java*********************************************
    import java.util.Scanner;
    public class series2 {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int no = sc.nextInt();
    int a = 1, b = 1;
    System.out.print(a + ” ” + b + ” “);
    for (int i = 2; i <= no/2; i++) {
    a = a * 2;
    b = b * 3;
    System.out.print(a + " " + b + " ");
    }
    if (no % 2 == 0)
    System.out.println("\n"+b);
    else
    System.out.print(a);
    }
    }


  • vicious

    def odd(n):
    a=1
    for _ in range(1,n,2):
    a=a*2
    print(a)
    def even(n):
    a=1
    for _ in range(2,n,2):
    a=a*3
    print(a)
    n=int(input())
    if n%2==0:
    (even(n))
    else:
    (odd(n))


  • Akansha

    i am not getting correct term in python code please tell error
    def geometric1(n):
    f=[1]*(n+2)
    f[1]= 1
    for i in range(1,n+1):
    f[i]=f[1]*2^(i-1)
    if i==n:
    return f[n]

    def geometric2(n):
    f=[1]*(n+2)
    f[1]=1
    for i in range(2,n+1):
    f[i]=f[1]*3^(i-1)
    if i==n:
    return f[n]

    def nthterm(n):
    if n%2==0:
    n//=2
    N=geometric2(n)
    print(N)
    else:
    n=(n//2)+1
    N=geometric1(n)
    print(N)
    n=int(input())
    N=nthterm(n)


  • seema jain

    i tried code in python but i am not getting the correct answer please tell the error

    def geometric1(n):
    f=[1]*(n+2)
    f[1]= 1
    for i in range(1,n+1):
    f[i]=f[1]*2^(i-1)
    if i==n:
    return f[n]

    def geometric2(n):
    f=[1]*(n+2)
    f[1]=1
    for i in range(2,n+1):
    f[i]=f[1]*3^(i-1)
    if i==n:
    return f[n]

    def nthterm(n):
    if n%2==0:
    n//=2
    N=geometric2(n)
    print(N)
    else:
    n=(n//2)+1
    N=geometric1(n)
    print(N)
    n=int(input())
    N=nthterm(n)


  • Divya

    ter is error in the program given in python i didnot get the 16th term correctly


    • Pawan

      a=[1,1,2,3,4,9,8,27,16,81,32,243,64,729,128,2187]
      num=int(input(“enter the index you want to print: “))
      print(a[num])
      —————————————————————————————
      can’t just use it? why not easy code