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
Login/Signup to comment
n=int(input())
l=[]
m=0
#print(3^8)
if n>30:
print(“n should not exceed 30”)
elif n==1 or n==2:
print(“1”)
elif n>2 and n%2==0:
m= int(n/2) -1
print(pow(3,m))
elif n>1 and n%2!=0:
m= int(n/2)
print(pow(2,m))
else: print(“invalid input”)
#python
n=int(input(“enter the number: “))
a=1
b=1
for i in range(2,n):
if i%2!=0:
a=a*3
if i%2==0:
b=b*2
if n%2==0:
print(“The {} term in the series is {}”.format(n,a))
else:
print(“The {} term in the series is {}”.format(n,b))
n=int(input())
y=2*n+1
x=[0]*y
for i in range(0,2*n,2):
x[i]=i
for i in range(0,2*n//2):
x[2*i+1]=i
for i in range(2*n):
print(x[i])