Computer Science, asked by meghavarshinicp, 1 year ago

Consider the below series : 1,2,1,3,2,5,3,7,5,11,8,13,13,7.. this series is a misture of 2 series- all the odd terms in this series forn a fibonacci series and all the even term are the prime number in the ascending order. write a program to find the nth term in series

Answers

Answered by nitish8089
6

code......................
import java.util.Scanner;
class Series1
{
public static int oddSeries(int x){
int y;
if(x==1||x==3){
return 1;
}
else{
y=oddSeries(x-4)+oddSeries(x-2);
}
return y;
}
public static int evenSeries(int x){
int a=0;
int n=2;
int k=0;
do{
int count=0;
for(int i=2;i<=n;i++){
if(n%i==0){++count;}
}
if(count==1){
a=n;
n++;
k++;}
else{
n++;}
}while(k<(x/2));

return a;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter the nth digit whose value you want ");
int n=sc.nextInt();
if(n>0){
if(n%2==1){
int z=Series1.oddSeries(n);
System.out.println(z);
}
else{
int b=Series1.evenSeries(n);
System.out.println(b);
}

}
else{
System.out.println("invalid input");
}
}
}
Answered by kankitbtech15pedwnr
1

package tcs_test1;

import java.util.Arrays;

import java.util.Scanner;

public class Test1 {

public static void main(String[] args) {

 // TODO Auto-generated method stub

 System.out.println("enter n to generate series");

 Scanner sc=new Scanner(System.in);

 int n=sc.nextInt();

 int a=1,b=1;

 int[] intarray=new int[2000000];

 int[] intarray_eve=new int[2000000];

 int[] intarray_odd=new int[2000000];

 intarray_odd[0]=1;

 intarray_odd[1]=1;

 int f=2;

 boolean isPrime=true;

 for(int i=1;i<=n-2;i++)

 {

   

  int sum=a+b;

  a=b;

  b=sum;

  intarray_odd[i+1]=sum;

 }

 for(int j=1;j<=n;j++)

 {

   isPrime=CheckPrime(f);

   if(isPrime)

   {

    intarray_eve[j-1]=f;

   }

   else {

    j--;

   }

   f++;

    }

 int z=0;

 for(int k=0;k<=n;k=k+2)

 {

    intarray[k]=intarray_odd[z];

    z++;

 }

 int x=0;

 for(int k=1;k<=n;k=k+2)

 {

 intarray[k]=intarray_eve[x];

 x++;

   }

 System.out.println(intarray[n-1]);

 

 

}

public static boolean CheckPrime(int numberToCheck) {

   int remainder;

   for (int i = 2; i <= numberToCheck / 2; i++) {

       remainder = numberToCheck % i;

       //if remainder is 0 than numberToCheckber is not prime and break loop. Elese continue loop

       if (remainder == 0) {

           return false;

       }

   }

   return true;

}

}

Similar questions