Consider the series:-
1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243, 64 ...........and so on.
Find the nth term in the given series ?
ex:- 9 th term print 16
6 th term print 9
Note:- We have to print only nth term not the whole series up to nth term.
Explanation:- The given series is a combination of two geometric progression series.
First series ( odd positions ) :- 1, 2, 4, 8, 16, 32, 64, ........and so on.
20 , 21 , 22 , 23 , 24 , 25 ,…….
Second series ( even positions ) :- 1, 3, 9, 27, 81, 243,..... and so on.
30 , 31 , 32 , 33 , 34 , 35 …….
Java
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Scanner; public class Series{ public static void main(String []args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if(n%2 != 0) System.out.print((int)Math.pow(2,(n-1)/2)); else System.out.print((int)Math.pow(3,(n-1)/2)); } } |
Python
n = int(input()) if n%2 != 0: print(2**((n-1)//2)) else: print(3**((n-1)//2))