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.
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
C
C++
Java
Python
C
#include<stdio.h> #include<math.h> int three(int n) { int x; //n-1 because powers start from 0 not 1 x = pow(3,n-1); printf("%d",x); } int two(int n) { int x; //n-1 because powers start from 0 not 1 x = pow(2,n-1); printf("%d",x); } int main() { int n; scanf("%d",&n); //Checking of the nth term will be at even position or odd position //Odd positions are powers of 2 //Even positions are powers of 3 if(n%2==0) { //nth position(if even) will be at n/2 position for sub GP-2 three(n/2); } else { //nth position(if odd) will be at (n/2 + 1) position for sub GP-1 two(n/2 + 1); } return 0; }
C++
#include<iostream> #include<math.h>
using namespace std; int three(int n) { int x; //n-1 because powers start from 0 not 1 x = pow(3,n-1); cout<< x; } int two(int n) { int x; //n-1 because powers start from 0 not 1 x = pow(2,n-1); cout<< x; } int main() { int n;
cin >> n; //Checking of the nth term will be at even position or odd position //Odd positions are powers of 2 //Even positions are powers of 3 if(n%2==0) { //nth position(if even) will be at n/2 position for sub GP-2 three(n/2); } else { //nth position(if odd) will be at (n/2 + 1) position for sub GP-1 two(n/2 + 1); } return 0; }
Java
import java.util.*; public class Main { public static void main(String[] args) { Scanner sin = new Scanner(System.in); int n = sin.nextInt(); System.out.println(n%2==0?(int)Math.pow(3,(n-1)/2):(int)Math.pow(2,(n-1)/2)); } } // we have used ternary operator above condition ? value_if_true : value_if_false
Python
num = int(input())
if(num%2==0):
num = num // 2
print(3**(num-1))
else:
num = num // 2 + 1
print(2**(num-1))
Login/Signup to comment
#py3
n = 13
if n%2 == 0:
n = (n//2)-1
print(3**n)
else:
n = (n//2)
print(2**n)
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)
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
if u give input it should return 4 but it returns 1 in the python program
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
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)
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])
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))
#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;
}
#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
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));
}
#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++;
}
}
}