Consider the below series:
1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, 13, 17, …
This series is a mixture of 2 series – all the odd terms in this series form a Fibonacci series and all the even terms are the prime numbers in ascending order.
Write a program to find the Nth term in this series.
The value N is 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 the value of Nth term, no other characters/strings or message should be written to STDOUT.
For example, when N = 14, the 14th term in the series is 17. So only the value 17 should be printed to STDOUT.
int prime(int position);
int fibonacci(int position);
int main (int argc, char *argv[]){
int n,number,position,i;
if(argc>1)
n = atoi(argv[1]);
else
n = 1;
if(n % 2 == 0){
position = n/2;
number = prime(position);
}else{
position = n/2+1;
number = fibonacci(position);
}
printf("%d",number);
}
int prime(int position){
int i=2,count=0,j;
while(1){
int factors =0;
for(j=2;j<=i;j++){
if(i%j==0)
factors++;
}
if(factors==1)
count++;
if(count == position)
break;
i++;
}
return j-1;
}
int fibonacci(int position){
int i, first = 1 ,second = 1, third;
if(position == 1 || position ==2)
return 1;
// Fibonacci logic
for(i=3; i<= position;i++){
third = first + second;
first = second;
second = third;
}
return third;
}
# | Testcase | Input | Output | Success |
---|---|---|---|---|
1 | Public | 14 | - |
|
2 | Private | Hidden | Hidden | |
3 | Private | Hidden | Hidden |
# | Testcase |
---|---|
1 |
Public
Input - 14
Output : -
|
2 |
Private
Input - Hidden
Output : hidden
|
3 |
Private
Input - Hidden
Output : hidden
|